ITECH1000/5000 Programming 1, Semester 3 2014
Assignment 2 – Development of a Simple Program Involving Multiple Classes
Due Date: see Course Description for full details
Please see the Course Description for further information related to extensions for assignments and Special Consideration.
Please read through the entire specification PRIOR to beginning work. The assignment should be completed in the stages mentioned below. The objectives of this assignment are for you to:
• Write your own classes from a specification
• Instantiate objects of these classes
• Perform required tasks by creating and manipulating objects in the code of one class through the public interfaces (methods) of the classes of the objects being manipulated.
The following files/links are available on Moodle:
• An electronic copy of this assignment specification sheet
• A sample program with similar features to the assignment requirements involving multiple classes • Starting (incomplete) code for four of the classes to be developed
Note: If you use any resources apart from the course material to complete your assignment you MUST provide an intext citation within your documentation and/or code, as well as providing a list of references in APA formatting. This includes the use of any websites, online forums, books, or text books. If you are unsure of how to do this please ask for help.
Your program should conform to the following constraints.
• Use a while-loop when the number of iterations is not known before loop execution.
• Use a for-loop when the number of iterations is known before loop execution.
• Indent your code correctly to aid readability and use a consistent curly bracket placement scheme.
• Use white space to make your code as readable as possible.
• Validation of input should be limited to ensuring that the user has entered values that are within correct bounds. You do not have to check that they have entered the correct data type.
• Comment your code appropriately.
ITECH 1000/5000 Programming 1 Assignment 2, Semester 3 2014 (201427)
Part A – Code Comprehension
A Sample program is provided that creates a list of shapes stored in an array. This program uses classes:
Shapes, Square, Rectangle and ShapesList. The main method is in the class: TestingShapesListClass. Conduct a careful examination of this code. Make sure you understand this code as it will help you with your own programming for this assignment. Using the uncommented sample code for classes: Shapes, Square, Rectangle and ShapesList provided, answer the following questions:
1. Draw a UML diagram of each of the Shapes, Rectangle and Square classes using the code that has been provided. Complete this using the examples that have been provide in the lecture slides.
2. Draw a UML diagram of the ShapesList class using the code that has been provided. Complete this using the examples that have been provide in the lecture slides.
3. Add appropriate comments, in your own words, into the file: ShapesList.java to explain all the methods. At a mimimum, explain the purpose of each method. For the more complex methods – reading input from a file, sorting the data and exporting data to the file – insert comments to explain the code.
4. Explain the line of code:
output = new BufferedWriter(new FileWriter(file)) ; in the ExportToFile method in ShapesList.java .
5. Briefly explain the code in mySort() in the ShapesList class. What attribute are the shapes being sorted by?
6. Name the sort algorithm that is being used. Explain in words how it works, using an example. Why do we need to use two for loops?
7. Briefly name and describe one other sorting algorithm that could have been used.
8. Explain the use of the return value in the getArea() method in the Rectangle class. What is it returning? Where does the value come from?
9. Examine the lines to open the output file and write the outputString in the ShapesList class:
File file = new File(“export.txt”);
System.out.println(file.getAbsolutePath()); output = new BufferedWriter(new FileWriter(file)); output.write(outputString);
Where is the output file located? Find BufferedWriter in the Java API documentation and describe the use of this object.
10. Briefly explain why a try and catch clause has been included in the code to read from the file. Remove this from the code and remove the input file and take note of the error. Re-add the line and investigate what happens if the input file is not present.
Part B Development of a basic Class
Your first coding task is to implement and test a class that represents a Person. A Person object has a firstName, lastName, address, phoneNumber, yearOfBirth, height, weight and sex.
To complete this task, you are required to:
1. Create a new package in Eclipse named assignTwoStudentNNNNNN where NNNNNN is your student number. You will author a number of classes for this assignment and they will all be in this package.
2. Use the UML diagram provided to implement the class Person in a file called Person.java. Starter code has been provided with this assignment, copy this starter code into your Person.java file. Complete the constructors that will create a Person object.
3. Author all mutator and accessor (set and get) methods and the toString() as indicated in the UML diagram.
4. Ensure that your Person class includes the following validation within the mutator (set) methods:
a. The lastName of the Person cannot be null or an empty String (ie “”). If an attempt is made to set the lastName to an invalid value the method should set the name to “Invalid Name” unless there is already a lastName, in which case it shouldn’t change. In either case the method should return false when attempting to set an invalid value.
b. The yearOfBirth of the Person must be greater than or equal to 1900. If an attempt is made to set the value of the yearOfBirth to an invalid value the method should return false and not change the value of the yearOfBirth.
– String firstName;
– String lastName;
– String address;
– String phoneNumber;
– int yearOfBirth;
– double height;
– double weight;
– char sex;
~ Person(firstName:String, lastName:String, address:String,
phoneNumber:String, yearOfBirth:int, height:double, weight:double, sex:char) create
~ Person() create
Figure 1: UML Diagram for the class Person
Part C Development of extended Classes using inheritance
Your second coding task is to implement and test a class that represents a Player. For this assignment a Player is a Person with additional attributes such as position, playerNumber, registrationNumber and feesOwed. A Player isA Person. The Player class will extend a Person class, so the firstName, lastName, address, phoneNumber, yearOfBirth, height, weight and sex will be inherited from the Person class. After you have created the Player class, you are asked to create a similar class for another type of Person that you choose (e.g. Team Manager, Coach, Scorer….).
To complete this task you are required to:
1. Use the UML diagram provided to implement the class Player in a file called Player.java. Ensure that you adhere to the naming used below as this class will be used by other classes that you will develop. Starter code is provided for you to copy.
2. Make sure that your Player class extends the Person class.
3. Ensure that your class includes the following validation within the mutator (set) methods:
a. playerNumber must be between 4 and 50. If an attempt is made to set the playerNumber to an invalid amount, then the method should set the playerNumber to three and return false, otherwise set theplayerNumber as given and return true.
b. feesOwed cannot be a negative number. If an attempt is made to set the value of the feesOwed to an invalid value the method should return false without changing the value.
4. Write additional methods for toString() and a method to check equality with another Player (equality should be based on playerNumber being the same for both Players).
5. Select two Players that you will create. Choose values for each of the data attributes for each Player. Write a class called TestTeamListClass (starter code is provided to copy from) and within the main method write code that will :
a. Instantiate an instance of the Player class for one of the Players you selected using the Player() constructor;
b. Set all of the instance variables to the appropriate values using the mutator methods (set);
c. Change the yearOfBirth to increase the yearOfBirth of the first Player by 2 years.
d. Instantiates an instance of the class Player for a second different Player contained in the table below using the Player(firstName:String, lastName:String, address:String, phoneNumber:String, yearOfBirth:int, height:double, weight:double, postion:String, playerNumber:int, registrationNumber:String, feesOwed:double) constructor
e. Display both of the Players that you have created using the toString() method
f. Display both of the Players that you have created using individual accessor (get) methods in a print statement
– position: String
– playerNumber: int
– registrationNumber: String
– feesOwed: double
~ Player(firstName:String, lastName:String, address:String, phoneNumber:String, yearOfBirth:int, height:double, weight:double, sex:char, postion:String, playerNumber:int, registrationNumber:String, feesOwed:double) create
~ Player() create
+ getPosition(): String
+ setPlayerNumber(_playerNumber:int): boolean
+ getPlayerNumber(): int
+ getRegistrationNumber(): String
+ setFeesOwed(_feesOwed: double): boolean
+ getFeesOwed() : double
+ comparePlayer(anotherPlayer: Player): boolean
Figure 2: UML Diagram for the class Player
6. Now create a second class representing another Person (e.g. Team Manager, Coach, Scorer etc.) in a similar fashion to the way you created the Player class. (The Player class is similar to the square class in the sample code provided. The square class extends the Shapes class. The Rectangle class also extends the Shapes class.
Your second Person type is similar to the Rectangle class. Your Person e.g. coach will extend the Person class.) a. Decide on your Person type and then choose attributes particular to that Person that will be instance variables
b. Draw a UML class diagram for your new class
c. Create your new class in a new file named appropriately e.g. Coach.java
d. Make sure your new class extends the Person class
e. Write get and set methods for your new class
f. Write a toString() method for your new class
g. Test your new class by writing code to create an object based on this class in your TestTeamListClass class
h. Test your object by modifying one of the instance variables using a set method then display the object
Part D Development of the TeamList class
Using the UML diagrams provided and the information below you are required to implement and test classes that represent a TeamList object. For this assignment a TeamList contains multiple Persons up to a specified capacity. (This may be modeled on the ShapesList class in the sample code. The ShapesList class contains multiple Shapes in an array. Your TeamList will in a similar way contain multiple Persons in an array.)
– name: String
– Persons: Person
– currSize: int
– maxPersons: int
~ TeamList (name: String, maxPersons: int) create
~ TeamList() create
+ setName(_name: String)
+ addPerson(newPerson: Person): boolean
+ exportToFile() //itech5000 students only
+ readFromFile() //itech5000 students only
Figure 3: UML Diagram for the class TeamList
1. You have been provided with a starting point in TeamList.java. Write get and set methods as well as an addPerson method.
2. Create a method for sorting the Persons in the TeamList (model this on the ShapesList mySort() method)
3. Update the class TestTeamListClass adding in code to:
a. Create an instance of the TeamList class
b. Add at least two Players created to the TeamList class using the addPerson() method
c. Add at least one other Person (e.g. Coach or Scorer (the one you have already created)) to the TeamList class
d. Display the details of the teamlist that you have created using the toString() method in the TeamList class – these should be accessed via the instance of the TeamList class that you have created
e. Display all of the Persons in the teamlist by looping through and accessing each Person in turn. Each Person should be accessed via the instance of the TeamList class you have created, then printed using the toString() method on that Person.
(Hint: Look at the TestingShapesListClass.java class for examples of similar code to the code you will need in your TestClass. In TestingShapesListClass, Shapes are added to a ShapesList object. In your code Persons are added to the TeamList object).
Part E Using the Classes – Implementation of Menu System
Include further test cases. Create a new class called TeamListSystem.java. This class is to be used to implement a basic menu system. You may find some of the code you have already written in the TestMemberListClass.java can be copied and used with some modification here. You may also find the menu code from assignment 1 helpful as an example of using a switch statement to process the menu options. Each menu item should be enacted by choosing the appropriate method on the TeamList object. The menu should include the following options:
1. Populate the TeamList
2. Display Persons in TeamList
3. Sort Persons data
4. Import data (ITECH5000 students only)
5. Export data (ITECH5000 students only
• Populate the TeamList – write a populate method that will insert data hardcoded in your method directly into each Person. You will use the set methods on your Player object and the set methods on your additional Person (Hint: copy this code from your TestMemberListClass.)
• Display Data – display the data of all Persons in the teamlist, using your toString() method on the TeamList object.
• Sort data should utilise some form of sorting algorithm to sort the Persons into ascending order of age. (Hint: look at the sort method in the ShapesList class. It sorts on area of the shape, you need to sort based on yearOfBirth of the Person.) For students enrolled in ITECH5000 ONLY:
• Import data – this menu should read in data from the file called SampleData.txt. You are required to add the additional data described above to this file.
• Export data – save the data to a new file ExportData.txt
The following criteria will be used when marking your assignment:
• successful completion of the required tasks
• quality of code that adheres to the programming standards for the course, including:
• comments and documentation
• code layout
• meaningful variable names
You are required to provide documentation, contained in an appropriate file, which includes:
• a front page – indicating your name, a statement of what has been completed and acknowledgement of the names of all people (including other students and people outside of the university) who have assisted you and details on what parts of the assignment that they have assisted you with
• a table of contents and page numbers
• answers to the questions from Part A
• list of references used (APA style); please specify if none have been used
• An appendix containing copies of your code and evidence of your testing Using the link provided in Moodle, please upload the following:
1. All of the classes created in a single zip file – SurnameStudentIdAssign2.zip
2. A copy of your report – surnameStudentIDAssign2.docx or surnameStudentIDAssign2.pdf
ITECH 1000 ITECH 5000
PART A – Code Comprehension /20 /20
PART B – Person class + Testing
PART C – Player and another Person Class + Testing
PART D – TeamList Class + Testing
PART E – Using the Classes – Implementation of Menu System /20 /40
Quality of code created
Presentation of documentation / report
Total /20 /160 /200
Appendix Figure 4. UML Class Diagram example for the TeamList system
[contact-form-7 id=”232″ title=”Contact form 1″]