Week 05: Homework 4
Learning Standard Class Design and Minimising Repetition
The Problem
-
Construct and test a Student class which stores the following data:
- Name
- Address
- Degree name
- Department
- Year Commenced
-
Write accessor/mutator –
get()/set(x)
– methods for each field. If you are using an IDE, find out (ask your tutor for help, if necessary), how these methods can be generated automatically. -
Override
toString()
method of the Student class to return “Name, Department, Year Commenced” information on the Student object. Do you understand how does this affect the result of printing an object state? -
Write overloaded constructors so that a student can be constructed in several different way:
Student() Student(name) Student(name, address) Student(name, address, department)
Think how to minimise code repetition in these constructors by calling other constructor using the
this(…)
syntax.Programmers should always strive to avoid coding the same instruction more than once. This principle is known as DRY – “Don’t Repeat Yourself!”
-
Write a test class,
StudentTest
, to test this class by creating an array of three students. Print out the students data as it’s done bytoString()
method (actually you don’t need to call this method; just put your object into a print statement and it gets called automatically!). -
Add a student ID field,
studentID
, and another static fieldnextID
for counting students. Write a private method to increment the last value. Modify every constructor to set the value ofstudentID
to the current value ofnextID
, and incrementnextID
each time a student object is constructed. Initialise thenextID
to a static constant,STUDENT_ID_BASE=901000
(static variables are initialised prior to the first constructor call). Finally, modifytoString()
method to print the student information including the student id. -
Optional.
- Modify the above program to use an
ArrayList<Student>
instead of an arrayStudent[]
. Why the new version is better? - Modify the Student class so that the student ID is assigned based on the year of commencement (to fit the ANU format where the first two digits are the last two digits of the commencement year, and the remaining five digits represent a unique number incremented by every new student enrolled on the same year; for example, the two IDs – 9207147 and 0207147 are both count 7,147th student enrolled correspondingly on 1992 and on 2002. You may assume that there was no enrolled students before the University was founded, in 1947).
Assessment
You will get up to two marks, if you present a solution to the Homework exercise during the next week lab, or submitted it by push you GitLab repository as explained in the Forking your GitLab repository exercise (part of the Lab 3).