Introductory Programming In Java
| HW 1 |
HW 2 |
HW 3 |
HW 4 |
HW 5 |
HW 6 |
HW 7 |
Homework 4
Defining your own class and testing it
The problem
Construct and test a Student class which stores the following data:
- Name
- Address
- Degree name
- Department
- Year Commenced
Write accessor/mutator methods for each field
Override toString() method of the Student class to return "Name, Department, Year Commenced" information on the Student object
Write overloaded constructors so that a student can be constructed in several different way:
Student() Student(name) Student(name, address) Student(name, address, department)
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 by toString() method (the last method needs not to be called — fancy that!).
Add a student ID field, studentID, and another static field nextID for counting students. Write a private method to increment the last value. Modify every constructor to set the value of studentID to the current value of nextID, and increment nextID each time a student object is constructed. Initialise the nextID to a static constant, STUDENT_ID_BASE=901000 (static variables are initialised prior to the first constructor call). Finally, modify toString() method to print the student information including the student id.
Optional.
- Modify the above program to use ArrayList<Student> instead of array of Students. 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.
| HW 1 |
HW 2 |
HW 3 |
HW 4 |
HW 5 |
HW 6 |
HW 7 |
