辅导案例-CSE4IOO

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
CSE1/CSE4IOO Semester 3, 2019
Assignment – Part 2
Assessment: This Part 2 of the assignment is worth 15 % of the final mark for this subject.
Due Date: To be announced
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 In-
formation 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 pla-
giarism.
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 re-
quirements 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, the program read the data from the text file
RRShelter.txt.
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 exe-
cuted, 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
14.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
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468