Question: 10.7 Programming activity: Using Polymorphism /* The Tortoise and the Hare Race Anderson, Franceschi */ import java.awt.*; import javax.swing.*; import java.util.ArrayList; public class RacePoly extends

10.7 Programming activity: Using Polymorphism

/* The Tortoise and the Hare Race Anderson, Franceschi */

import java.awt.*; import javax.swing.*; import java.util.ArrayList;

public class RacePoly extends JFrame { private ArrayList racerList; // racers stored in ArrayList private static RacePoly 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 RacePoly( ) { 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 writes 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 named racerList * The API of the Hare 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"); */ // Part 1 student code starts here: // write your switch statement here: switch (input) { case 'T': case 't': // Your code break; case 'H': case 'h': // Your code break; default: // Your code break; }

// Part 1 student code ends here.

repaint(); input = getRacer(); // get input from user } // end while } // end prepareToRace

private class RacePanel extends JPanel { /** paintComponent 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 then draw for each element * The API for move is: * void move() * The API for draw is: * void draw(Graphics g) * where g is the Graphics context * passed to this paint method */ // Part 2 student code starts here:

// Part 2 student code ends here. } 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 */ // Part 3 student code starts here:

// Part 3 student code ends here. } } // end paintComponent } // end RacePanel class

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!