Modify H33 program based on the following information:
1 Create three arrays based on the Faculty, Student, & GStudent classes (One for each of the three classes). The three arrays are all of size 2.
2 Each member of the arrays will reference an object of its type. You already have the data for one object. So, you need to create provide data (of your choice) for the additional object.
3 After all objects are instantiated, display the objects by using the getter function. Since there are three classes of objects, so each group should have its unique header and the adjacent groups are separated by blank line(s).
1. Arrays:
A Array is a data structure to be used to store large amount of “like” data.
B An array can be declared by using any of the existing data types, or any data classes – either library or user-defined.
C Array size must be constructed by using the new operator.
D Declaring & constructing array:
Syntax |
Example |
|
Declaration | <data type> [ ] <variable>; | int [ ] gas; |
Construction | <variable> = new <data type> [ size]; | gas = new int[100]; |
.1 Explanation of the above example:
.a Variable “gas” is declared as an array variable. Its size (100) is determined by using the new operator.
.b The two statement can be combined: int [ ] gas = new int [100];
E Range of index values for identifying array members: 0 through (size-1).
F Size of an array can be determined by using the length property:
.1 Example: int size = age.length; (age is the name of the array & length is its property)
G Exercise 3.3 (E33) – processing array data:
E33 program file |
import javax.swing.*;public class E33 {public static void main(String [] args){int [ ] numbers = new int [5];int i, sum=0;for (i=0; i<5; i++) {if ((i % 2) == 0) numbers[i] = i*10+5;
else numbers[i]=i*5+5; ; System.out.println(“Array member # “+(i+1)+” has “+ numbers[i]); sum = sum + numbers[i]; }; System.out.println(“The sum of all numbers is “+sum); } } |
The output |
Array member # 1 has 5Array member # 2 has 10Array member # 3 has 25Array member # 4 has 20Array member # 5 has 45The sum of all numbers is 105 |
H Explanation:
.1 Array “numbers” is an integer array of size 10.
.2 The “for” loop will load integer values into this array based on the subscript value (even or odd) & the total sum will be calculated.
2. Array of Objects:
A Arrays can be declared to reference objects instantiated from a class.
B Syntax, example, & explanation:
.1 className [ ] arrayName = new className[ size ];
.2 Example: People [ ] p = new People [ 3 ];
.3 Explanation: Array p is an array of size 3 based on the People class. Each member of p can reference a People object.
C Exercise 3.4 (E34) – A different program file with array for referencing objects:
“Student” class |
// Data class “Student”public class Student {private String id;private String name;public Student( ) { }public Student(String d, String n) { id=d; name=n;}public void getStudent( ) {System.out.println(“Student ID: “+ id);
System.out.println(“Student\’s Name: “+ name); } } |
E34 class |
// The program classpublic class e34 {public static void main(String [] ss) throws Exception{Student [] s = new Student[2];s[0] = new Student(“999”, “Bob Hope”);s[1] = new Student(“111”, “John Wayne”);for (int x=0; x<2; x++) s[x].getStudent();
} } |
Output: |
Student ID: 999Student’s Name: Bob HopeStudent ID: 111Student’s Name: John Wayne |
D Explanation:
.1 Array s (of size 2) has been declared based on the Student class.
.2 Each member of array s has been assigned to reference a Student object.
In the “for” loop, x is used as a control variable and its value will be changed to allow the expression “s[x].getStudent()” to reference a different Student object to call the getStudent() function.