Professional Documents
Culture Documents
Topic 4 (Concept of Classes) : 4.1 Data Members of Type Array 4.2 Array of Objects
Topic 4 (Concept of Classes) : 4.1 Data Members of Type Array 4.2 Array of Objects
Topic 4 (Concept of Classes) : 4.1 Data Members of Type Array 4.2 Array of Objects
TOPIC 4
(CONCEPT OF CLASSES)
4.1 Data members of type array
4.2 Array of objects
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
class Student
{ private String name;
private double [ ]quiz=new double[3]; // this is data members of type array
:
//default constructor
//mutator for name
//mutator for SP_ID
//mutator for sales
//write all the retriever
//printer method
}
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.)