Question: In java Get the four files provided.Do not alter classes Racer, Tortoise, or Hare at this point. Race.java modifications Fill in the three places in

In java

Get the four files provided.Do not alter classes Racer, Tortoise, or Hare at this point.

Race.java modifications Fill in the three places in Race.java (the driver program) where code is required. Find these sections by comments starting with "***** Student write"

Add switch statement to prepareToRace().

Add for loop to paintComponent() if clause (enhanced for-loop is best).

Add for loop ro paintComponent() else clause (enhanced for-loop is best)

Save it, compile it, and run Race to watch the races! Run it with a variety of racers.

Instructions Part 2

Make a new Racer--anything you want.

The easiest way to do this is to make a new class and copy into it the code from either Tortoise.java or Hare.java.

Then modify the appearance (draw) and position (move) according to what fits your new racer.

Add your racer to the switch statement in Race.java so you can race it!

Add a new abstract method morph() to the abstract class Racer.java.

Implement some type of morphing to be enacted if the racer wins (or some other time if you prefer).

Some suggestions from students: dance, jump up and down, put on a hat, turn a different color, grow, shrink.

Turning in the program

Submission for this program is "On your honor" statements in the following program which you will upload to this lab. Only print statements that are true.

public class Certify { public static void main(String[] args) { // Print the following line if you got part 1 working and ran some races. System.out.println("I certify on my honor that I ran the Race code in part one."); // Print the following line if you added and raced a new racer type in part 2. System.out.println("My new racer class is "); // Print the following line if you added the morph method in part 2. System.out.println("The action added is morph."); } }

RACE.JAVA

import java.awt.*;

import javax.swing.*;

import java.util.ArrayList;

public class Race extends JFrame {

private ArrayList racerList; // racers stored in ArrayList

private static Race app;

private final int FIRST_RACER = 50;

private int finishX; // location of finish line, dependent on window width

private boolean raceIsOn = false;

private RacePanel racePanel;

/**

* Constructor instantiates list to track racers sets up GUI components

*/

public Race() {

super("The Tortoise & The Hare!");

Container c = getContentPane();

racePanel = new RacePanel();

c.add(racePanel, BorderLayout.CENTER);

racerList = new ArrayList();

setSize(400, 400);

setVisible(true);

}

/**

* prepareToRace method

* uses a dialog box to prompt user for racer types and

* to start the race

* racer types are 't' or 'T' for Tortoise,

* 'h' or 'H' for Hare

* 's' or 'S' will start the race

*/

private void prepareToRace() {

int yPos = FIRST_RACER; // y position of first racer

final int START_LINE = 40; // x position of start of race

final int RACER_SPACE = 50; // spacing between racers

char input;

input = getRacer(); // get input from user

while (input != 's' && input != 'S') {

/* 1. ***** Student write this switch statement

* input local char variable contains the racer type

* entered by the user.

* If input is 'T' or 't',

* add a Tortoise object to the ArrayList named racerList

* which is an instance variable of this class.

* The API of the Tortoise constructor is:

* Tortoise( String ID, int startX, int startY )

* a sample call to the constructor is

* new Tortoise( "Tortoise". START_LINE, yPos )

* where START_LINE is a constant local variable

* representing the starting x position for the race

* and yPos is a local variable representing the next

* racer's y position

*

* If input is 'H' or 'h',

* add a Hare object to the ArrayList name racerList.

* The API of the Tortoise constructor is:

* Hare( String ID, int startX, int startY )

* a sample call to the constructor is

* new Hare( "Hare". START_LINE, yPos )

* where START_LINE is a constant local variable

* representing the starting x position for the race

* and yPos is a local variable representing the next

* racer's y position

*

* After adding a racer to the ArrayList racerList,

* increment yPos by the value of the

* constant local variable RACER_SPACE

*

* If input is anything other than 'T','t','H', or 'h',

* pop up an error dialog box

* A sample method call for the output dialog box is :

* JOptionPane.showMessageDialog( this, "Message" );

*

*/

// write your switch statement here

/** end of student code, Part 1 */

repaint();

input = getRacer(); // get input from user

} // end while

} // end prepareToRace

private class RacePanel extends JPanel {

/**

* paint method

*

* @param g

* Graphics context draws the finish line; moves and draws

* racers

*/

protected void paintComponent(Graphics g) {

super.paintComponent(g);

// draw the finish line

finishX = getWidth() - 20;

g.setColor(Color.blue);

g.drawLine(finishX, 0, finishX, getHeight());

if (raceIsOn) {

/* 2. ***** student writes this code

* Loop through instance variable ArrayList racerList,

* which contains Racer object references,

* calling move, and then draw for each racer.

* The API for move is:

* void move( )

* The API for draw is:

* void draw( Graphics g )

* where g is the Graphics context passed to

* the paint method

*/

// student code goes here

/** end of student code, part 2 */

} else // display racers before race begins

{

/* 3. ***** Student writes this code

* Loop through instance variable ArrayList racerList,

* which contains Racer object references,

* calling draw for each element. (Do not call move!)

* The API for draw is:

* void draw( Graphics g )

* where g is the Graphics context

* passed to this paint method

*/

// student code goes here

/** end of student code, part 3 */

}

}

}

/**

* runRace method checks whether any racers have been added to racerList if

* no racers, exits with message otherwise, runs race, calls repaint to move

* & draw racers calls reportRaceResults to identify winners(s) calls reset

* to set up for next race

* @throws InterruptedException

*/

public void runRace() throws InterruptedException {

if (racerList.size() == 0) {

JOptionPane.showMessageDialog(this, "The race has no racers. exiting",

"No Racers", JOptionPane.ERROR_MESSAGE);

System.exit(0);

}

raceIsOn = true;

while (!findWinner()) {

// slow down here if you know how

Thread.sleep(30);

repaint();

} // end while

reportRaceResults();

reset();

}

/**

* gets racer selection from user

*

* @return first character of user entry if user presses cancel, exits the

* program

*/

private char getRacer() {

String input = JOptionPane.showInputDialog(this, "Enter a racer:"

+ " t for Tortoise, h for hare," + " or s to start the race");

if (input == null) {

System.out.println("Exiting");

System.exit(0);

}

if (input.length() == 0)

return 'n';

else

return input.charAt(0);

}

/**

* findWinners: checks for any racer whose x position is past the finish line

*

* @return true if any racer's x position is past the finish line or false if

* no racer's x position is past the finish line

*/

private boolean findWinner() {

for (Racer r : racerList) {

if (r.getX() > finishX)

return true;

}

return false;

}

/**

* reportRaceResults : compiles winner names and prints message winners are

* all racers whose x position is past the finish line

*/

private void reportRaceResults() {

raceIsOn = false;

String results = "Racer ";

for (int i = 0; i < racerList.size(); i++) {

if (racerList.get(i).getX() > finishX) {

results += (i + 1) + ", a " + racerList.get(i).getID() + ", ";

}

}

JOptionPane.showMessageDialog(this, results + " win(s) the race ");

}

/**

* reset: sets up for next race: sets raceIsOn flag to false clears the list

* of racers resets racer position to FIRST_RACER enables checkboxes and

* radio buttons

* @throws InterruptedException

*/

private void reset() throws InterruptedException {

char answer;

String input = JOptionPane.showInputDialog(this, "Another race? (y, n)");

if (input == null || input.length() == 0) {

System.out.println("Exiting");

System.exit(0);

}

answer = input.charAt(0);

if (answer == 'y' || answer == 'Y') {

raceIsOn = false;

racerList.clear();

app.prepareToRace();

app.runRace();

} else

System.exit(0);

}

/**

* main instantiates the Race object app

* calls runRace method

* @throws InterruptedException

*/

public static void main(String[] args) throws InterruptedException {

app = new Race();

app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

app.prepareToRace();

app.runRace();

}

}

HARE.JAVA

/** Hare class * inherits from abstract Racer class */

import java.awt.Graphics; import java.awt.Color; import java.util.Random;

public class Hare extends Racer { /** Default Constructor: calls Racer default constructor */ public Hare( ) { super( ); }

/** Constructor * @param rID racer Id, passed to Racer constructor * @param rX x position, passed to Racer constructor * @param rY y position, passed to Racer constructor */ public Hare( String rID, int rX, int rY ) { super( rID, rX, rY ); }

/** move: calculates the new x position for the racer * Hare move characteristics: 30% of the time, Hare jumps 5 pixels * 70% of the time, Hare sleeps (no move) * generates random number between 1 & 10 * for 1 - 7, no change to x position * for 8 - 10, x position is incremented by 5 */ public void move( ) { Random rand = new Random( ); int move = rand.nextInt( 10 ) + 1 ;

if ( getX( ) < 100 ) { if ( move > 6 ) setX( getX( ) + 4 ); } else { if ( move > 8 ) setX( getX( ) + 4 ); } }

/** draw: draws the Hare at current (x, y) coordinate * @param g Graphics context */ public void draw( Graphics g ) { int startY = getY( ); int startX = getX( );

// tail g.setColor( Color.LIGHT_GRAY ); g.fillOval( startX - 37, startY + 8, 12, 12 ) ;

//body g.setColor( Color.GRAY ); g.fillOval( startX - 30, startY, 20, 20 );

//head g.fillOval( startX - 13, startY + 2, 13, 8 ); g.fillOval( startX - 13, startY - 8, 8, 28 );

//flatten bottom g.clearRect( startX - 37, startY + 15, 32, 5 ); } }

TORTOISE.JAVA

/** Tortoise class

* inherits from abstract Racer class

*/

import java.awt.Graphics;

import java.awt.Color;

import java.util.Random;

public class Tortoise extends Racer

{

private int speed;

private Random rand;

/** Default Constructor: calls Racer default constructor

*/

public Tortoise( )

{

super( );

setRandAndSpeed();

}

/** Constructor

* @param rID racer Id, passed to Racer constructor

* @param rX x position, passed to Racer constructor

* @param rY y position, passed to Racer constructor

*/

public Tortoise( String rID, int rX, int rY )

{

super( rID, rX, rY );

setRandAndSpeed();

}

/** move: calculates the new x position for the racer

* Tortoise move characteristics: "slow & steady wins the race"

* increment x by 1 most of the time

*/

public void move( )

{

int move = rand.nextInt( 100 ) + 1;

if ( move < speed )

setX( getX( ) + 1 );

}

/** draw: draws the Tortoise at current (x, y) coordinate

* @param g Graphics context

*/

public void draw( Graphics g )

{

int startX = getX( );

int startY = getY( );

g.setColor( new Color( 34, 139, 34 ) ); // dark green

//body

g.fillOval( startX - 30, startY, 25, 15 );

//head

g.fillOval( startX - 10, startY + 5, 15, 10 );

//flatten bottom

g.clearRect( startX - 30, startY + 11, 35, 4 );

//feet

g.setColor( new Color( 34, 139, 34 ) ); // brown

g.fillOval( startX - 27, startY + 10, 5, 5 );

g.fillOval( startX - 13, startY + 10, 5, 5 );

}

private void setRandAndSpeed( ) {

// percentage of time (between 90 - 99%) that this tortoise moves each turn

rand = new Random( );

speed = rand.nextInt( 10 ) + 90;

}

}

CERTIFY.JAVA

public class Certify { public static void main(String[] args) { System.out.println("I certify on my honor that I ran the Race code in part one."); System.out.println("My new racer class is "); System.out.println("The action added is morph."); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!