Sunday, October 4, 2009

To analyse a problem in an object-oriented manner, and then design and implement an object- oriented solution that conforms to given specifications

PLEASE NOTE: You are NOT permitted to use container classes from the API such as
ArrayList and LinkedList in this assignment.

Problem Description
Several organisations across Australia are dedicated to caring for injured wildlife or implementing captive breeding programs for endangered species. The general aims of such organisations are to return healthy animals to their natural environment. Rebecca runs a Recovery and Release Centre that monitors animals that are close to being ready for release and determines when animals in her care will be released. Rebecca has employed you to implement a small interactive information system to manage parts of her Recovery and Release (R&R) Centre.

Rebecca stores all information about each animal in a text file that must be loaded when your program starts. Rebecca’s R and R Program looks after Kangaroos, Joeys, Quolls and Possums. She has paddocks for the Kangaroos and Joeys and the Quolls and 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, Possum or Quoll) – 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 4 digits.
• Line 3 indicates how much longer the animal will stay at the R&R Centre 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



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 Centre 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 centre). All joeys are without their mother and are large enough to be cared for in a paddock. Joeys with weights less than 3kg are taken care of at a nearby centre.



Information on Quolls is stored in 5 lines. A typical record is:

Quoll
F1122
S beef|chicken Dav

• 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 Centre before release
• Line 4 is a list of foods that form the quoll’s diet (each food is separated by a ‘|’ character) – not mutable
• Line 5 is the carer of the quoll – not mutable. The carer is responsible for giving the quoll a varied diet from its list of foods. Possible carers are Dav, Josh and Jackie.
 
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 Centre before release
• Line 4 is a list of foods that form the possum’s diet – not mutable
• 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.

Task 1 – Animal Hierarchy
Design and implement a class hierarchy to represent the animals at the R&R Centre. As you complete the following tasks which require certain behaviours (methods) of your classes, consider whether any methods or classes should be abstract.

Task 2 – Driver Program which Loads from and Saves to a Text File
Write a menu-based driver program to manipulate the collection of animals at the R&R Centre (and begin coding whatever classes are need for that collection). See the end of this assignment for skeleton code for the menu-based driver program (Driver.java) and an array-based collection of Animal objects (RandRCollection.java) that you may wish to use directly or adapt to your solution.

The program must first ask the user for the name of the text file containing the animal records and load that information into the program. If the file does not exist, a warning message should be displayed to screen before the program continues (with an empty animal collection) by displaying the menu options.

The menu should have the following options (you may display the menu in whatever format you wish and simply output a message for options that are not yet implemented.)

****************** Recovery & Release
****************** S) Show Submenu
A) Add Animal F) Food Lists E) Empty Nest R) R&R
X) BYTE

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.

Before terminating, the collection should be written back to the text file that was used for input (using the same format). If no file was used for input, ask the user for the name of the file in which to save the collection. If the user enters a file name that already exists, repeatedly prompt for a file name until the user enters the name of a file that does not already exist.
 
Task 3 – Show Submenu
The ShowSubmenu menu option takes the user to a submenu with the following options, repeatedly displayed until the user enters m.
• Options 1) and 2) result in all details of all male Kangaroos and male Joeys in the particular paddock being displayed to screen, followed by a total of those male Kangaroos and male Joeys in the particular paddock. This information helps Rebecca allocate new roos to paddocks.
• Option a) results in all details of all animals displayed to screen.

********************** Show Submenu
**********************
1) Male Roos Paddock 1
2) Male Roos Paddock 2 a) Show All Animals

m) Return to Main Menu
********************** Please select:

Task 4 – Add Animal
The Add Animal menu option prompts the user for the type of animal to add, and then, depending on the animal, prompts for appropriate values for the attributes needed for the animal and adds the animal to Rebecca’s collection.

Task 5 – 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 Quoll, the output starts with the carer’s name followed by the Quoll’s tag and then each food in the Quoll’s food list is written 1 per line, indented 1 space. For example:
Dav F1122 beef chicken

• 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 1 space. For example:
M3322 apples bananas grapes
 
Task 6 – Empty Nest
Animals are released (and deleted from Rebecca’s 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). Whether an animal was deleted or not, at the same time one animal of the same type with a medium-term (M) stay value (if it exists) has its stay value changed to S and one animal of the same type with a long-term (L) stay value (if it exists) has its stay value changed to M.

For the Empty Nest menu option E) prompt the user for the type of animal to release and if appropriate animals are in the collection, delete one animal of that type (stay value S), and change the stay value from M to S for one animal of that type, and change the stay value from L to M for one animal of that type.

Task 7 – Bunyips and their Billabong Parties
Only a select group of carers is aware of one secret service at Rebecca’s Recovery and Release Centre. On misty nights around the billabongs and other local waterholes, bunyips do gather to party and their dance moves are impressive. When enthusiasm overcomes ability and an injury occurs, Rebecca often receives a call for help!

 
Information on Bunyips is stored in 4 lines. A typical entry (record) is:

Bunyip
M3425
S
Bill

• Line 1 is the type of animal (Bunyip) – not mutable
 
Bunyips are too shy to have a picture taken :-(
 
• 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 4 digits.
• Line 3 indicates how much longer the animal will stay at the R&R Centre before release.
Possible values are:
S – short-term
M – medium-term
L – long-term
• Line 4 is the name of the Bunyip – not mutable. The Bunyips are shy and the carers call their names at night to coax them out of the billabong at the R&R Centre to take care of them.

Add Bunyips to your Animal hierarchy and make whatever changes are needed to your code to:
• handle Bunyips with other animals in the text files that are initially loaded into the collection and saved when the program ends
• have Bunyip details displayed with other animals at the Show All Animals submenu option
• allow adding a Bunyip at the Add Animal option
• for each Bunyip in the Food Lists option, output the line:
XXXX: no extra needs

Task 8 – Empty Nest (for Bunyips)
When the night is clear and cold, conditions are right to release Bunyips safely. One Bunyip ready for release (stay value S) is written to a binary file called XXXX and deleted from Rebecca’s collection only if the file does NOT already exist.

If a Bunyip is ready for release, check if the file XXXX exists and if it does, print a message to screen and do NOT delete the Bunyip (because we already have a Bunyip in the file waiting for the
 
Bunyip Team – see Task 9). Whether a Bunyip was deleted or not, at the same time one Bunyip with a medium-term (M) stay value (if it exists) has its stay value changed to S, and one Bunyip with a long-term (L) stay value (if it exists) has its stay value changed to M.

Add code to your Empty Nest menu option to handle Bunyips as required.

Task 9 – BYTE (BunYip TEam to release Bunyips)
Menu option X is used by the BunYip TEam (BYTE) to release one healthy Bunyip. If the file
XXXX does not exist, display an appropriate message to screen.

Otherwise, if the file XXXX exists, read in the Bunyip object and display to screen the tag and name of the Bunyip to call from the billabong for release and then delete the file XXXX.

Task 10 – InvalidAnimalException
Write an InvalidAnimalException class that extends the Exception class. It should have constructors that take a single argument and that take no arguments. Both constructors call the appropriate superclass constructor from the Exception class.

For the Add Animal menu option A), use exception handling to terminate the interactive input of an Animal (and return to the menu) if an incorrect string is entered for the Animal type. Valid values are:
Kangaroo, Joey, Quoll, Possum and Bunyip
------------------------------------------------------------------------------------------------------------------------

Tasks11 –

Robustness
a) Make menu option A) robust by checking that all user input is valid (using exceptions or coding appropriate checks). When an invalid value is input for any attribute value of any Animal, terminate the input of that Animal (and return to the main menu). Valid values for attributes are summarised below.

Characterisitcs Valid Values
type of Animal Kangaroo, Joey, Quoll, Possum or Bunyip
tag M or F followed by 4 digits (each digit is from 0-9 inclusive)
stay or release time S, M or L
paddock 1 or 2
weight of Joey double between 3 and 8 inclusive
food list any strings separated by ‘|’ character
carer Dav, Josh or Jackie
home territory U or B followed by 1 digit (from 0-9 inclusive)

b) Check for incorrect values in the input text file and read over any invalid record.

Rest and Relaxation
c) For menu option R) design a board game that requires traversing a 2-dimensional array
(board) starting in the top left hand corner of the board and proceeding left to right along each row,
 
until the bottom right-hand corner is reached. Design some elements (squares on the board) that have positive effects on the journey by causing further forward movement and some that cause the player to move backwards. As an initial attempt, you may decide to have just one player and count the number of moves taken to reach the end. You may have a fixed board size and you are free to impose whatever rule restrictions you wish.

Display the board, player(s), and the lucky and unlucky squares to screen, and redisplay the board after each move. On completion of the game, the user is returned to the main menu. (Note Math.random( ) is a useful method for generating random numbers.)


------------------------------------------------------------------------------------------------------------------------

Skeleton Code (to start your answer)


import java.io.*;
public class RandRCollection
{
private Animal[] animals; // i.e. an array of Animal objects
// or whatever name you used
private int numberOfAnimals;

public RandRCollection()
{
animals = new Animal[16];
numberOfAnimals = 0;
}

// see Contacts.java in Lab 6 answers for how this can be useful private void doubleArray()
{
Animal[] newArray = new Animal[animals.length * 2];
for (int i = 0; i < animals.length; ++i)
{
newArray[i] = animals[i];
}
animals = newArray;
}

// and so on ...
}


import java.util.*; import java.io.*; public class Driver
{
private static Scanner keyboard = new Scanner(System.in);
private RandRCollection myAnimals;

public Driver()
{
myAnimals = new RandRCollection();
}

private void runMenu()
{
char input;
do
{
displayMainMenu();
input = keyboard.nextLine().trim().toUpperCase().charAt(0);

switch(input)
{
case 'S':
System.out.println("To be implemented");
break;
case 'A':
 
System.out.println("To be implemented");
break;
case 'F':
System.out.println("To be implemented");
break;
case 'E':
System.out.println("To be implemented");
break;
case 'R':
System.out.println("To be implemented");
break;
case 'X':
System.out.println("To be implemented");
break;
case 'Q':
System.out.println("Exiting system...");
break;
default:
System.out.println("That was not a valid choice!");
}
} while (input != 'Q');
}

private void displayMainMenu()
{
System.out.println("********************"); System.out.println("Recovery and Release"); System.out.println("********************"); System.out.println("S) Show Submenu"); System.out.println("A) Add Animal"); System.out.println("F) Food Lists"); System.out.println("E) Empty Nest"); System.out.println("R) R&R"); System.out.println("X) BYTE"); System.out.println();
System.out.println("Q) Quit"); System.out.println("********************"); System.out.print("Please select: ");
}

public static void main(String[] args)
{
Driver myDriver = new Driver();
myDriver.runMenu();
}
}

No comments:

Post a Comment