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

CSC186 OBJECT ORIENTED PROGRAMMING

CSC186 OBJECT ORIENTED PROGRAMMING| JUHAIDA ISMAIL 1


LETS RECAP ☺

1. What is a class in Java?


2. Differentiate between predefined & user defined classes.
3. List FOUR (4) types of access modifiers supported by Java
language.

CSC186 OBJECT ORIENTED PROGRAMMING 2


LESSON OUTCOMES
Upon completion of this chapter, students should be able to:

▪ Understand type of methods in OOP


▪ Apply and used all types of methods in OOP

CSC186 OBJECT ORIENTED PROGRAMMING 3


METHODS IN OOP
Method(member functions) are accessed and invoked, respectively
using an object reference followed by a dot and at the right of the
dot the name of the data or function of the class.

Example :
bike1.setOwnerName(..); ---------- method

▪ Invoking a function of an object is known as “sending a


message” to that object.
▪ There are 5 basic methods when dealing with OOP:
▪ Constructor
▪ Storer/Mutator
▪ Retriever/Accessor
▪ Calculator/Processor
▪ Printer
METHODS IN OOP
1. CONSTRUCTOR METHOD

A constructor is a special method that is executed automatically


when a new instance of the class (object) is instantiated
(created).

▪ The constructors have three peculiarities (features):


✓ Constructor have the same name as the class itself.
✓ Constructors do not have a return type
✓ Constructors are invoked using the new operator when an
object is created.

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

public <class name> ( <parameters> ){


<statements>
}

Modifier Class Name Parameter

public Bicycle ( ) {
ownerName = "Unassigned";
} Statements

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

▪ THREE (3) types of constructors:

✓ default constructor – no parameters


✓ normal (parameterized)constructor – with parameters
✓ copy constructor – with parameters that pass objects

▪ A class can have more than one constructor.


▪ Constructor always used to set the initial value and to reserve
a memory space for the object.

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

1. DEFAULT CONSTRUCTOR

▪ Initialize all instance variables to default value (zero for


numeric type, null for object reference and false for Booleans).

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

1. DEFAULT CONSTRUCTOR (example)

class Desk {
private String deskType;
private double surf_Area;
private int no_of_Drawer;
private int desk_Purchase;

public Desk()
{ deskType=“ ”;
surf_Area=0;
no_of_Drawer=0;
desk_Purchase=0; }

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

2. NORMAL CONSTRUCTOR

▪ A normal (parameterized) constructor 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.

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
2. CONSTRUCTOR METHOD (con’t)

2. NORMAL CONSTRUCTOR (example)


class Desk{
private String deskType;
private double surf_Area;
private int no_of_Drawer;
private int desk_Purchase;

public Desk(String dType, double sArea,int noDrawer,


double dPurchase)

{ deskType=dType;
surf_Area=sArea;
no_of_Drawer= noDrawer;
desk_Purchase=dPurchase; }

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
1. CONSTRUCTOR METHOD (cont’)

3. COPY CONSTRUCTOR

▪ A copy constructor is a constructor that receives object from


the main method through parameters and initialize the data
members of an object with the given object values.

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
2. CONSTRUCTOR METHOD (con’t)

3. COPY CONSTRUCTOR (example)


class Desk{
private String deskType;
private double surf_Area;
private int no_of_Drawer;
private int desk_Purchase;

public Desk(Desk d)

{ deskType=d.deskType;
surf_Area=d.surf_Area;
no_of_Drawer= d.no_of_Drawer;
desk_Purchase=d.desk_Purchase; }

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
2. STORER/MUTATOR

▪ Storer is a method that alter variables and modify the states of


an object. This method name should be started with “set”
▪ The variable can be changed through argument(s) or by input
the value in the storer/mutator.

▪ Syntax :

//mutator method definition

modifier void setDataMember (list_of_parameters)


{
//method body
}

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
2. STORER/MUTATOR (cont’)

▪ Example #1 :

public void setNumerator (int value)


{ numerator = value; }

public void setDenominator (int value)


{ denominator = value; }

public void setFraction (int data1, int data2)


{
numerator = data1;
denominator = data2;
}
CSC186 OBJECT ORIENTED PROGRAMMING
METHODS IN OOP
2. STORER/MUTATOR (cont’)
▪ Example #2 :

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
3. ACCESSOR/RETRIEVER

▪ Accessor is the method that retrieves object data and usually


starts with “get”
▪ One accessor method may return only one variable to the
object.

▪ Syntax :

//accessor method definition

modifier returntype getDataMember ()


{
//method body
}

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
3. ACCESSOR/RETRIEVER (cont’)

▪ Example #1 :

public int getNumerator()


{ return numerator; }
public int getDenominator()
{ return denominator; }
public String getName()
{ return name; }

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
3. ACCESSOR/RETRIEVER (cont’)
▪ Example #2 :

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
4. PRINTER/DISPLAY

▪ This method is used to display the output to the screen. May


also use toString() method.
▪ A method that returns a string representation of an object
attributes.

▪ Syntax :

//toString method definition

modifier String toString()


{
//method body
}

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
4. PRINTER/DISPLAY (cont’)

▪ Example #1 :

//toString method definition

modifier String toString()


{
//method body
}

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
4. PRINTER/DISPLAY (cont’)
▪ Example #2 :

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
5. CALCULATOR/PROCESSOR

▪ This method that calculates or process the data members.


▪ Example #1 : Method that calculates the distance between
two points.

public double countDistance()


{ double distanceTwoPoints;
double m;

m=(x*x)+(y*y);
distanceTwoPoints=Math.sqrt(m);
return distanceTwoPoints;
}

CSC186 OBJECT ORIENTED PROGRAMMING


METHODS IN OOP
5. CALCULATOR/PROCESSOR (cont’)

▪ Example #2 :

public double findArea()


{
return radius * radius * 3.142;

}//end of method

CSC186 OBJECT ORIENTED PROGRAMMING


EXAMPLE(Let’s do it together)
Given the class for Student:
public class Student
{private String name;
private double test1Mark,test2Mark;//full mark for each test is 30

public Student(){..}
public Student(String name, double test1Mark,double test2Mark){..}

public void setStudent(String name, double test1Mark,double test2Mark){..}

//write all the retriever methods

public double calcTestMark(){..}


//totTest=((test1Mark/30)*10)+((test2Mark/30)*10)
//to calculate test mark that bring 20% of the assessment which is each quiz
//represent 10%

public String toString(){..} CSC186 OBJECT ORIENTED PROGRAMMING


EXAMPLE(cont.)-using default constructor

Your task are:


1) Define the Student class
2) Write a main application that will do:
a) Declare an object named stud with datatype Student
b) Input a detail for a student
c) Create an object by using default constructor
d) Calculate his/her total mark for test
e) Display the details of the student together with his/her mark
e) Display whether the students is pass or fail the total test. Mark for
passing the total test is 10.

CSC186 OBJECT ORIENTED PROGRAMMING


EXAMPLE(cont.)-using normal constructor

Your task are:


1) Define the Student class
2) Write a main application that will do:
a) Declare an object named stud with datatype Student
b) Input a detail for a student
c) Create an object by using normal constructor
d) Calculate his/her total mark for test
e) Display the details of the student together with his/her mark
e) Display whether the students is pass or fail the total test. Mark for
passing the total test is 10%.

CSC186 OBJECT ORIENTED PROGRAMMING


EXERCISE
Given the class for Athlete:
public class Athlete
{private String name;
private double weight;
private double height;

public Athlete(){..}
public Athlete(String nm, double w,double h){..}

public void setAthlete(String name, double weight, double height){..}

//write all the retriever methods

public double calcBMI(){..} //BMI=weight(kg)/(height(m)*height(m))


//to use pow can import java.lang.Math and to use it Math.pow(x,y);

public String toString(){..}


CSC186 OBJECT ORIENTED PROGRAMMING
EXERCISE(continue)
Your task are:
1) Define the Athlete class
2) Write a main application that will do:
a) Declare an object named ath with datatype Athlete
b) Create an object by using normal constructor
c) Input a detail for an athlete
d) Calculate his/her BMI value
e) Display an athlete detail together with the BMI value
f) Determine and display the BMI health status by using given table:
Classification BMI score
Underweight <18.5
Normal 18.5 – 24.9
Overweight 25.0 – 29.0
Obese 30.0 – 40.0
Extreme Obese >40.0
CSC186 OBJECT ORIENTED PROGRAMMING
THANK YOU

CSC186 OBJECT ORIENTED PROGRAMMING 30

You might also like