inheritances and interfaces, JAVA Programming

Assignment Help:
#queThis assignment will give you practice with defining classes. You are to write a set of
classes that define the behavior of certain animals. You will be given a program that runs
a simulation of a world with many animals moving around in it. Different kinds of
animals will move in different ways and you are defining those differences. As the
simulation runs, animals can “die” by ending up on the same location, in which case the
simulator randomly selects one animal to survive the collision.
For this assignment you will be given a lot of supporting code that runs the simulation.
You are just defining the individual “critters” that wander around this world. On each
round of the simulation, each critter is asked which direction it wants to move. Each
critter can move north, south, east or west or can stay on the current location by saying
that the direction it wants is “center.”
This program will probably be confusing at first because this is the first time where you
are not writing the main method. Your code will not be in control. Instead, you are
defining a series of objects that become part of a larger system. For example, you might
find that you want to have one of your critters make several moves all at once. You
won’t be able to do that. The only way a critter can move is to wait for the simulator to
ask it for a move. The simulator is in control, not your critters. Although this experience
can be frustrating, it is a good introduction to the kind of programming we do with
objects. A typical Java program involves many different interacting objects that are each
a small part of a much larger system.
Each of your classes will implement the Critter interface, shown below.
// Stuart Reges
// 12/03/04
// The Critter interface defines the methods necessary for an animal
// to participate in the critter simulation. It must return a
// character when getChar is called that is used for displaying it on
// the screen. The getMove method must return a legal move (one of
// the constants NORTH, SOUTH, EAST, WEST, CENTER). See the
// CritterInfo interface for a description of its methods.

public interface Critter {
public char getChar();
public int getMove(CritterInfo info);

public static final int NORTH = -1;
public static final int SOUTH = 3;
public static final int EAST = 8;
public static final int WEST = 11;
public static final int CENTER = 42;
Interfaces are discussed in detail in chapter 9 of the textbook, but to do this assignment
you just need to know a few simple rules about interfaces. Your class headers should
indicate that they implement this interface, as in:
public class Bird implements Critter {

} Page #2
The other important detail about interfaces is that you must include in each class a
definition for the two methods in the interface (the getChar method and the getMove
method). For example, below is a definition for a class called Stone that is part of the
simulation. Stones are displayed with the letter S and always stay on the current location
(returning CENTER for their move).
public class Stone implements Critter {
public char getChar() {
return ''S'';

public int getMove(CritterInfo info) {
return CENTER;
Critters move around in a world of finite size, but the word is toroidal (going off the end
to the right brings you back to the left and vice versa; going off the end to the top brings
you back to the bottom and vice versa).
You are allowed to include a constructor for your classes if you want, although it must be
a zero-argument constructors (one that takes no arguments).
You are to implement five classes. The behavior of each class is described below.
Class getChar getMove
Bird B Randomly selects one of the four directions each time
Frog F
Picks a random direction, moves 3 in that direction,
repeat (same as bird, but staying in a single direction
Mouse M West 1, north 1, repeat (zig zag to the NW)
Turtle T South 5, west 5, north 5, east 5, repeat (clockwise box)
Wolf You decide You define this
For the random moves, each possible choice must be equally likely. You may use either
a Random object or the Math.random() method to obtain pseudorandom values.
The first four of these classes won’t use the CritterInfo object that is passed to getMove.
This object is provided in case you want to do something with this information in
defining your Wolf class. See the description of CritterInfo later in the writeup.
The critter world is divided into cells that have integer coordinates, much like the pixels
in a DrawingPanel. There are 100 cells across and 50 cells up and down. As with the
DrawingPanel, the upper-left cell has coordinates (0, 0), increasing x values move you
right and increasing y values move you down.
Notice that the Critter class defines five constants for the various directions. You can
refer to these using the name of the interface (Critter.NORTH, Critter.SOUTH, etc) or
you can refer to them directly (NORTH, SOUTH, etc) because you are implementing the
interface. Your code should not depend upon the specific values assigned to these
constants, although you may assume they will always be of type int. You will lose style
points if you fail to use the named constants when appropriate. As noted above, your Page #3
critter can stay on its current location by indicating that the direction it wants to move is
Every time your critters are asked to move, they are passed an object of type CritterInfo
that will provide information about that particular critter. For example, you can find out
the critter’s current x and y coordinates by calling getX() and getY(). You can find out
what is around the critter by calling getNeighbor using one of the direction constants.
You will be told the display character for whatever is in that location (a period for an
empty cell). The CritterInfo interface is defined as follows:
// Stuart Reges
// 5/22/06
// The CritterInfo interface defines a set of methods for querying the
// state of a critter simulation. The getX and getY methods return a
// critter''s current location in the world. The getNeighbor method
// takes a direction as a parameter (one of the constants NORTH,
// SOUTH, EAST, WEST or CENTER from the Critter interface). It
// returns the display character for the critter that is one unit away
// in that direction (a period if the square is empty). The getHeight
// and getWidth methods return the height and width of the world.

public interface CritterInfo {
public int getX();
public int getY();
public char getNeighbor(int direction);
public int getHeight();
public int getWidth();
Notice that you are asked to define the Wolf class yourself, including the display
character. Critters are asked what display character to use on each round of the
simulation, so you can have a critter that displays itself differently over time.
Four of the style points for this assignment will be awarded on the basis of how much
energy and creativity you put into defining an interesting class. These four points will be
much harder to earn than the other 16 points for the assignment, so it really is a way for
us to reward the students who decide to spend time figuring out an interesting critter
The rest of the style points will be awarded on the basis of your use of good comments
and variable names and your ability to express these operations simply and clearly. Any
data fields (state variables) of your classes should be declared as “private”.

Related Discussions:- inheritances and interfaces

Toward privacy preserving and collusion resistance, Need Toward Privacy Pre...

Need Toward Privacy Preserving and Collusion Resistance in a Location Proof Updating System Project Description: Today's location-sensitive service relies on user's mobile de

What do you understand by java virtual machine, Java Programming 1. Wha...

Java Programming 1. What do you understand by Java Virtual Machine? 2. Write a simple Java program to display a string message and explain the steps of compilation and execu

What is constructors and explain with an example, What is Constructors? Exp...

What is Constructors? Explain with an example? A constructor forms a new instance of the class. It initializes all the variables and does any work essential to prepare the clas

Compare classes of java with c++, Compare classes with C++ Some signif...

Compare classes with C++ Some significant points to consider when defining a class in java as you probably noticed from above given skeleton are -  There  are  no  global

How instant messaging works explain their process, How instant messaging wo...

How instant messaging works? Write the steps involved in this process? The Instant Messaging services available permit us to manage a list of people (contacts) that we interact

Code, pebble merchant

pebble merchant

Advanced java info1414, This is a working program that shows election resul...

This is a working program that shows election results from the infamous 2000 presidential election involving a lot of Floridians and 'hanging chads'. The program as written reads a

Hotel online booking system, who can helps me with pseudocode with that top...

who can helps me with pseudocode with that topic above mencioned

Illustrate the parser method, Illustrate the parser method The below ta...

Illustrate the parser method The below table summarizes the parser methods available to a java programmer.

Break statement, The break statement is utilized for terminating the curren...

The break statement is utilized for terminating the current While or For loop and after that transferring program control towards statement just after the terminated loop. The foll

Write Your Message!

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd