Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will create an Interface that lists constants and abstract methods to be implemented by various Critter classes, an Abstract class which partially implements one

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

You will create an Interface that lists constants and abstract methods to be implemented by various Critter classes, an Abstract class which partially implements one of the Interface's abstract methods that is identical for any of the Critter classes as well as a related Instance field and Constructor, and finally, 6 classes that represent various Critters (actually, one is an inanimate Stone) that have basic movement characteristics. All 6 Critter classes will inherit (extend) the Abstract class mentioned. Like with the previous assignment, one major purpose of inheritance is to eliminate redundant code. For this reason you should take full advantage of super class code or methods when developing your subclasses. In this case, the super class will be this Abstract class. Always remember to use final constants in place of any literal constants (anything other than 0 \& 1) you would normally use in your code! Greater detail of each class follows: 1. Critter.java public interface Critter Describes what methods to be implemented by Critter subclasses and includes several constants. Finals: Here you will define 5 int constants (final) for the direction an animal can move. The names for each of these will HAVE TO BE as follows (the model expects these names): NORTH WEST SOUTH EAST CENTER You can assign any values to them as you see fit, as long as each is unique (may I suggest simply 0,1,2,3,4 respectively) Fields: None. Interfaces can not be instantiated and thus, cannot contain instance fields. Abstract Methods: (the only type of method that can exist in an Interface). These must have the following header signatures: public char getChar(); public int getMove(CritterInfo theInfo); (Critterlnfo is an interface already defined and used by an inner class of the CritterModel class. 5 of the Critter classes will simply not use this parameter. However, the Frog class uses the getNeighbor method via the thelnfo parameter, discussed in the Frog.java details) 2. AbstractCritter.java public abstract class AbstractCritter implements Critter Declares an Instance variable that all subclasses must have, a constructor to initialize the instance variable, and implements the getChar method (this way this method is only implemented once and to be inherited by all Critter subclasses). Fields: A private char field to hold the single character which represents a particular Critter and is used for display on the Critter Environment Grid (fancy name for what you see in the JPanel of the GUI). Constructor - public AbstractCritter(final char theChar) Assigns theChar to the Instance Field (This has to be called by all subclasses to set the correct character for the class). Abstract Methods: None declared here. The responsibility to Implement the getMove method from Critter is passed down to the inheriting subclasses as getMove has to be unique within each Critter subclass. Non Abstract Methods/lmplemented methods from the Critter Interface: public char getchar() 1 \} // Your code goes here. Returns the Instance Field. 3. Stone.java public class Stone extends AbstractCritter Stone is the only inanimate Critter. Several Stone objects are placed on the simulation grid during the initial instantiation and remain stationary throughout the simulation. All but one Critter dies when crashing into a Stone. Frog never moves to where a Stone is and waits until it moves elsewhere. Details to follow. Constructor - public Stone( ) Calls the super constructor passing it an UPPERCASE S: 'S' char to initialize the super's instance field. Implemented methods from the Critter Interface: 3. Stone.java public class Stone extends AbstractCritter Stone is the only inanimate Critter. Several Stone objects are placed on the simulation grid during the initial instantiation and remain stationary throughout the simulation. All but one Critter dies when crashing into a Stone. Frog never moves to where a Stone is and waits until it moves elsewhere. Details to follow. Constructor - public Stone( ) Calls the super constructor passing it an UPPERCASE S: 'S' char to initialize the super's instance field. Implemented methods from the Critter Interface: Implements the super getMove method: public int getMove(Critterinfo theinfo) \{ // Since Stones never move this should simply return CENTER. 3 4. Bat.java public class Bat extends AbstractCritter Bats have rather erratic behavior as they flutter about catching flying insects. Their direction appears random, flying in one of the 4 constant directions (CENTER is not an option since they can't really hover). Constructor - public Bat( ) Calls the super constructor passing it an uppercase B: 'B' char to initialize the super's instance field. Implemented method from the Critter Interface: Implements the super getMove method: public int getMove(Critterinfo theInfo) \{ I/ Randomly chooses and returns one of the 4 directions: NORTH, WEST, SOUTH, EAST, each // having a 25% chance of being chosen. . 3 5. Wolf.java public class Wolf extends AbstractCritter Because they are pack animals, their movement is somewhat consistent and is done in parallel with all wolves. However, they tend to travel in straight directions for some time before changing. This will require several fields to create a pattern of behavior: Fields (all initializations are done in the Constructor & all fields should be private): myDirection: int to remember the current direction. Initialized to EAST. myCount: int to count the number of moves before a change of direction. Initialized to 0 . myFirst: boolean toggles (true/false) whenever a change of direction takes place. Initialize to true. myTarget: int Details of its use is described in getMove below. Initialized to 1. Constructor - public Wolf() Calls the super constructor passing it an UPPERCASE W: 'W' char to initialize the super's instance field. Initialize the 4 fields to the values mentioned above. Implemented methods from the Critter Interface: Implements the super getMove method: Begin by setting a local int variable (let's say direction) to myDirection. This will be returned as is and not changed until the next time getMove is called. Next, increment the myCount, then: when myCount reaches myTarget, several things will happen: if myFirst is false, myTarget should be incremented. toggle myFirst to the opposite state (i.e.false to true, true to false) myCount is reset to 0 myDirection is changed (in a counterclockwise direction, i.e. if myDirection is EAST, set it to NORTH, if NORTH, set to WEST, etc. Of course, this change is what will be assigned the local direction variable next time getMove is called. return the local direction variable value. */ \} 6. Mouse.java public class Mouse extends AbstractCritter Mice move in a zigzag (right-left-right-left, etc.) motion. Their overall direction then is NORTH-WEST-NORTH- 6. Mouse.java public class Mouse extends AbstractCritter Mice move in a zigzag (right-left-right-left, etc.) motion. Their overall direction then is NORTH-WEST-NORTHWEST, or WEST-SOUTH-WEST-SOUTH, or SOUTH-EAST... etc.. The general direction of this zigzag motion should be chosen at random intervals. How you go about accomplishing this will be up to you but, try to maintain a specific direction for some time. Fields (all initializations are done in the Constructor & all fields should be private): You will have to decide just what you need here in order to fulfill the zigzag/random direction of a Mouse. Constructor - public Mouse() Calls the super constructor passing it an UPPERCASE M: 'M' char to initialize the super's instance field. Initialize all other Mouse class instance fields. Implemented methods from the Critter Interface: Implements the super getMove method: /* public int getMove(critterinfo theinfo) \{ Like the Wolf and Bat, the movement of the mouse will be determined by what getMove returns (NORTH, WEST, SOUTH, EAST) to the calling model object (the program using your Critter classes. */ 3 7. Turtle.java public class Turtle extends AbstractCritter As we all know, turtles move rather slowly. As such, they will only move 1/3 as many times as all other Critters (excluding the Stone, of course, which doesn't move at all). The Turtle getMove method can return a direction of CENTER as needed, which will keep it from moving. t. I urtle. java public class Turtle extends AbstractCritter As we all know, turtles move rather slowly. As such, they will only move 1/3 as many times as all other Critters (excluding the Stone, of course, which doesn't move at all). The Turtle getMove method can return a direction of CENTER as needed, which will keep it from moving. Also, each Turtle object will make its first (following instantiation) actual move (none CENTER) based on some randomly chosen value (NORTH, WEST, SOUTH, EAST) and each Turtle will begin movement following some randomly chosen interval (they will not all start moving at the same time). When a Turtle does move (every 3 calls to getMove), if one randomly chosen boolean value is true and a second randomly chosen int value of: 01-move NORTH 2-3 -move EAST 4 -move SOUTH 5-7-move WEST 8-12 move CENTER If this randomly chosen boolean value is false, then the direction based on the second random (int) value is: 0-3 -move SOUTH 4-5 -move WEST 6-7 -move NORTH 8-10 -move EAST Note: The first group uses a range of 13(012)& the second group uses a range of 11(010)& doesn't have CENTER. Fields (all initializations are done in the Constructor \& all fields should be private): Whatever you might need to satisfy the movement of a Turtle. Constructor - public Turtle( ) Calls the super constructor passing it an UPPERCASE T: 'T' char to initialize the super's instance field. Initialize all necessary instance fields of this class. Implemented methods from the Critter Interface: Implements the super getMove method: public int getMove(CritterInfo theInfo) \{ Implemented methods from the Critter Interface: Implements the super getMove method: /* public int getMove (Critterinfo theInfo) \{ This is based on the rules for a Turtle mentioned above. *I 3 8. Frog.java public class Frog extends AbstractCritter The movement of a frog is a bit less complicated than some of our other Critters. Frogs never jump into Stones and thus, never die unless encountering another Critter. Generally, a frog will continue in the same direction unless a counter (one of the fields) is zero. If the counter is zero then a random value should be chosen (in this case, use the Math.random() which returns a double in the range of 0..1.0 (1.0 exclusive). If the result is

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions