STEP 1: Understand the UML Diagram STEP 2: Build the Inheritance Hierarchy Build
ID: 3641627 • Letter: S
Question
STEP 1: Understand the UML Diagram
STEP 2: Build the Inheritance Hierarchy
Build the class structure shown in the UML diagram. Remember to include getters and setters for each class attribute.
STEP 3: Implement Logic for HotRod Class
Hint: To generate a random number, use the following code, which returns a random number from 0 to 1:
Random rnd = new Random();
rnd.NextDouble();
STEP 4: Implement Logic for StreetTuner Class
STEP 5: Construct the Main Program
Hint: When completing the Main Class for your lab, feel free to reuse old methods from previous labs!
STEP 6: Compile and Test
When done, compile and execute your code, and debug any errors until your code is error-free.
Check your output to ensure that you have the desired output, modify your code as necessary, and rebuild.
Your code may resemble the following:
STEP 2: Build the Inheritance Hierarchy
Explanation / Answer
public abstract class Racer {
private String name;
private int speed;
private Engine engine;
public Racer() {
}
public Racer(String name, int speed, Engine engine) {
this.name = name;
this.speed = speed;
this.engine = engine;
}
abstract boolean isDead();
public String toString() {
return " Racer Name : " + this.getName()
+ " Speed : " + this.getSpeed()
+ getEngine().toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
}
public class Engine {
private int cylinders;
private int horsePower;
public Engine() {
}
public Engine(int cylinders, int horsePower) {
this.cylinders = cylinders;
this.horsePower = horsePower;
}
public String toString() {
return " **Engine Information**"
+ " Cylinders : " + this.getCylinders()
+ " Horse Power : " + this.getHorsePower();
}
public int getCylinders() {
return cylinders;
}
public void setCylinders(int cylinders) {
this.cylinders = cylinders;
}
public int getHorsePower() {
return horsePower;
}
public void setHorsePower(int horsePower) {
this.horsePower = horsePower;
}
}
public class HotRod extends Racer {
private boolean blower;
public HotRod() {
}
public HotRod(String name, int speed, Engine engine, boolean blower) {
super(name, speed, engine);
this.blower = blower;
}
boolean isDead() {
java.util.Random rnd = new java.util.Random();
boolean dead = false;
if (super.getSpeed() > 50 && rnd.nextDouble() > 0.6) {
if (super.getEngine().getHorsePower() < 300 && this.blower==true) {
dead = false;
} else {
dead = true;
}
} else if (super.getSpeed() > 100 && rnd.nextDouble() > 0.4) {
if (super.getEngine().getHorsePower() >= 300 && this.blower == true) {
dead = true;
} else {
dead = false;
}
} else {
dead=false;
}
return dead;
}
public String toString() {
return " **************** Racer Information ****************"
+ super.toString()
+ " Blower : " + this.isBlower();
}
public boolean isBlower() {
return blower;
}
public void setBlower(boolean blower) {
this.blower = blower;
}
}
public abstract class Racer {
private String name;
private int speed;
private Engine engine;
public Racer() {
}
public Racer(String name, int speed, Engine engine) {
this.name = name;
this.speed = speed;
this.engine = engine;
}
abstract boolean isDead();
public String toString() {
return " Racer Name : " + this.getName()
+ " Speed : " + this.getSpeed()
+ getEngine().toString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.