Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STEP 3: Implement Logic for HotRod Class Provide suitable logic for the toString

ID: 3641906 • Letter: S

Question

STEP 3: Implement Logic for HotRod Class


Provide suitable logic for the toString method. As always, the toString method should reveal the state of an object.
For the isDead method in HotRod, use the logic below to implement the inherited base class abstract method called isDead.
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();


Pseudocode for the isDead method of HotRod Random rnd = new Random(); boolean dead if (speed > 50 && rnd.NextDouble() > 0.6) if (engineHorsePower < 300 && blower=true) dead = false else dead = true end if else if (speed > 100 && rnd.NextDouble() > 0.4) if (engineHorsePower >= 300 && blower = true) dead = true else dead = false end if else dead = false end if STEP 4: Implement Logic for StreetTuner Class


Provide suitable logic for the toString method. As always, the toString method should reveal the state of an object.
For the isDead() method in StreetTuner, use the logic below to implement the inherited base class abstract method called isDead.

Pseudocode for the isDead method of StreetTuner Random rnd = new Random(); boolean dead if (speed > 50 && rnd.NextDouble() > 0.6) if (engineHorsePower < 300 && nitrous=true) dead = false else dead = true end if else if (speed > 100 && rnd.NextDouble() > 0.4) if (engineHorsePower >= 300 && nitrous = true) dead = true else dead = false end if else dead = false end if STEP 5: Construct the Main Program


Hint: When completing the Main Class for your lab, feel free to reuse old methods from previous labs!

Create an array of Racer objects that will hold three Racer objects.
Write a method, called collectRacerInformation that accepts as an argument a Racer object, and then prompts the user to provide the following information for each racer.
Racer name
Racer Speed
Number of cylinders in the racer's engine
Horsepower of the racer's engine
Add logic to collectRacerInformation to determine if the Racer object passed in is a HotRod or a StreetTuner. If it is a HotRod, prompt the user to indicate the existence of a blower. If the object is a StreetTuner, prompt the user to indicate the existence of nitrous. Set these values appropriately.
Write a method called displayRacerInformation that accepts as an argument a Racer object, and then displays all the information for the specific racer type.
Create any combination of Racer objects (make sure you create at least one StreetTuner and one HotRod) and invoke collectRacerInformation, passing in each of the Racer objects. Store each object in the array. For StreetTuner objects, be sure to set the nitrous flag (either true or false), and for the HotRod objects set the blower flag (either true or false).
Iterate through the racer array and, for each Racer object, display all the object's attribute information (call the displayRacerInformation method for each object). Don't forget to indicate whether or not the Racer is dead!

Explanation / Answer


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 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 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 class StreetTuner extends Racer {

    private boolean nitrous;

    public StreetTuner() {
    }

    public StreetTuner(String name, int speed, Engine engine, boolean nitrous) {
        super(name, speed, engine);
        this.nitrous = nitrous;
    }

    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.nitrous == true) {
                dead = false;
            } else {
                dead = true;
            }
        } else if (super.getSpeed() > 100 && rnd.nextDouble() > 0.4) {
            if (super.getEngine().getHorsePower() >= 300 && this.nitrous == true) {
                dead = true;
            } else {
                dead = false;
            }
        } else {
            dead = false;
        }

        return dead;
    }

    public String toString() {
        return " **************** Racer Information ****************"
                + super.toString()
                + " Nitrous          : " + this.isNitrous();
    }

    public boolean isNitrous() {
        return nitrous;
    }

    public void setNitrous(boolean nitrous) {
        this.nitrous = nitrous;
    }
} public class MainDriver {

    public static void main(String[] args) {
        
        Racer[] racer = new Racer[3];
        
        racer[0] = new HotRod("HotRod Racer",85, new Engine(8,100),true);
        racer[1] = new StreetTuner("StreetTuner Racer #1",95, new Engine(10,135),false);
        racer[2] = new StreetTuner("StreetTuner Racer #2",103, new Engine(12,150),true);
        
        System.out.println(racer[0].toString());
        System.out.println(" Is the Racer dead ? "+racer[0].isDead());
        
        System.out.println(racer[1].toString());
        System.out.println(" Is the Racer dead ? "+racer[1].isDead());
        
        System.out.println(racer[2].toString());
        System.out.println(" Is the Racer dead ? "+racer[2].isDead());
    }
}
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;
    }
}