Content Analysis
Object Oriented Design and Programming
Week 11
A B Emran Salahuddin, Anchal Shrestha, Chiatalia Samani (Sydney)
Hanspreet Kaur, Allan NG (Melbourne)
Kent Institute Australia Pty. Ltd.
ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051
Version 2 – 18th December 2015
1
SLIDE TITLE
Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning
2
2
Programming Logic and Design
Array of Objects
3
Arrays
We used an Array for the first assignment and have stored basic data types.
An array is a collection that can store items of the same data type (eg all int values or all Student marks)
Accessed using an index or subscript
Size set at creation
4
Arrays Revision
Arrays are groups of elements of the same data type
One name is used to refer to the entire group
Individual elements in the group have an index that begins at 0 in Java.
So, what do each of the following lines of code do?
int[] studentMarks;
studentMarks = new int[200];
studentMarks[0] = 85;
5
Array of Objects
An array of objects is created just like an array of primitive type data items in the following way. Student[] studentArray = new Student[7];
The above statement creates the array which can hold references to seven Student objects.
6
Students Array
The line on previous slide doesn't create the Student objects themselves.
They must be created separately using the constructor of the Student class.
The studentArray contains seven memory spaces in which the address of seven Student objects may be stored.
If we try to access the Student objects even before creating them, run time errors would occur. For instance, the following statement throws a NullPointerException during runtime which indicates that studentArray[0] isn't yet pointing to a Student object.
7
Students Array
The Student objects must be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.
studentArray[0] = new Student();
In this way, we create the other Student objects also.
8
Students Array
If each of the Student objects have to be created using a different constructor, we use a statement similar to the above several times.
However, in this particular case, we may use a for loop since all Student objects are created with the same default constructor.
for ( int i=0; i<studentArray.length; i++) { studentArray[i]=new Student(); }
9
Array of Objects
Consider these two classes
10
| Student |
| - studentNumber: String - name: String |
| + Student(String,String) + Student() + getName(): String + setName(newName: String): void + getStudentNumber(): String + setStudentNumber(newNumber: String): void + toString(): String |
| Course |
| - courseNumber: String - courseName: String - students: Student[ ] - maximumEnrolment: int - numEnrolled: int |
| + Course() + Course(courseName: String,courseNumber: String, maximumEnrolment:int) + getCourseName(): String + setCourseName(newName: String): void + getCourseNumber(): String + setCourseNumber(newNumber: String): void + addStudent(aStudent: Student): void + getStudentAt(index: int): Student + numberEnrolled(): int + displayAllStudents(): void + toString(): String |
1
1-n
Array of Objects
Course class is having array of type Students.
Create these two classes in eclipse.
For your reference, download student.java and course.java from moodle.
Some methods are already given. Try to understand them.
After creating both classes, download test.java and complete that class.
11
Spend rest of your class time on your assessment
12
kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051
13
13