Starting from:
$30

$24

Lab Assignment 4 Inheritance and Binary Files Solution

Concepts




Review polymorphism Review abstract classes Review binary files




Review exceptions Review interfaces




Problem Specification




Develop a Java application that simulates a race between different types of animals by using data specific to the animals to move them along in different types of terrain till the first animal arrives at the finish line, and prints out the final configuration showing the position of all the animals on the race track. The animals in the race are ostriches and turtles.




Racetrack




The racetrack will consist of different types of terrain – open plains, forests, lakes, and deserts. The track will be represented by a one-dimensional array of characters, where different characters represent different terrain. On each turn, each racer will get a random number of movement points (between 1 and the animal’s maximum speed) to move through the terrain. When an animal reaches the finish line it becomes a winner and stops moving. It is possible for more than one animal to reach the finish line during the same turn – they are all winners; the winner(s) is determined after all animals have had their turn to move in a particular round.




The different types of terrain and how they will be represented in your program are shown below:




. = open plains




= forest O = desert ~ = lake
| = finish line




Animal Types




The racers will consist of 2 different types of animals. Each animal type will move through the terrain in different ways – e.g. in a forest, an ostrich may be hindered. An animal must have enough movement points remaining to enter the next terrain block or it cannot do so.




NOTE: The maximum speed for each animal is obtained from the input file.




Ostrich:




If an ostrich starts its turn in open plains or desert, it gains a bonus of 1 movement point

CS 1120 LA 4 Inheritance & Binary Files







Ostriches get lost easily, so moving into forest costs 3 movement points




Ostriches don’t like to get wet, so moving into a lake costs 2 movement points Moving into other terrain costs 2 movement points




With each move within the same terrain, an ostrich loses 1 movement point.




Turtle:




Can move into lakes (From any other terrain) for free (costs 0 movement points)




Turtles are slow but steady, and can move into all other terrain for 1 movement point With each move within the same terrain, a turtle loses 1 movement point.




The input file is a binary file with data for 10 animals. The data for each creature is stored on a line in the input file as follows:




The type of the animal (of type “char”: ‘O’ for ostrich, or ‘T’ for turtle) The maximum speed of the animal (of type “int”)




The name of the creature (of type “String”)




The application should exhibit the following functionality




Display the type of the animal followed by its name and serial number.




Serial numbers are the numbers 1 – 10 and are determined by the order in which the animals’ data is read from the input file.
Display the “race track” using specific characters to denote the different types of terrain on the track.




Indicate the position of each animal on its own race track by surrounding the specific terrain symbol with angular brackets (< .. ).




Your output should show a print out of the final configuration of the race (when one or more animals have won the race).
Display a list of all animals currently at the finish line (the winner(s)).




A sample output (to be followed exactly) is included below. The output indicates different types of terrain and how they will be represented in your program:




. = open plains

= forest O = desert ~ = lake
| = finish line







Example Output 1:

Ostrich (Chacha) 1:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#<~|
Turtle (Sissy) 2:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|
Turtle (Tata) 3:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|
Ostrich (Chooby) 4:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|
Ostrich (Lightning) 5:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#<~|
Ostrich (Speedo) 6:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#<~|
Turtle (Steady) 7:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|
Ostrich (Osha) 8:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
. <#
~ |
Turtle (Swinger) 9:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|
Turtle (Coach) 10:
.
O
O
O
#
~
#
#
~
~
O
~
O
#
~ .
.
#
~ <|



Winner(s):




Sissy

Tata

CS 1120 LA 4 Inheritance & Binary Files







Chooby




Steady




Swinger

Coach




Output 1 shows (with <|) that most of the competitors reached the finish line. Some did not reach the finish line, like Chacha, who is still swimming through the last lake when most others finished the race.




Example Output 2:

Ostrich (Chacha) 1:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Turtle (Sissy) 2:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Turtle (Tata) 3:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Ostrich (Chooby) 4:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Ostrich (Lightning) 5:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Ostrich (Speedo) 6:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#<~|
Turtle (Steady) 7:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Ostrich (Osha) 8:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#<~|
Turtle (Swinger) 9:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|
Turtle (Coach) 10:
O
~
#
O
#
#
~
~
~
~
~
O .
O
.
O
~
#
~ <|



Winner(s):




Chacha




Sissy

Tata

Chooby




Lightning




Steady

Swinger

Coach




Design Requirements




Use the following class names: Animal, Ostrich, Turtle, Race and AnimalRace (the test class). Note that an animal object must be one of the two animal types (ostrich or turtle).




The Race class controls everything – including generating a racetrack, keeping a list of racers, and making the racers move each turn. It should also check at the end of each turn whether or not any racer has crossed the finish line yet (if one or more racers crossed the line, the race is over and the output is printed).




This class must implement certain methods; this will be enforced by having the Race class implement the following IRace interface:




public interface IRace {




//returns an array of characters, representing the racetrack char[] getRacetrack();




//Returns the name of the racer with the given index String getRacerName(int racerIndex);




//Returns the position of the racer with the given index int getRacerPosition(int racerIndex);




//Returns whether or not the racer with the given index is a winner (crossed the finish line) boolean getRacerIsWinner(int racerIndex);

CS 1120 LA 4 Inheritance & Binary Files







//Creates the racetrack of a given length and instantiates the specified number of racers //Racetrack should be a random racetrack using the four different types of terrain, and ending //with the finish line.




void createRace(int length, int numRacers) throws IOException;




//Causes each racer to take one turn, moving a number of spaces based on their




movement speed and the terrain void advanceOneTurn();



}







Hints




You can generate one of four terrain symbols using: randNum = rand.nextInt(4); See how this can be used in the code snippet below:



for (int i = 0; i < trackSize - 1; i++) { randNum = rand.nextInt(4); switch (randNum) {




case 0:

raceTrack[i] = '.';

break;

// Include cases for the other three terrains.

}

}




The last position on the track should be the finish line (‘|’).




Your application will need to contain all the logic for moving the racers, including keeping track of each of their positions along the track.



Since each animal needs to be of one of the two types: Ostrich or Turtle, an Animal class cannot be instantiated. What type of class should the Animal class be?



Each animal should also have a reference to the racetrack array, in order to know what type of terrain it is moving into. For any run of the program, all animals should use the same racetrack layout.



An animal needs to be able to move, but each individual subclass (for each different animal) will implement this in a different way. How can you ensure that the subclasses implement this method?



The advanceOneTurn method should iterate through each of your racers (once, with each call to this method), causing them to move appropriately. For each one, you will need to determine randomly how many movement points it gets (between 1 and its maximum speed), and then move it appropriately. The maximum speed for each animal is contained in the input (binary) file.



To test your project, you may want to hardcode a racetrack (or use a specific randomization seed), so that it is the same each time. The final code must create a random track, but sometimes for testing it is more helpful to leave out randomization until the rest works.






Additional Requirements




Software Life Cycle Report with UML diagrams

CS 1120 LA 4 Inheritance & Binary Files







You are required to follow the Software Life Cycle (SLC) methodology presented in class, and write the SLC Report, explaining how you followed the nine phases of the Software Life Cycle in this assignment.




For this LA, you are also required (for the first time) to provide the UML diagram for your application, showing all classes, their fields/attributes and the methods they provide. You should also show the class hierarchy. Your UML diagrams should be included in Part 1 of Phase 2 of your SLC. Please note that UML diagrams must be drawn using an appropriate program (e.g., MS Paint or MS Powerpoint).




A proper design (with stepwise pseudocode refinement), a proper coding method (with stepwise code refinement starting from the most detailed pseudocode refinement), and proper testing are all essential. For reference, please see the Sample SLC Report (covered in class) on Elearning.




Note: Correct pseudocode development will be worth 30% of the total LA grade.




You will need to generate Javadoc for your project. Follow the steps shown in class (a copy of these steps can be found on the Content page in Elearning). If you follow the steps accurately, a “doc” folder (for Javadoc) should be created in your project.




Coding Standards




You must adhere to all conventions in the CS 1120 Java coding standards (available on Elearning for your Lab). This includes the use of white spaces and indentations for readability, and the use of comments to explain the meaning of various methods and attributes. Be sure to follow the conventions also for naming classes, variables, method parameters and methods.







Assignment Submission




Generate a .zip file that contains all your files including: o Program files




o Any input or output files

o The SLC Report (a text with description of all nine phases of the Software Life Cycle) Submit the .zip file to the appropriate folder on Elearning.




NOTE: The Elearning folder for LA submission will remain open beyond the due date but will indicate by how many days you missed the assignment deadline.




The penalty for late submissions as stated in the course syllabus will be applied in grading any assignment submitted late. Remember to turn in an LA even if not completed within 7 days after the deadline – since a student who does not submit 2 or more LAs will earn a failing grade in the class (even with perfect scores earned in the remaining LAs, exams, etc.)

More products