Skill Practice with these end-of-chapter questions 10.10.1 Multiple Choice Exerc
ID: 3902602 • Letter: S
Question
Skill Practice with these end-of-chapter questions 10.10.1 Multiple Choice Exercises Questions 7,10,11 10.10.4 Identifying Errors in Code Question 31 10.10.5 Debugging Area Questions 33,34,35 10.7 ProgrammingAstvity 2: Using Polymorphism In this Programming Activity, you will complete the implementation of the Tortoise and the Hare race. The Tortoise runs a slow and steady race, while the Hare runs in spurts with rests in between. Figure 10.14 shows a sample run of the race. In this figure, we show only one tortoise and one hare; however, using polymorphism we can easily run the race with any number and combination of tortoises and hares The class hierarchy for this Programming Activity is shown in Figure 10.15. The code for the Racer class, which is the superclass of the Tortoise and Hare classes, is shown in Example 10.20. The Racer class has three instance variables (lines 10-12): a String ID, which identifies the type of racer; and rand y positions, both of which are ints. The class has the usual construc- tors, as well as accessor and mutator methods for the x and y positions and ID. These instance variables and methods are common to all racers, so weExplanation / Answer
RacePoly.java
import java.util.List;
import java.util.ArrayList;
import java.awt.*;
import javax.swing.*;
public class RacePoly extends JFrame
{
private static final long serialVersionUID = 1L;
private List<Racer> racerList;
private static RacePoly app;
private final int FIRST_RACER = 50;
private int finishX;
private boolean raceIsOn = false;
private RacePanel racePanel;
public RacePoly()
{
Container c = getContentPane();
racePanel = new RacePanel();
c.add(racePanel, BorderLayout.CENTER);
this.racerList = new ArrayList<Racer>();
setSize(400, 400);
setVisible(true);
}
private void prepareToRace()
{
int yPos = FIRST_RACER;
final int START_LINE = 40;
final int RACER_SPACE = 50;
char input = Character.toUpperCase(this.getRacer());
while(input != 'S')
{
switch(input)
{
case 'T': this.racerList.add(new Tortoise("Tortoise",START_LINE,yPos)); yPos+=RACER_SPACE; break;
case 'H': this.racerList.add(new Hare("Hare",START_LINE,yPos)); yPos+=RACER_SPACE; break;
default: JOptionPane.showMessageDialog(this, "Wrong Input"); break;
}
this.repaint();
input = Character.toUpperCase(getRacer());
}
}
private class RacePanel extends JPanel
{
private static final long serialVersionUID = 1L;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
finishX = getWidth()-20;
g.setColor(Color.blue);
g.drawLine(finishX, 0 , finishX, getHeight());
if(raceIsOn)
{
for(Racer R : racerList)
{
R.Move();
R.Draw(g);
}
}
else
{
for(Racer R : racerList)
{
R.Draw(g);
}
}
}
}
public void Race() throws InterruptedException
{
if(racerList.size()==0)
{
JOptionPane.showMessageDialog(this, "Racer List is Empty. No Racer is Present", "No Racers", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
raceIsOn = true;
while(!findWinner())
{
Thread.sleep(30);
repaint();
}
reportRaceResults( );
reset();
}
private char getRacer()
{
String input = JOptionPane.showInputDialog( this, " Add Racer ( t for Tortoise, h for hare or s to start the race) " );
if ( input == null )
{
JOptionPane.showMessageDialog(this, "Input is null", "Null Input", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
if(input.trim().length() == 0)
return 'n';
else
return input.charAt(0);
}
private boolean findWinner()
{
for(Racer r : racerList)
if(r.getX() > finishX)
return true;
return false;
}
private void reportRaceResults()
{
raceIsOn = false;
String results = "";
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 ");
}
private void reset() throws InterruptedException
{
char answer;
String input = JOptionPane.showInputDialog( this, "Want to Start Again? (y, n) " );
if ( input == null || input.length( ) == 0 )
{
System.out.println( "Exiting" );
System.exit( 0 );
}
answer = Character.toUpperCase(input.charAt(0));
if (answer == 'Y')
{
raceIsOn = false;
racerList.clear();
app.prepareToRace();
app.Race();
}
else
System.exit(0);
}
public static void main(String [] args) throws InterruptedException
{
app = new RacePoly();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.prepareToRace();
app.Race();
}
}
Hare.java
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Hare extends Racer
{
public Hare()
{
super();
}
public Hare(String id, int x, int y)
{
super(id,x,y);
}
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);
}
public void Draw(Graphics g)
{
int startY = getY( );
int startX = getX( );
g.setColor(Color.LIGHT_GRAY );
g.fillOval((startX-37), startY + 8, 12, 12 ) ;
g.setColor(Color.GRAY );
g.fillOval((startX-30), startY, 20, 20);
g.fillOval((startX-13), (startY+2), 13, 8);
g.fillOval((startX-13), (startY-8), 8, 28);
g.clearRect((startX-37), (startY+15), 32, 5);
}
}
Tortoise.java
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Tortoise extends Racer
{
private int speed;
private Random random;
public Tortoise()
{
super();
this.random = new Random();
this.speed = this.random.nextInt(10) + 90;
}
public Tortoise(String id, int x, int y)
{
super(id,x,y);
this.random = new Random();
this.speed = this.random.nextInt(10) + 90;
}
public void Move()
{
int move = this.random.nextInt(100) + 1;
if (move < speed)
setX(getX() + 1);
}
public void Draw(Graphics g)
{
int startX = getX();
int startY = getY();
g.setColor(new Color(34, 139, 34));
g.fillOval((startX-30), startY, 25, 15);
g.fillOval((startX-10), startY + 5, 15, 10);
g.clearRect((startX-30), startY + 11, 35, 4);
g.setColor( new Color(34, 139, 34));
g.fillOval((startX-27), startY + 10, 5, 5);
g.fillOval((startX-13), startY + 10, 5, 5);
}
}
Racer.java
import java.awt.Graphics;
public abstract class Racer
{
private String ID;
private int x;
private int y;
public Racer()
{
ID = "";
}
public Racer(String rID, int rX, int rY)
{
this.ID = rID;
this.x = rX;
this.y = rY;
}
public String getID()
{
return this.ID;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public void setX(int newX)
{
this.x = newX;
}
public void setY(int newY)
{
this.y = newY;
}
public abstract void Move();
public abstract void Draw(Graphics g);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.