CSE1/CSE4IOO Semester 3, 2019Assignment – Part 2
Assessment: This Part 2 of the assignment is worth 15 % of the final mark for this subject.
Due Date: To
be3,
announced
Feb
2020
Delays caused by computer downtime cannot be accepted as a valid reason for a late submission
without penalty. Students must plan their work to allow for both scheduled and unscheduled
downtime. Penalties are applied to late assignments (accepted up to 5 days after the due date
only). See the university policy for details.
Individual Assignment: This is an individual assignment. You are not permitted to work as a
group when writing this assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner that
gives the impression that the work is your own. The Department of Computer Science and Information Technology treats academic misconduct seriously. When it is detected, penalties are
strictly imposed. Refer to the unit guide for further information and strategies you can use to
avoid a charge of academic misconduct. All submissions will be electronically checked for plagiarism.
Objectives: The general aims of this assignment are:
• To analyze a problem in an object-oriented manner, and then design and implement an
object-oriented solution that conforms to given specifications
• To practise using inheritance in Java
• To practise file input and output in Java
• To make implementations more robust through mechanisms such as exception handling.
Submission Details: Please follow your lecturer’s instructions.
Compiling and Execution Requirements: We should be able to compile your classes with
the simple command javac *.java, and execute your programs with a simple command, e.g.
java RRShelterMenu.
1
Two-Part Assignment
• This is part 2 of the tw0-part assignment
• When you complete part 2, you would have implemented a menu-driven program whose requirements are described below (which is a slight variation of what was described in part 1)
• Everything described for part 1 is applied for part 2, except where they are otherwise explicitly
stated.
For Part 2 of the assignment, complete the following tasks.
Task 1 – ArrayList
• Modify what you did in part 1 so that you will maintain the collection of animals kept in the
shelter as an ArrayList, instead of an array.
• Test your modifications with the test programs RRShelterPart2Tester1 Add and
RRShelterPart2Tester2 Release, in Appendices 1 and 2.
Your classes must be such that the test programs (for this and other tasks) can be run without
change.
Task 2 – Food List
• Add code to your classes so that you can display the food list for the animal in the shelter.
• Test your classes with the test program RRShelterPart2Tester3 Foods in Appendix 3.
As stated above, your classes must be such that the test program can be run without change.
Note: In the handout of part 1, the line showing food for a kangaroo does not start with the
animal tag. This is a mistake. It should be corrected to include the animal tag as shown in the
example below:
M001 Kangaroo: no extra feed
F002 Kangaroo: extra cut grass paddock 1
M003 Joey: milk supplement
F004 Joey: no extra feed
M005
apple
banana
M006
apple
banana
grapes
2
Task 3 – Saving and Reading Data
• Implement necessary methods so that you can write the animal data to a text file and read the
data from the text file.
The file name and the file format are exactly as described in part 1 handout.
• Test your classes with the test programs RRShelterPart2Tester4 WriteToFile and
RRShelterPart2Tester5 ReadFromFile in Appendices 4 and 5.
Again, your classes must be such that these test programs can be run without change.
Task 4 – Providing a Menu
Implement the class that presents the menu should have the following options:
******************
Recovery & Release
******************
1: Add a Kangaroo
2: Add a Joey
3: Add a Possum
A: Display the Animals
F: Display the Food List
R: Release an Animal
Q: Quit
Please select an option:
i. Before displaying the menu,
RRShelter.txt.
the program read the data from the text file
If the file does not exist or contains errors, an error message should be displayed and the
program terminates.
ii. Then the menu is repeatedly be displayed after each (case-insensitive) user selection is executed, until the user chooses ’Q’ or ’q’ to quit the program.
If a chosen option is invalid, the program displays an error message and returns to the main
menu.
If an exception is thrown in carrying out an option, the program displays an error message
and returns to the main menu. That is, the program must be robust.
Note that the whole program (which includes any classes used) must ensure that the data
maintained by the application are valid – as described in part 1 handout.
iii. Of course, for options 1, 2, 3 and R, the program must get the required data entered by the
user from the keyboard. For example, for option R, the user needs to enter the type of animal
to be released.
iv. After quiting the menu and before terminating, the collection should be written back to the
text file RRShelter.txt (in the overwriting mode).
3
Marking Scheme Overview
• 94 marks will be given to Tasks 1-4.
• 6 marks will be given to program design, coding style and readability.
Return of Assignments
Department policy requires that assignments are returned within 3 weeks of the submission date.
Students will be notified by email and via the CSE1 LMS forum when marking sheets are available
for collection.
4
Appendix 1
public class RRShelterPart2Tester1_Add
{
public static void main(String [] args) throws Exception
{
// Test add animals – valid cases
System.out.println(“Test 1:”);
test1();
// Inva;id cases
System.out.println(“\nTest 2”);
test2();
System.out.println(“\nTest 3”);
test3();
System.out.println(“\nTest 4”);
try{ test4();}
catch(Exception e){ System.out.println(e.getMessage());}
System.out.println(“\nTest 5”);
try{ test5();}
catch(Exception e){ System.out.println(e.getMessage());}
}
// Add animals – valid cases
public static void test1() throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addJoey(“M002”, ’S’, 1, 4.5);
shelter.addPossum(“M003”, ’S’, “apple|banana”, “U1”);
System.out.println(shelter);
}
public static void test2() throws Exception
// Add kangaroo – tag number is not new
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’M’, 2);
System.out.println(shelter);
try
{
shelter.addKangaroo(“M001”, ’M’, 2);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
5
public static void test3() throws Exception
// Add kangaroo – invalid tag
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
try
{
shelter.addKangaroo(“A001”, ’S’, 1);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
public static void test4() throws Exception
// Add joey – invalid weight
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
try
{
shelter.addJoey(“M001”, ’S’, 1, 2.5);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
public static void test5() throws Exception
// Add possum – invalid territory
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
try
{
shelter.addPossum(“M001”, ’S’, “apple|banana”, “X1”);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
6
}
/* Sample output:
Test 1:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Joey[tag: M002, stayTime: S, paddock: 1, weight: 4.5]
Possum[tag: M003, stayTime: S, diet: apple|banana, territory: U1]
Test 2
RR Shelter:
Kangaroo[tag: M001, stayTime: M, paddock: 2]
java.lang.Exception: Error: Tag number already exists!
RR Shelter:
Kangaroo[tag: M001, stayTime: M, paddock: 2]
Test 3
RR Shelter:
java.lang.Exception: Error: Tag number must start with ’F’ or ’M’!
RR Shelter:
Test 4
RR Shelter:
java.lang.Exception: Error: Joey’s weight must be between 3 and 8 kilograms!
RR Shelter:
Test 5
RR Shelter:
java.lang.Exception: Error: Territory code must start with ’U’ or ’B’!
RR Shelter:
*/
7
Appendix 2
public class RRShelterPart2Tester2_Release
{
public static void main(String [] args) throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addKangaroo(“M002”, ’M’, 1);
shelter.addKangaroo(“M003”, ’L’, 1);
shelter.addJoey(“M004”, ’S’, 1, 4.5);
shelter.addJoey(“M005”, ’M’, 1, 4.5);
shelter.addJoey(“M006”, ’L’, 1, 4.5);
shelter.addPossum(“M007”, ’S’, “apple|banana”, “U1”);
shelter.addPossum(“M008”, ’M’, “apple|banana”, “U1”);
shelter.addPossum(“M009”, ’L’, “apple|banana”, “U1”);
System.out.println(“\nTest1:\n” + shelter);
// release a kangagoo
shelter.releaseKangaroo();
System.out.println(“\nTest2:\n” + shelter);
// release a joey
shelter.releaseJoey();
System.out.println(“\nTest3:\n” + shelter);
// release a possum
shelter.releasePossum();
System.out.println(“\nTest4:\n” + shelter);
// release second possum
shelter.releasePossum();
System.out.println(“\nTest5:\n” + shelter);
// release third possum
shelter.releasePossum();
System.out.println(“\nTest6:\n” + shelter);
// try to release another possum
shelter.releasePossum();
System.out.println(“\nTest7:\n” + shelter);
}
}
/* Sample output:
Test1:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 1]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
8
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M001, stayTime: S, paddock: 1
Test2:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M004, stayTime: S, paddock: 1, weight: 4.5
Test3:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M007, stayTime: S, diet: apple|banana, territory: U1
Test4:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M008, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: M, diet: apple|banana, territory: U1]
Animal to release:
tag: M008, stayTime: S, diet: apple|banana, territory: U1
Test5:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M009, stayTime: S, diet: apple|banana, territory: U1]
Animal to release:
tag: M009, stayTime: S, diet: apple|banana, territory: U1
9
Test6:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
No such animal to be released!
Test7:
RR Shelter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
*/
10
Appendix 3
public class RRShelterPart2Tester3_Foods
{
public static void main(String [] args) throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addKangaroo(“M002”, ’M’, 1);
shelter.addKangaroo(“M003”, ’L’, 1);
shelter.addJoey(“F004”, ’S’, 1, 4.5);
shelter.addJoey(“F005”, ’M’, 1, 5.0);
shelter.addJoey(“F006”, ’L’, 1, 5.5);
shelter.addPossum(“M007”, ’S’, “apple|banana”, “U1”);
shelter.addPossum(“M008”, ’M’, “apple|banana”, “U1”);
shelter.addPossum(“M009”, ’L’, “apple|banana|grapes”, “U1”);
System.out.println(shelter);
System.out.println(“\nFood List:”);
shelter.displayFoodList();
}
}
/* Sample output:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 1]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: F004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: F005, stayTime: M, paddock: 1, weight: 5.0]
Joey[tag: F006, stayTime: L, paddock: 1, weight: 5.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana|grapes, territory: U1]
Food List:
M001 Kangaroo: no extra feed
M002 Kangaroo: no extra feed
M003 Kangaroo: extra cut grass paddock 1
F004 Joey: milk supplement
F005 Joey: milk supplement
F006 Joey: no extra feed
M007
apple
banana
M008
apple
banana
M009
apple
banana
grapes
*/
11
Appendix 4
ublic class RRShelterPart2Tester4_WriteToFile
{
public static void main(String [] args) throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addKangaroo(“M002”, ’M’, 2);
shelter.addKangaroo(“M003”, ’L’, 1);
shelter.addJoey(“M004”, ’S’, 1, 4.5);
shelter.addJoey(“M005”, ’M’, 1, 4.5);
shelter.addJoey(“M006”, ’L’, 1, 4.5);
shelter.addPossum(“M007”, ’S’, “apple|banana”, “U1”);
shelter.addPossum(“M008”, ’M’, “apple|banana”, “U1”);
shelter.addPossum(“M009”, ’L’, “apple|banana|grapes”, “U2”);
System.out.println(shelter);
shelter.writeToFile();
}
}
/* Sample output on screen:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 2]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana|grapes, territory: U2]
*/
/* Output file:
Kangaroo
M001
S
1
Kangaroo
M002
M
2
Kangaroo
M003
L
1
Joey
M004
S
1
4.5
Joey
M005
M
12
1
4.5
Joey
M006
L
1
4.5
Possum
M007
S
apple|banana
U1
Possum
M008
M
apple|banana
U1
Possum
M009
L
apple|banana|grapes
U2
*/
13
Appendix 5
public class RRShelterPart2Tester5_ReadFromFile
{
public static void main(String [] args) throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addKangaroo(“M002”, ’M’, 2);
shelter.addKangaroo(“M003”, ’L’, 1);
shelter.addJoey(“M004”, ’S’, 1, 4.5);
shelter.addJoey(“M005”, ’M’, 1, 4.5);
shelter.addJoey(“M006”, ’L’, 1, 4.5);
shelter.addPossum(“M007”, ’S’, “apple|banana”, “U1”);
shelter.addPossum(“M008”, ’M’, “apple|banana”, “U1”);
shelter.addPossum(“M009”, ’L’, “apple|banana|grapes”, “U2”);
System.out.println(shelter);
shelter.writeToFile();
RRShelter shelter2 = RRShelter.readFromFile();
System.out.println(“\nRetrieved Data:\n” + shelter2);
}
}
/* Sample output:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 2]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana|grapes, territory: U2]
Retrieved Data:
RR Shelter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 2]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana|grapes, territory: U2]
*/
14
CSE1/CSE4IOO Semester 3, 2019
Assignment – Part 1
Assessment: This Part 1 of the assignment is worth 15 % of the final mark for this
subject.
Due Date: To
announced
13beJan,
2020
Delays caused by computer downtime cannot be accepted as a valid reason for a late
submission without penalty. Students must plan their work to allow for both scheduled
and unscheduled downtime. Penalties are applied to late assignments (accepted up to 5
days after the due date only). See the university policy for details.
Individual Assignment: This is an individual assignment. You are not permitted to
work as a group when writing this assignment.
Copying, Plagiarism: Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. The Department of Computer
Science and Information Technology treats academic misconduct seriously. When it is
detected, penalties are strictly imposed. Refer to the unit guide for further information
and strategies you can use to avoid a charge of academic misconduct. All submissions
will be electronically checked for plagiarism.
Objectives: The general aims of this assignment are:
• To analyze a problem in an object-oriented manner, and then design and implement
an object-oriented solution that conforms to given specifications
• To practise using inheritance in Java
• To practise file input and output in Java
• To make implementations more robust through mechanisms such as exception handling.
Submission Details: Please follow your lecturer’s instructions.
Compiling and Execution Requirements: We should be able to compile your
classes with the simple command javac *.java, and execute your programs with a simple command, e.g. java KangarooTester.
1
Two-Part Assignment
• This assignment consists of two parts: Part 1 and Part 2.
• When you complete both parts, you would implement a menu-driven program whose
requirements are described below.
• For part 1, you only need to complete a number of tasks specified later in this handout.
Requirements Description
Several organisations across Australia are dedicated to caring for injured wildlife. The
general aims of such organisations are to return healthy animals to their natural environment. One such organisation is the Recovery and Release Shelter (R&R Shelter). They
monitor animals that are close to being ready for release and determine when animals in
their care will be released.
The shelter has employed you to implement a small interactive application to manage
their operations.
The application stores all information about each animal in a text file that must be loaded
when your program starts. The shelter looks after Kangaroos, Joeys and Possums. The
shelter has paddocks for the Kangaroos and Joeys and the Possums are kept in cages.
Information on Kangaroos is stored in 4 lines. A typical entry (record) is:
Kangaroo
M3425
M
1
• Line 1 is the type of animal (Kangaroo, Joey or Possum) – not mutable
• Line 2 is a unique electronic tag for the animal – not mutable. All tags begin with
M or F (indicating the sex of the animal) followed by one or more digits.
• Line 3 indicates how much longer the animal will stay at the R&R Shelter before
release. Possible values are:
S – short-term
M – medium-term
L – long-term
• Line 4 is the paddock number in which the animal has been placed (1 or 2) – not
mutable
2
Information on Joeys (kangaroos not yet adult-sized) is stored in 5 lines. A typical record
is:
Joey
F5432
L
2
4.55
• Line 1 is the type of animal – not mutable
• Line 2 is the unique electronic tag for the animal – not mutable
• Line 3 indicates how much longer the animal will stay in the R&R Shelter before
release
• Line 4 is the paddock number in which the animal has been placed (1 or 2) – not
mutable
• Line 5 is the weight of the joey in kg (kangaroos with weights less than or equal
to 8kg are classified as joeys by the shelter). All joeys are without their mothers.
Those that are large enough are to be cared for in a paddock. Joeys with weights
less than 3kg are taken care of at a nearby shelter.
Information on Possums is stored in 5 lines. A typical record is:
Possum
M3322
M
apples|bananas|grapes
U7
• Line 1 is the type of animal – not mutable
• Line 2 is the unique electronic tag for the animal – not mutable
• Line 3 indicates how much longer the animal will remain in the R&R Shelter before
release
• Line 4 is a list of foods that form the possum’s diet – not mutable. The foods are
listed in a string, separated by the vertical bar character (|).
• Line 5 indicates the home territory of the possum (the letter U or B for urban or
bush, followed by a single digit from 0 to 9) – not mutable. All possums are returned
to their home territory.
***
The required application is called RRShelterMenu, which will be implemented in Part
2. It is a menu-driven program to maintain the collection of animals at the R&R Shelter.
3
Loading data
The program must first read the text file RRShelter.txt, which contains the animal
records, and load that information into the program. If the text file does not exist, a
warning message should be displayed to screen and the program terminates. Otherwise,
the program continues by displaying the menu options.
Main menu
The menu should have the following options:
******************
Recovery & Release
******************
A) Add Animal Submenu
S) Show Animals
F) Food Lists
E) Empty Nest
Q) Quit
******************
Please select:
This menu should repeatedly be displayed after each (case-insensitive) user selection is
executed, until the user chooses ’Q’ or ’q’ to quit the program.
Saving data
Before terminating, the collection should be written back to the text file that was used for
input (using the same format).
Option A – Add Animal Submenu
This menu option takes the user to a submenu with the following options.
**********************
Add Animal Submenu
**********************
K) Add Kangaroo
J) Add Joey
P) Add Possum
**********************
Please select:
The user can choose one of the three options, each for adding a particular type of animal.
For each option, the program prompts the user for relevant information. And once the
option is finished, the program returns to the main menu.
4
Option S – Show Animals
This option shows the details of all the animals currently in the shelter. It must show all
the attribute names and values for each animal.
Option F – Food Lists
The Food Lists menu option displays to screen the food needs of all the animals. The
information displayed depends on the type of the animal.
• For each Kangaroo, one of the following three lines is output:
Kangaroo: no extra needs
Kangaroo: extra cut grass paddock 1
Kangaroo: extra cut grass paddock 2
If a Kangaroo has a long-term (L) stay value then extra cut grass is placed in that
Kangaroo’s paddock (and the output line indicates the paddock number). Otherwise
the first line is output.
• For each Joey, one of the following two lines is output:
M1234 Joey: no extra needs
F5643 Joey: milk supplement
If a Joey’s weight is 5kg or less, a milk supplement is given. Otherwise the first line
is output.
• For each Possum, the output starts with the Possum’s tag and then each food in the
Possum’s food list is written 1 per line, indented 2 spaces. For example:
M3322
apples
bananas
grapes
Option E – Empty Nest
• Animals are released (and deleted from the collection) one at a time.
• When conditions are right, one animal of a particular type with a short-term (S) stay
value (if it exists) is released back into its natural environment (and deleted from the
collection).
• For this option E, the program prompts the user for the type of animal to release.
• Regardless of whether or not an animal was actually released or not, at the same time
all animals of the same type with a medium-term (M) stay value (if exist) have their
stay value changed to S, and all animals of the same type with a long-term (L) stay
value (if exist) have their stay value changed to M.
5
Exception Handling
1. For various types of information, the program should check that the data value is valid,
as shown in the table below:
Characteristics
type of Animal
tag
stay time
paddock
weight of Joey
food list
home territory
Valid Values
Kangaroo, Joey, Possum
M or F followed by 1 or more digits (each digit is from 0-9
inclusive)
S, M or L
1 or 2
double between 3 and 8 inclusive
any string, possibly with substrings separated by ‘|’ character
U or B followed by 1 digit (from 0-9 inclusive)
2. An exception should be thrown for any error encountered in the input text file.
3. When an exception is thrown for a menu option (from either the main menu or the
submenu), the program must display the error message and then return to the main
menu.
4. If the user chooses an invalid menu option (from the main menu or the submenu), the
program displays an error message and returns to the main menu.
***
For Part 1 of the assignment, complete the following tasks.
Task 1
Design an inheritance hierarchy for the animals in the Rescue and Release Shelter. Draw
a diagram to show the inheritance hierarchy. For simplicity, include the attributes only.
Task 2
• Implement class Kangaroo (and of course, its super class).
• Run the test program KangarooTester in Appendix A. Your Kangaroo class must be
such that the test program can be run without changes.
• Note: You do not need to implement methods relating to file input/output. These requirements will be done in Part 2 of the assignment.
6
Task 3
• Implement class Joey.
• Run the test program JoeyTester in Appendix B. Your Joey class must be such that
the test program can be run without changes.
Task 4
• Implement class Possum.
• Run the test program PossumTester in Appendix C. Your Possum class must be such
that the test program can be run without changes.
Task 5
• Implement class RRShelter, which maintains the collection of animals in the shelter.
• The collection is maintained as an array. Assume that the maximum number of animals
is 100.
• This class must have at least methods to add and release animals.
• Run test programs RRShelterTestAddAnimal and RRShelterTestReleaseAnimal
in Appendices D and E, respectively. Your RRShelter must be such that these test
programs can be run without changes.
***
Marking Scheme Overview
• 94 marks will be given to Tasks 1-5.
• 6 marks will be given to program design, coding style and readability.
Return of Assignments
Department policy requires that assignments are returned within 3 weeks of the submission date. Students will be notified by email and via the CSE1/CSE4IOO LMS forum
when marking sheets are available for collection.
7
Appendix A
public class KangarooTester
{
public static void main(String [] args) throws Exception
{
// valid case
test1();
// invalid cases
try{ test2();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test3();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test4();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test5();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test6();}
catch(Exception e){ System.out.println(e.getMessage());}
}
public static void test1() throws Exception
{
String tag = “M1234”;
char stayTime = ’S’;
int paddock = 1;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
public static void test2() throws Exception
// Invalid tag length
{
String tag = “M”;
char stayTime = ’S’;
int paddock = 1;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
8
public static void test3() throws Exception
// Invalid first character
{
String tag = “A1234”;
char stayTime = ’S’;
int paddock = 1;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
public static void test4() throws Exception
// Invalid characters after the first character
{
String tag = “M12X4”;
char stayTime = ’S’;
int paddock = 1;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
public static void test5() throws Exception
// Invalid code for stay time
{
String tag = “M1234”;
char stayTime = ’A’;
int paddock = 1;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
public static void test6() throws Exception
// Invalid paddock number
{
String tag = “M1234”;
char stayTime = ’S’;
int paddock = 3;
Kangaroo kan = new Kangaroo(tag, stayTime, paddock);
System.out.println(kan);
}
}
/* Sample output:
Kangaroo[tag: M1234, stayTime: S, paddock: 1]
Error: Tag number must have at least two characters!
Error: Tag number must start with ’F’ or ’M’!
Error: Tag must contain digits only after the first character!
Error: Code for stay time must be ’S’, ’M’ or ’L’!
*/
9
Appendix B
public class JoeyTester
{
public static void main(String [] args) throws Exception
{
// valid case
test1();
// invalid cases
try{ test2();}
catch(Exception e){ System.out.println(e.getMessage());}
}
public static void test1() throws Exception
{
String tag = “M1234”;
char stayTime = ’S’;
int paddock = 1;
double weight = 5.250;
Joey joey = new Joey(tag, stayTime, paddock, weight);
System.out.println(joey);
}
public static void test2() throws Exception
// Invalid weight
{
String tag = “M1234”;
char stayTime = ’S’;
int paddock = 1;
double weight = 1.500;
Joey joey = new Joey(tag, stayTime, paddock, weight);
System.out.println(joey);
}
}
/* Sample output:
Joey[tag: M1234, stayTime: S, paddock: 1, weight: 5.25]
Error: Joey’s weight must be between 3 and 8 kilograms!
*/
10
Appendix C
public class PossumTester
{
public static void main(String [] args) throws Exception
{
// valid case
test1();
// invalid cases
try{ test2();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test3();}
catch(Exception e){ System.out.println(e.getMessage());}
try{ test4();}
catch(Exception e){ System.out.println(e.getMessage());}
}
public static void test1() throws Exception
{
String tag = “M1234”;
char stayTime = ’S’;
String diet = “apples|banana”;
String territory = “U1”;
Possum possum = new Possum(tag, stayTime, diet, territory);
System.out.println(possum);
}
public static void test2() throws Exception
// invalid length for territoty code
{
String tag = “M1234”;
char stayTime = ’S’;
String diet = “apples|banana”;
String territory = “U12”;
Possum possum = new Possum(tag, stayTime, diet, territory);
System.out.println(possum);
}
public static void test3() throws Exception
// invalid first character for territoty code
{
String tag = “M1234”;
char stayTime = ’S’;
String diet = “apples|banana”;
String territory = “A1”;
Possum possum = new Possum(tag, stayTime, diet, territory);
11
System.out.println(possum);
}
public static void test4() throws Exception
// invalid second character for territoty code
{
String tag = “M1234”;
char stayTime = ’S’;
String diet = “apples|banana”;
String territory = “UB”;
Possum possum = new Possum(tag, stayTime, diet, territory);
System.out.println(possum);
}
}
/* Sample output:
Possum[tag: M1234, stayTime: S, diet: apples|banana, territory: U1]
Error: Territory code must have exactly two characters!
Error: Territory code must start with ’U’ or ’B’!
Error: Second character of territory code must be a digit!
*/
12
Appendix D
public class RRShelterTestAddAnimal
{
public static void main(String [] args) throws Exception
{
// add animal valid case
test1();
// add animal invalid cases
// some quick tests
System.out.println(“\nTest 2”);
test2();
System.out.println(“\nTest 3”);
test3();
System.out.println(“\nTest 4”);
try{ test4();}
catch(Exception e){ System.out.println(e.getMessage());}
System.out.println(“\nTest 5”);
try{ test5();}
catch(Exception e){ System.out.println(e.getMessage());}
}
public static void test1() throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addJoey(“M002”, ’S’, 1, 4.5);
shelter.addPossum(“M003”, ’S’, “apple|banana”, “U1”);
System.out.println(shelter);
}
public static void test2() throws Exception
// Add kangaroo – tag number is not new
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
System.out.println(shelter);
try
{
shelter.addKangaroo(“M001”, ’M’, 2);
}
catch(Exception e)
{
System.out.println(e);
13
}
finally
{
System.out.println(shelter);
}
}
public static void test3() throws Exception
// Add kangaroo – invalid tag
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
try
{
shelter.addKangaroo(“A001”, ’S’, 1);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
public static void test4() throws Exception
// Add joey – invalid weight
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
try
{
shelter.addJoey(“M001”, ’S’, 1, 2.5);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
public static void test5() throws Exception
// Add possum – invalid territory
{
RRShelter shelter = new RRShelter();
System.out.println(shelter);
14
try
{
shelter.addPossum(“M001”, ’S’, “apple|banana”, “X1”);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
System.out.println(shelter);
}
}
}
/* Sample output:
RR Sheter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Joey[tag: M002, stayTime: S, paddock: 1, weight: 4.5]
Possum[tag: M003, stayTime: S, diet: apple|banana, territory: U1]
Test 2
RR Sheter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
java.lang.Exception: Error: Tag number already exists!
RR Sheter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Test 3
RR Sheter:
java.lang.Exception: Error: Tag number must start with ’F’ or ’M’!
RR Sheter:
Test 4
RR Sheter:
java.lang.Exception: Error: Joey’s weight must be between 3 and 8
kilograms!
RR Sheter:
15
Test 5
RR Sheter:
java.lang.Exception: Error: Territory code must start with ’U’ or ’B’!
RR Sheter:
*/
16
Appendix E
public class RRShelterTestReleaseAnimal
{
public static void main(String [] args) throws Exception
{
test1();
// there are no invalid cases
// note that release method is rather artificial. What if we
// want to release several joeys at the same time?
}
public static void test1() throws Exception
{
RRShelter shelter = new RRShelter();
shelter.addKangaroo(“M001”, ’S’, 1);
shelter.addKangaroo(“M002”, ’M’, 1);
shelter.addKangaroo(“M003”, ’L’, 1);
shelter.addJoey(“M004”, ’S’, 1, 4.5);
shelter.addJoey(“M005”, ’M’, 1, 4.5);
shelter.addJoey(“M006”, ’L’, 1, 4.5);
shelter.addPossum(“M007”, ’S’, “apple|banana”, “U1”);
shelter.addPossum(“M008”, ’M’, “apple|banana”, “U1”);
shelter.addPossum(“M009”, ’L’, “apple|banana”, “U1”);
System.out.println(shelter);
// release a kangagoo
shelter.releaseKangaroo();
System.out.println(shelter);
// release a joey
shelter.releaseJoey();
System.out.println(shelter);
// release a possum
shelter.releasePossum();
System.out.println(shelter);
// release second possum
shelter.releasePossum();
System.out.println(shelter);
// release third possum
shelter.releasePossum();
System.out.println(shelter);
// try to release another possum
shelter.releasePossum();
System.out.println(shelter);
17
}
}
/* Sample output:
RR Sheter:
Kangaroo[tag: M001, stayTime: S, paddock: 1]
Kangaroo[tag: M002, stayTime: M, paddock: 1]
Kangaroo[tag: M003, stayTime: L, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M001, stayTime: S, paddock: 1
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M004, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M005, stayTime: M, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: L, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M004, stayTime: S, paddock: 1, weight: 4.5
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M007, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M008, stayTime: M, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: L, diet: apple|banana, territory: U1]
Animal to release:
tag: M007, stayTime: S, diet: apple|banana, territory: U1
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M008, stayTime: S, diet: apple|banana, territory: U1]
Possum[tag: M009, stayTime: M, diet: apple|banana, territory: U1]
18
Animal to release:
tag: M008, stayTime: S, diet: apple|banana, territory: U1
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
Possum[tag: M009, stayTime: S, diet: apple|banana, territory: U1]
Animal to release:
tag: M009, stayTime: S, diet: apple|banana, territory: U1
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
No such animal to be released!
RR Sheter:
Kangaroo[tag: M002, stayTime: S, paddock: 1]
Kangaroo[tag: M003, stayTime: M, paddock: 1]
Joey[tag: M005, stayTime: S, paddock: 1, weight: 4.5]
Joey[tag: M006, stayTime: M, paddock: 1, weight: 4.5]
*/
19