Topic 4 (Concept of Classes) : 4.1 Data Members of Type Array 4.2 Array of Objects

You might also like

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

PART 1

TOPIC 4
(CONCEPT OF CLASSES)
4.1 Data members of type array
4.2 Array of objects

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 1


LESSON OUTCOME

At the end of this lessons, students should be able to:


1) Understand the needs of data members of type array
2) Do an implementation of program that contain data members of type array
3) Understand the needs of array of objects
4) Do an implementation of program that involve array of objects

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 2


4.1 DATA MEMBERS OF TYPE ARRAY
ARRAY IN JAVA

Definition of array : A fixed set of data that have the same data type.
Declaration of array in java:
int []num=new int[10];
Example of array application in java:
a) To input data: c) To display lowest value:
System.out.println(“Enter 10 integer numbers:”); int lowest=num[0] //or int lowest=999999;
for(int i=0;i<num.length;i++)
num[i]=input.nextInt(); for(int i=0;i<10;i++)
{ if(num[i]>high)
b) To display data: high=num[i];
System.out.println(“Data in the array:\n”); }
for(int i=0;i<num.length;i++) System.out.println(“The highest value is “+high);
System.out.print(num[i]+” “); NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL
SCIENCES
3
4.1 DATA MEMBERS OF TYPE ARRAY
THE NEEDS OF DATA MEMBERS OF TYPE ARRAY

Example of data members of type array:

class Student
{ private String name;
private double [ ]quiz=new double[3]; // this is data members of type array
:

Why we need data members of type array:


1) To simplify the coding.
2) To make the coding readable and easy to maintain

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 4


4.1 DATA MEMBERS OF TYPE ARRAY
EXAMPLE OF CODING

EXAMPLE 1 : INPUT VALUE IN MAIN METHOD

EXAMPLE 2 : INPUT VALUE IN CLASS

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 5


4.1 DATA MEMBERS OF TYPE ARRAY
EXERCISE 1

Given the class definition:


struct Salesperson
{ private String name;
private SP_ID;
private double [ ] sales=new double[4];
//represent sales for 1st Quarter, 2nd Quarter, 3rd Quarter and 4th Quarter

//default constructor
//mutator for name
//mutator for SP_ID
//mutator for sales
//write all the retriever
//printer method
}

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 6


4.1 DATA MEMBERS OF TYPE ARRAY
EXERCISE 1(CONT.)

Your task are:


1) Define the Salesperson class
2) Write a main method that will:
a) Declare and create an object named emp with data type Employee
b) Input a data for emp object(you can input either in main method or in the Employee class)
c) Display the average sales
d) Display the index of highest sales
e) Display the count of sales more than RM 4000

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 7


4.2 ARRAY OF OBJECTS
THE NEEDS OF ARRAY OF OBJECTS

Example of array of objects:


public static void main(String args[])
{ Student [ ] stud=new Student[20];//declare and create new object that represent 20 students
:
Why we need an array of objects:
1) To simplify the coding by avoiding the declaration and creation of many objects.
Example:
Student stud1,stud2,stud3,stud4…..,stud20;
stud1=new Student();
stud2=new Student();
stud3=new Student();
:
stud20=new Student();
NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 8
4.2 ARRAY OF OBJECTS
CODING EXAMPLE

EXAMPLE 3 : ARRAY OF OBJECTS

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 9


4.2 ARRAY OF OBJECTS
EXERCISE

Write the class definition for Rectangle class:

Class :Rectangle
Attribute : width and length
Behavior
a)public Rectangle(double width,double length){…}
b)public void setWidth(double width) {…}
c)public void setLength(double length) {…}
d)public double calcArea(){…}
e)public double getLength(){…}
f) public double getWidth(){…..}
g)public String toString(){…….}
NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 10
4.2 ARRAY OF OBJECTS
EXERCISE(Cont.)

Write a main application that will do the following:


a) Create 4 instance object of class using array of object
b) Input and store the objects data into an array
c) Display the average area of objects
d) Display the highest area of objects
e) Display the details of the lowest area of objects.

NOR ZALINA BINTI ISMAIL|FACULTY OF COMPUTER AND MATHEMATICAL SCIENCES 11

You might also like