0% found this document useful (0 votes)
15 views102 pages

CC103 Lesson

Uploaded by

SHERYL MAE RODA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views102 pages

CC103 Lesson

Uploaded by

SHERYL MAE RODA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 102

COMPUTER PRO-

GRAMMING 02
COURSE OUTCOME
01 analyze the identified problem specification to come up with
possible and best solution;

02 design, implement, test, and debug a program, based on a given


specification that uses standard libraries in the
programming language;

03 apply problem solving skills and provide a foundation for


advanced programming courses using OOP methodology.

04 develop a GUI interface and related processing for an


application.
PRELIM LESSON GUIDE

OOP Con- Classes and Object and


cepts Methods Methods

INHERITANCE POLYMORPHISM INTERFACES


QUIZ 01
OBJECTIVES:

Determine the bene- Describe the ba-


fits of OOP ap- sic techniques of
proach; program design
in OOP
OOP Concepts

Need to understand what


objects and classes are;
 need to understand how
these concepts translate to
code.
OOP – is preoccupied with the manipulation of soft-
ware objects. OOP is a way of thinking about problem
solving and a method of software organization and
construction.
What is Object?
 are the basic run time
entities in an object
oriented system.
 is a software bundle
of variables and re-
lated methods.
Two Characteristics of an Object:
STATE Represents the data of an
object.

BEHAVIOR Represents the behavior


of an object such as de-
posit, withdraw, etc.
What is Object? (Cont.)

STATE

BEHAVIOR
What is Object? (cont.)

Dog a = new Dog();

Attribute/Variable Method

An Object can choose to expose its


variables to other objects allowing
those other objects to inspect and
even modify the variables.
Activity 01:
What is a Class?
-it is a “blueprint” or “prototype” that
defines the variables and methods
common to all objects.
- the values for instance variables are
provided by each instance of the
class.
Elements of Classes:

Fields – members/variables of a certain type.


Properties – these are special type of ele-
ments, which extend the functionality of the
field by giving ability of extra management
when extracting and recording it in the class.
Methods – they implement the manipulation
of the data.
Creating a Class sampleclass
C# JAVA
ACTIVITY 02:
Find the output:
OBJECTIVES:
Java Class Methods (Static and Public)
Class Method Definition
• Methods – is a collection of statements that are
grouped together to perform an operation.
Types of Method:
a. Methods that return a single value or object are
used to derive data from a task or operation.
b. Methods that perform other actions, such as
se-tting of variables values.
Class Method Definition
• Instance variables
– variables that are declared outside any method but
within the main class;
– usually declared within a class; and
– can be used on any method within the main class.

Class Method Definition
• The this keyword
- used if has the receiving method call of a
method definition;
- object variables are usually set as part of
the method definition’s declaration syntax.
Class Method Definition
• The this keyword
Class Method Definition
• Local Variables
- variables declared within a method definition;
- only visible to the methods in which they are
declared;
- is inaccessible from the rest of the class.
Information Hiding
 A programming mechanism restricting access to
some of the objects components.
 Details of a program’s code are hidden, especially
parts that other programmers or users need to
know and/or use.
 Allows a more abstract view of the method and
classes being used in a program.
JAVA MODIFIER
• Access Modifier – specifies whether
other classes can use a particular
field/method.
• Public modifier – the methods
visible to all classes everywhere.
• Private modifier – the methods
can only be visible to its own
class.
• Protected modifier – the mem-
ber can only be accessed within
its own package.
The most common optional specifier:
Static – use for class methods
Abstract – used when a method does not have a
body
Final – used when a method is not allowed to be
overridden by a sub class.
Return Type – is the data type of the value returned
by the value method, or void if the method does not
return a value.
Method Name:
 Should be a verb in lowercase;
 it cannot be the same with the class identi-
fier;
 multi-word begins with a verb in lowercase,
followed by adjective, noun and or starts with
capital letters.
Parameter – it is used to receive data from a
method.
Method Body – a set of instructions needed to com-
plete the required activity and it is enclosed between
braces.
• With Parameters • Without Parameters
CLASS METHOD
Encapsulation
What is Encapsulation?

 The process of combining data and actions into a


single item.
 Groups instance variables and methods into class.
 Hides implementation details.
What is Encapsulation? (cont.)
To create the function of Encapsulation:
- Declares the variables of a class as private.
- Provide public setter and getter methods to
modify and view the variables values.
What is Encapsulation? (cont.)
Note:
Class attributes can be made read-only - only use the get
method or write –only – only use the set method.
Constructor
a. A special method used to create and initialize objects.
b. Uses the new keyword for calling its method.
c. Must have the same as the class to which it belongs.
Constructor (cont.)
I. Static Variables.
I. Variables that are shared by all objects of their
class.
II. Can be given the public or private modifiers.
II. Static Method.
I. A method that has no relation to any object.
II. Written with the static keyword on the heading of
the method definition.
STATIC VARIABLES
Constructor (cont.)
III. The Math Class
I. A predefined class that provides a number
of standard mathematical methods.
II. Automatically provided by Java, therefore,
not needing an important statement.
III. All methods are static, you do not need an ob-
ject for a class.
CONSTRUCTOR IN CODES

Attributes

Constructor

Function
It is called when it
does not have any
parameter. It has a specific
numbers of parame-
ters.
• With Parameters • Without Parameters
It will be invoked at the time
of object creation.
you are not creating any constructor so
compiler provides you a default construc-
tor. Here 0 and null values are provided
by default constructor.
Activity 03: ½ sheet of paper
Complete the codes to have the output given.
Answer:
ASSIGNMENT: ½ sheet of paper

1. Design a Method and Identify the function


of each codes.
What is Inheritance?

 -It enables the use of an existing class to


define new classes.
 -Uses the keyword extends.
What is Inheritance?
(cont.)
SUPERCLASS AND SUBCLASS

Superclass (PARENT) - is the class where we


will inherit the attributes and methods.

Subclass (CHILD) - is the one who will inherit


the attributes and methods from a Superclass.
What is Polymorphism?

Ability of an object to take on many forms.


Allows you to make changes in the method def-
inition for the derived classes and have those
changes apply to the methods written in the
base class.
What is Polymorphism?(cont.)

Static Binding – the object is determined at


compiled time.
 Dynamic Binding – the object is determined at
run time.
What is Interface?

- use implements keyword;


- used to specify methods that a class must
implement;
- it contains headings for a number of public
methods.
What is Interface?(cont.)
Laboratory04: Encapsulation

Assignment.
Create a program that will use encapsulation.
Package

- It is a collection of related classes and inter-


faces that have been grouped together into a
folder.
Example:
import java.util.Scanner;
import java.util.*;
Static Variables and Static Method
Static Variables – is a class level variable and it
is common to all the class object.
Static Method – manipulates the static variables
in a class.
Static variables
Static Method
END
MIDTERM TOPIC
MIDTERM LESSON GUIDE

Exception File Input User Inter-


Handling and Output face
1. What is Exception Handling?
2. Create a program that will explain exception
handling.
3. Explain the process of your program based
on your item number 2 answer.

- write your answer on a document and save


as Activity02_midterm.docx
- Item number 2 copy the print screen of
your program and output to your .docx file
Lab01_Midterm.java
Topic 01: Exception Handling

 An exception is an event that occurs during execu-


tion of a program that disrupt the normal flow of in-
structions.
 Exception Handling is the process used to change
the normal flow of code execution if a specified ex-
ception occurs.
Exception Handling (cont.)

 try block is a block of code that might throw an ex-


ception that can be handled by a matching catch
block.
 catch block is a segment of code that can handle an
exception that might be thrown by the try block that
precedes.
Exception Handling (cont.)

 Unchecked exceptions are exceptions that occur


during execution.
Exception Handling (cont.)

 Checked exceptions are exceptions that occur dur-


ing compile time execution.
Checked Exception must be reported in any one
of the two ways:
 Using try-catch block
 Propagating the exception using throws
Try and Catch Block

 Try block should be followed by zero or more


catch blocks.
 When you expect the problems with any java
statements, then place those statements inside
the try block.
 When exception is raised with the try block
statements, then control will be transferred to
the corresponding catch block.
Try and Catch Block (cont.)

 Catch block should contain statements to han-


dle the exceptions raised by the try block
statements.
The throw keyword

•throw is a keyword used to throw excep-


tions explicitly.
•You can throw any checked or
unchecked exceptions.
•You can throw any built-in or user-de-
fined exceptions.
The exceptions which are defined and
implemented by application developer
as per project requirements are called
user defined exceptions.
try{
try{ // statements ASSIGNMENT_01:
// statements } catch(){ ½ SHEET OF PAPER
} catch(){ // statements
// statements }catch(){ WRITE A PROGRAM
}Your Picture Here Your Picture Here
// statements THAT WILL USE TRY
} CATCH SYNTAX.
TOPIC 02: File Input and Output

 Compute Files
 Types of Storage Devices:
 Volatile – it is referred to temporary memory.
 Non-Volatile – a memory which data or in-
formation remains keep within memory albeit
power is completed.
 A text file consists of data that can be read in a text
editor.
The Path and File Classes

 Path Class – it contains information about files and


directories, such as sizes, locations, creation dates,
and is used to check whether a file or directory exist.
 File Class – performs operations on files and direc-
tories, such as determining their attributes, creating
input and output streams, and deleting them.
The Path and File Classes (cont.)

 Absolute Path – it is a complete path. It does not


require any other information to locate a file on a
system.
 Relative Path – it depends on other path informa-
tion.
The Path Methods

 String toString() – returns the string representation


on the path, eliminating double backslashes.
 Path getFileName() – returns the file or directory
denoted by this path. This is the last item in the se-
quence of name elements.
The Path Methods(cont.)

 int getNameCount() – returns the number of the


name elements in the path.
 Path getName(int) – returns the name in the po-
sition of the path specified by the integer parame-
ter.
Arguments of the checkAccess() Method
Arguments Description
None Checks whether the file exists.
READ Checks whether the file exists and whether the program
has permission to read the file.
WRITE Checks whether the file exists and whether the program
has permission to write the file.
EXECUTE Checks whether the file exists and whether the program
has permission to execute the file.
File Organization, Streams, and Buffers

1. Character – can be any letter, number, or other spe-


cial symbol (such as punctuation mark) that makes
up data.
2. Field – is a group of data that has some meaning.
3. Record – is a collection of the fields that contain
data about an entity.
4. File – consist of related data.
Example:
The Input / Output Classes:
Class Description
InputStream Abstract class that contains method for per-
forming input.
FileInputStream Provides the capability to read disk from files.
BufferedInputStream Handles input from a systems standard or de-
fault input device(usually keyboard).
OutputStream Abstract class that contains method for per-
forming output.
FileOutputStream Provides the capability to write the disk file.
The Input / Output Classes: (cont)
Class Description
BufferedOutputStream Handles input from a systems standard or de-
fault outpud device (monitor)
PrintStream Contains methods for performing output that
never throws an exceptions (System.output)
Reader Abstract class for reading character stream;
BufferedReader Read text from a character- input stream,
buffering characters to provide for efficient
reading of characters, arrays, and lines
BufferedWriter Writes text to a character-output stream buffer-
ing characters to provide for the efficient writ-
ing of characters, arrays, and lines
TOPIC 03: User Interface (1)
 SWING COMPO-
NENTS
 JFRAME
 JLABEL
 LAYOUT MANAGER
 EVENTS
SWING COMPONENTS

 A user interface component allow user to interact


with a program.
 JAVA’s basic GUI programming: AWT and
SWING.
 Controls – or User Interface component.
JFRAME

 it is used to hold the other components


that will be displayed in the program. It
has a title, size, and its visibility can be
configured.
JFRAME (cont.)
Constructor of the JFrame Class:
 JFrame() – creates an invisible Jframe
without title.
JFrame a – new JFrame();
 JFrame(String Title) – construct with
invisble Jframe with title.
JFrame a = new JFrame(“BSIT”);
Constructor of the JFrame Class:
Cont.)
 JFrame(GrapahicsConfiguration gc) –
create a Jframe in specified Grapahic-
sConfiguration of a screen device with
a blank title. (monitor or printer)
Constructor of the JFrame Class:
Cont.)
 JFrame(String title, GrapahicsConfigura-
tion gc) – conducts a jframe with title and
the specified GraphicsConfiguration of a
screen.
METHODS OF JFRAMES
JLABEL

JLabel - is a component used for display-


ing text.
JLabel() – constructs a Jlabel without a ti-
tle and image.
JLabel a = new JLabel();
JLabel (String text) – creates a JLabel with
the specified text.
JLabel a = new JLabel(“BSIT”);
fr.add(lb);
JLabel (Icon image) – creates a JLabel with
the specified image.
icon a = new ImageIcon(“BSIT.jpg”);
JLabel a = new JLabel(“BSIT”);
fr.add(lb);

You might also like