Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

TOPIC 3: BASIC CONCEPTS OF CLASSES

CSC 435 OBJECT ORIENTED PROGRAMMING


CHAPTER OUTLINE

 CLASS CONCEPT
 CLASS DEFINITION
 DATA MEMBERS
 BASIC TYPES OF METHODS
 METHODS DEFINITION
 STATIC FIELDS
 PREDIFINED CLASSES AND WRAPPER CLASS
INTRODUCTION

 In OOP, the central modules are classes where data and the procedure to manipulate the data are encapsulated
 A Java program is composed of one or more classes:

CLASSES

Pre-defined classes User Defined classes

Class defined in JAVA Classes that defined by


standard class library programmer

Example: Define own class is the first step


• String towards mastering the skill
• Math necessary in building a big program
• Random
INTRODUCTION

 Every object is an instance of a class.


 A class is a type or category of objects. (normally starts with capital letter)
 A class provides blueprint (structure) for its objects.
JAVA PROGRAM ANATOMY

Java Classes (Programs)

Class Program App Program

-Data member (fields)


-Methods -public static void main( )
-static methods
-Constructors
-Mutators
-Accessors //how these methods can be
-Printer applied in App Program?
-Processor
CLASS CONCEPT

 The dependency diagram between the two classes is as follows:

 For this sample program, we have created two (2) classes — BicycleRegistration (the main class)
and Bicycle. So there are two source files for this program.
CLASS CONCEPT

Bicycle is the programmer-defined classes.


CLASS CONCEPT
(-)
private
modifier
/ data members

(+)
public
modifier
CLASS DEFINITION
CLASS DEFINITION: SYNTAX

Modifier class className


{
classMembers
}

Modifier (s)
classMembers
-Used to alter the behavior of the class
- Consists of named constants, variable
- type of modifiers
declarations, methods, other classes
(public, private and static)
CLASS DEFINITION: EXAMPLE

modifier Class name


DATA MEMBERS

❖ The components of a class are called members of the class


❖ Data members is the object attributes
 also known as attributes, fields, or properties
❖ Data members can be any data type (primitives or objects)
❖ The members are classified into 4 categories:

private String name;


private int ID;
Private double balance;
DATA MEMBERS: SYNTAX

AccessModifier datatype dataMemberName

private String Name


EXAMPLE OF CLASS WITH DATA MEMBERS

public class Account //class name


{
//data member
private String name;
private int ID;
private double balance;

//methods

}
BASIC TYPE OF METHODS:

1. Constructor

2. Accessor/ getter

 Method represents the


operation on the attributes 3. Mutator / setter
 The types of methods are:

4. Printer

5. Processor
METHOD DEFINITION: 1. CONSTRUCTOR

❖ A constructor is a special method that is


executed when a new instance of the class is Default Constructor
created. A constructor that takes no arguments. It will
❖ There are two types of constructor: Default set data members to default value.
Constructor and normal constructor

❖ If no Normal Constructor
constructor is Set all data members based on arguments.
provided, Will set data members based on what user
compiler uses specify using parameters.
default
constructor
that initializes:
METHOD DEFINITION: : 1. CONSTRUCTOR

Default Constructor: Syntax

Modifier className ()
{
constructor body
}

public Student ()
{
name = “”;
}
METHOD DEFINITION: 1. CONSTRUCTOR
Normal Constructor: Syntax
 It is a constructor that receives several values from the main method
through parameters and initialize the data members of an object with
the given values

Modifier className (parameter list)


{
constructor body
}

public Student (String name)


{
this.name = name;
}
EXAMPLE NORMAL CONSTRUCTOR

public Account (String n, int i, double b)


{
name = n;
ID = i ;
balance = b;
}
METHOD DEFINITION: 2. ACCESSOR @ GETTER

❖ Retrieve/read data members and return their values through the function type.
❖ Clients cannot directly access private instance variables, so classes will provide
public accessor/getter methods with this standard form:
❖ Also known as getter method
❖ Used to read / access the values stored in object’s variables
❖ Returns a value read
❖ No arguments (parameter list)
METHOD DEFINITION: 2. ACCESSOR @ GETTER
Syntax

public datatype get<variable>()


{
return <variable>
}

public String getName()


{
return name;
}
EXAMPLE OF ACCESSOR

public String getName ()


{
return name;
}
public int getID ()
{
return ID;
}
public double getBalance ()
{
return balance ;
}
METHOD DEFINITION: 3. MUTATOR @ SETTER
Syntax
❖ Also known as setter method
❖ Used to change one or more of an object’s values after the object is created and initialized
❖ It does not return any value, void method.
❖ Has arguments (parameter list) to set the variables

public void setName(String name)


{
this.name = name;
}
EXAMPLE OF MUTATOR (1)

public void setAccount (String n, int i, double b)


{
name = n;
ID = i ;
balance = b;
}
EXAMPLE OF MUTATOR (2)
public void setName (String n)
{
name = n;
}
public void setID (int i)
{
ID = i ;
}
public void setBalance (double b)
{
balance = b ;
}
METHOD DEFINITION: 4. PRINTER

❖ Used to display the values stored in object


❖ Returns a String representing the data members of an object.
❖ Can use toString() or without toString()

Example 2: Not Using toString()


Example 1: Using toString() public void displayInfo()
public String toString() {
{ System.out.println(“Name: “ +
return “Name: “ + name + name);
“\nAge: “ + age; System.out.println(“Age: “ +
} age);
}
EXAMPLE (TOSTRING())

public String toString()


{
return (“Name :”+ name + “\nID:” + ID
+ “\nBalance :” + balance);
}
EXAMPLE (DISPLAY())
public void display()
{
System.out.println(“Name :”+ name
+ “\nID:” + ID + “\nBalance :” +
balance);
}

public void display()


{
System.out.println(“Name :”+ name);
System.out.println(“ID:” + ID);
System.out.println(“Balance :” + balance);
}
METHOD DEFINITION: 5. PROCESSOR

❖ Method used for calculation purpose


❖ Example: Calculate the course grade, calculate gpa, etc..

Example
public double calculate
Area()
{
double area;
area = width * length;
return area;
}
EXAMPLE
public double calculateNewBalance(double withdrawal)
{
double newBalance;
newBalance = balance – withdrawal;
return newBalance;
}
OR
public void calculateNewBalance(double withdrawal)
{
double newBalance;
newBalance = balance – withdrawal;
System.out.println (“New balance: ” + newBalance);
}
STATIC METHOD

 A method that does not operate on an object but receives all of its data as
arguments.
 Independent function
 If the method has public modifier, it is referred as a general-purpose method which
means it is constructed to be used to perform a general purpose task.
 It uses the keyword static
public static void main(String[] args) {…..}
STATIC FIELDS

❖ Static field is also known as class field, where data shared by all instances of a class
❖ Static data belongs to the entire class not to individual object
❖ All objects of the class share the static data

Object 1 Object 2

public class Rectangle Width: 3 Width: 2


{ …… Both objects
Length: 5 Length: 4 (Object 1 & Object 2
private int width,length;
private static int counter; shares the same
} value of Counter
Counter
:2
PREDEFINED CLASSES
Usage Package Method Example

Dialog Box javax.swing showInputDialog(String) String w =


JOptionPane.showInputDialog(null,
“Enter name”);

showMessageDialog(null, “Good
showMessageDialog(String) Morning!”);

Format output java.text DecimalFormat n = new


format(String) DecimalFormat(“00.00”)
class name =
DecimalFormat To diplay decimal placeholder
Mathematical Static methods in abs(n) int x = Math.pow(4,2);
Methods class Math are pow(n1, n2)
used. sqrt(n1)
round(n)
min(x, y)
WRAPPER CLASSES

 Java provides routine for converting String to a primitive data types or vice versa

 It is called wrapper classes because the classes constructed by wrapping a class structure around the
primitive data types
WRAPPER CLASSES
Wrapper Method Description Example
Class
Integer parseInt(String) Converts a string to int int x = Integer.parseInt(s);

Integer toString(x) Converts an int to string String s = Integer.toString(x);

Long parseLong(string) Converts a string to long Long x = Long.parseLong(s);


int
Long toString(x) Converts a long int to String s = Long.toString(x);
string
Float parseFloat(s) Converts a string to float float x = Float.parseFloat(s);

Float toString(x) Converts a float to string String s = Float.toString(x);

Double parseDouble(s) Converts a string to double x =


double Double.parseDouble(s);
Double toString(x) Converts a double to String s = Double.toString(x);
string
JAVA PROGRAM

Class header

Attributes/ data
members/ variables

Default constructor :
initialize the object
variables to zero or null

Normal constructor :
Set all data members
based on arguments
JAVA PROGRAM

Setter/Mutator
JAVA PROGRAM

Accessor/Getter

Processor

Print
JAVA PROGRAM

 We have that Student class, but what we can do with it?


 We cannot run that class.
 To use that class, we need to create objects.
 Classes is just the blueprint to create objects, while objects are
instances of that class that we can use and manipulate.
 So, we need to create another class, which is the Main class.
 We call it as Main class since it contains the:
 Public static void main(String args[]) …
 Which we can RUN
JAVA PROGRAM – MAIN CLASS
Import the package : be able to use the pre-defined classes

Class header : modifier, reserved word class and class name

Main method

Method

You might also like