Chapter 4 - Classes Intermediate
Chapter 4 - Classes Intermediate
Classes –
Intermediate
By
En. Mohd Nizam bin Osman
(Senior Lecturer)
Department of Computer Science
Faculty of Computer and Mathematical Science, UiTM
Perlis
Objectives
• By the end of this chapter, students should be able to:
METHOD
OVERLOADING
• Example:
Method Overloading
public void methodA (int x)
CONCEPT:
Overloading a method name means
giving the same name to more than one
method within a class.
Method Overloading
(Example 1)
public class Overload
{
public static void main(String[] args)
{
double average1 = Overload.getAverage(40.0, 50.0);
double average2 = Overload.getAverage(1.0, 2.0, 3.0);
char average3 = Overload.getAverage('a', 'c');
System.out.println("average1 = " + average1);
System.out.println("average2 = " + average2);
System.out.println("average3 = " + average3);
} Method overloading
public static double getAverage(double first, double second)
{
return (first + second) / 2.0;
}
c1 c2 c1 c2
(15) (10)
(15) (10) circleA circleB
circleA circleB
16
(100)
Object as Parameter
(Example -Analysis)
STEP5 – After Returning from parameterTester(c1,c2)
c1 c2
(15) (10)
circleA circleB
X X
STEP 1
Sample Screen Output
c1 Radius = 20.0 STEP 3
c2 Radius = 10.0
STEP 4
circleA Radius = 15.0
circleB Radius = 100.0
c1 Radius = 15.0 STEP 5
c2 Radius = 10.0
17
Object as Parameter
(Example -Analysis)
• Therefore, programmers must be very careful
when using an object that has been passed as
a parameter, particularly when changes are
being made to objects of the same class.
Object as Method Type
• The classes that you create become data type.
• Example:
public static Person findEldest(Person p1, Person p2)
{
//method body
}
Object as Method Type
(Example)
public class Person{
private String name;
private int age;
• The variables for the object you have worked with so far
are designed to hold only one object at a time.
• Example:
Student stud = new Student();
Array of Objects
• Can solve using:
1. Using different variables to represent each object.
2. Repetition.
a l ?
• Example of using different variables:
a c tic
Student stud1 = new Student();
P r ?
i n g t ?
Student stud2 = new Student();
e a t j e c
Ch s ob
• Example of using repetition:
i o u
for (int i = 0; i<10; i++){
r e v
Student stud1 = new Student(); P
:
:
}
Array of Objects
• We can use arrays to manipulate objects.
public Staff () {
name = “”;
staffNo = 0;
stf[49]
salary = 0.0;
}
//other methods
}
stf[99]
Array of Objects
• Example: Suppose that you have 100 of staffs.
Staff[] stf = new Staff[100];
staffNo 0
stf
salary 0
stf[0]
name “”
stf[49] staffNo 0
salary 0
stf[99]
name “”
staffNo 0
salary 0
Array of Objects
(Example 1 – To Calculate Bonus based
on 60% of Salary)
import java.util.*;
public Staff(){
name = "";
staffNo = 0;
salary = 0.0;
}
stf[i].setName(name);
stf[i].setStaffNo(staffNo); Store the input onto the array
stf[i].setSalary(salary);
} using setter/mutator
(STEP 4)
Array of Objects
(Example 1 – To Calculate Bonus based
on 60% of Salary)
public class StaffApp
{ Array declaration
public static void main(String args[])
{
(STEP 1)
Scanner scan = new Scanner (System.in);
//declaration array of object
Staff[] stf = new Staff[10];
stf[i] = new Staff(name, staffNo, salary); Store the input onto the array
}
using normal constructor
(STEP 4)
Array of Objects
(Example 1 – To Calculate Bonus based
on 60% of Salary)
//to find the maximum bonus
double maxBonus = stf[0].calcBonus();
Staff maxObject = stf[0];
}
}
Composite Objects
• In addition to a class containing data and
methods, it can also contain other classes.
EmployeeTest
+main: (String[]): void
Composite Objects
(Example 1)
Date
Inner Class
Composite Objects
(Example 1)
public class Date
{
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
CompositionApp
+main: (String): void
Composite Objects
(Example 2)
import java.util.*;
public class Box
{
private double width;
private double length;
private double height;
private double volume;
private char category;
public Box()
{
width = 0.0;
length = 0.0;
height = 0.0;
}
(Container Class)
Outer Class
(Box Class)
Inner Class
Application
(Sorting and Searching)
Application
(Sorting and Searching)
public class Person {
private int age;
private char gender;
String name;
public Person( ) {
}
public Person (int ag, char gen, String nm){
age = ag;
gender = gen;
name = nm;
}
public int getAge() {
return age;
}
public char getGender() {
return gender;
}
public String getName() {
return name;
}
//other methods
}
Application
(Sorting and Searching)
class AddressBook {
private Person[] entry;
private int count;
public AddressBook( ) {
}
public AddressBook(int size) {
count = 0;
if (size <= 0 ) { //invalid data value
throw new IllegalArgumentException("Size must be positive.");
}
entry = new Person[size];
for (int i =0; i < entry.length; i++)
{
entry[i] = new Person();
}
System.out.println("array of "+ size + " is created.");
}
public void setDataPerson(int age, char gender, String name){
//other statements
}
// Other methods
}
Application
(Sorting and Searching)
public Person search(String searchName) {
Person foundPerson;
int loc = 0;
entry[99]
15 age
foundPerson =
entry[loc]; M gender
XX name
Application
(Sorting – Based on Name)
public Person[] sort ()
{
Person[] sortedList = new Person[count];
Person P1, P2, temp;
for (int i=0; i<count; i++)
sortedList[i] = entry[i];
int bottom;
boolean exchanged = true;
bottom = sortedList.length -2;
while(exchanged) {
exchanged = false;
for (int i=0; i<=bottom; i++) {
P1 = sortedList[i];
P2 = sortedList[i+1];
if (P1.getName().compareTo(P2.getName()) > 0){
sortedList[i] = P2;
sortedList[i+1] = P1;
exchanged = true;
}
}
bottom--;
}
return sortedList;
}
BEFORE AFTER
SORTING SORTING
entry entry
0 1 2 3 4 …….. 0 1 2 3 4 ……..
0 1 2 3 4 …….. 0 1 2 3 4 ……..
sortedList sortedList
Person[]
for (int sortedList
i=0; i<count;
Person[count];
= new
i++)
sortedList[i] = entry[i]; Sorting - Analysis
Application
(Sorting [Analysis])
P1 P2 sortedList
sortedList 0 1 2 3 4 ……..
exchanged = false;
for (int i=0; i<=bottom; i++) {
P1 = sortedList[i];
P2 = sortedList[i+1]; P1 P2
if (P1.getName().compareTo(
P2.getName()) > 0){ sortedList 0 1 2 3 4 ……..
sortedList[i] = P2;
sortedList[i+1] = P1;
exchanged = true;
}
} Person Person
Ali Jamal
bottom--; Person Person
Ahmad
} Siti
Application
(Sorting [Analysis])
P1 P2
sortedList
sortedList 0 1 2 3 4 ……..
Person Person
AFTER SORTING
0 1 2 3 4 ……..
sortedList
The End
Q&A
59