Programming Activity 2: Using Polymorphism Programming Activity, you will comple
ID: 3877161 • Letter: P
Question
Programming Activity 2: Using Polymorphism Programming Activity, you will complete the implementation of the Ihis nth Hare race. The Tortoise runs a slow and steady race, while e runs in spurts with rests in between. Figure 10.14 shows a sample the Hare f the race. In this figure, we show only one tortoise and one hare; uh over using polymorphism we can easily run the race with any number however, and combination of tortoises and hares. The class hierarchy for this Programming Activity is shown in Figure 10.15. code for the Racer class, which is the superclass of the Tortoise and re 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 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 we HaExplanation / Answer
public class Hare extends Racer {
public Hare(String id, int x, int y) {
super(id, x, y);
// TODO Auto-generated constructor stub
}
public void distance() {
{
distance=x-y;
System.out.println("distance covered by heir"+distance);
}
}
}
public class Racer {
public String id;
public int x;
public int y;
public int distance;
public Racer(String id, int x, int y) {
super();
this.id = id;
this.x = x;
this.y = y;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public void distance() {
distance=x-y;
System.out.println(distance);
}
}
public class Tortoise extends Racer {
public Tortoise(String id, int x, int y) {
super(id, x, y);
// TODO Auto-generated constructor stub
}
public void distance() {
{
distance=x-y;
System.out.println("distance covered by tortoise"+distance);
}
}}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Racer r1=new Hare("ABC",0,100);
Racer r2=new Tortoise("LMN",0,150);
r1.distance();
r2.distance();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.