159.272 Programming Paradigms
Assignment 1
Object-oriented Programming in Java
Total marks: 15 marks
Due: 8pm Friday 12 April 2019
Important: this is an individual assignment. You must not share your code with others,
or use other’s code (this will be plagiarism!). If we find two assignments that are copied
from each other, both assignments will receive Zero marks!
Submit your work on Stream by 8pm (Beijing time) Friday 12th April 2019. You must
submit your final work using the stream submission system before the deadline (see
stream for details).
There is a penalty for late submissions. The penalty is 10% deducted from the total
possible mark for every day delay in submission (one day late – 90%, two days – 80% etc).
You are expected to manage your source code, this includes making frequent backups.
“The cat ate my source code”is not a valid excuse for late or missing submissions.
Consider managing your code in a private repository (github, gitlab, bitbucket or similar).
Set you repository to private (this is essential here to avoid plagiarism), we reserve the
right to deduct marks from your submission if the repository is public.
Assignment 1 Objectives
Apply the concepts learnt in part 1 of this course, in particular:
- core OO concepts (inheritance, encapsulation, object identity and equality)
- explore and use an API from the standard library (such as java.io)
- exception handling
- unit testing
- building user interfaces with swing
Tasks
Work individually to create the following program in Java using Eclipse. Create an Eclipse project
assignment1-<yourid> , where <yourid> is replaced by your student id. Within this project, create a
package nz.ac.massey.cs159272.ass1.id<yourid>. In this package, create the following classes (as
explained in part 1-part 4).
Part 1 Domain Model [5 marks] - Create the following classes in your package:
i. Course, with properties for number and name (both String)
ii. Address, with properties for town (String), street (String), post code number
(int) and house number (int)
iii. Activity, with properties name and type (both String) (i.e., activities is for social
and sport activities that students enrolled in)
iv. Student, with properties for name, first name, id (all String), dob (for
“dateOfBirth”, of type java.util.Date), course (of type Course), activity (of type
Activity) and address (of type Address). - All properties should be implemented using getters and setters, those can be
generated from your IDE. - All classes should override equals(Object), and hashCode(), these methods must
adhere to the usual contract between equals and hashCode, all those can be
generated from your IDE. - All classes should override toString(), these methods to return the string information
from these classes (e.g., for class Student, this will return first name, ID and dob
(converted to string)). - Student should have a clone() method that is a combination of deep and shallow
clone: deep clone should be used for the address and dob, a shallow clone should be
used for course and activity.
Part 2 Persistency [4 marks] - Create a class StudentStorage with the following three static methods:
? void save(java.util.Collection<Student>,java.io.File file) throws
IOException – saves a list of students to a binary file and to CSV file,
depending on what file format a user choose (see part 4). Note that the
data of referenced objects (address and course) should be saved as well.
Please also read the hints at the end of the document!
void save(java.util.Collection<Student>,String fileName) throws
IOException – saves a list of students to a binary file file with a given
name.
No code should be replicated (copied & pasted) between the two
versions of save(..).
java.util.Collection<Student> load(java.io.File file) throws IOException
– reads student data from a binary file. - save(..)/load (..) should preserve referential integrity: for instance, if two instances of
Student share the same (==) course, then this should be preserved. I.e., when
loading these two instances from a file, they should reference the same Course
instance.
Part 3 Testing [3 marks] - write a class TestCloningStudents with JUnit test(s) to test the clone() method in
Student. - write a class TestPersistency with JUnit tests to test the save(..) and load (..) methods
in StudentStorage. In particular, write tests that“round-trip”data: create a list of
Student instances list1, save it, then load it from the file as list2, and compare list1
and list2. - the roundtrip test should test whether save(..) and load (..) preserve referential
integrity - write tests to check whether the exception declared by load(..) work as expected
when an invalid file name is used
Part 4 Graphical User Interface(GUI) [3 marks] - write a user interface StudentListEditor to edit lists of students, this is an executable
class (a class with a main method) that when executed opens a window - the user interface must show all instances of students represented in a file, and a
form that can be used to edit a selected instance - for instance, this user interface could look as follows:
Note : you don’t have to implement the publish or delete buttons ! - a table (using JTable) based“spreadsheet-like”user interface is also acceptable. The
save / load functions in the toolbar should save / load data to/from files that are
selected using file selection dialogs that pop up when the respective buttons are
pressed. You can use the class javax.swing.JFileChooser for this purpose. - Export the results to a file, you may choose a file format to save (.txt , .csv or .html).
Feel free to implement this in multiple file formats if you wish.
javax.swing.JFileChooser can be helpful here.
Part 5 Bonus question: [1 mark]
Create an executable java application studenteditor.jar with StudentListEditor as the
main class.
Hints - you can use code and ideas from tutorials for the first three parts
- for part 2, have a look at the following classes:
java.io.ObjectOutputStream
java.io.ObjectInputStream - plan how much time you want to invest to answer each question: 4 and 5 can take a lot of
time, but will only give you 3-4 marks, it might be better to focus on 1-3. - see http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html for instructions how
to create executable jars (for the bonus questions).
How to submit - export the Eclipse project to a file assign1-<yourid>.zip, where <yourid> is replaced by
your student id - check that your zip file contains all project files and java sources by unzipping the file
and inspecting its content before you submit , it is also recommended to re-import this
as a project into an Eclipse workspace to check this (in Eclipse, use File > Import >
General > Existing Projects into Workspace) -
if you decide to do the bonus question (part 5), also include studenteditor.jar in the
root (top) folder of this zip file
Upload this file to Stream. Once completed, make sure that you click Submit (don’t
just save your work as a draft!!).WX:codehelp