Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this Java code that I have, can you show how to make these changes? Alter the car object to display its engines RPM using

In this Java code that I have, can you show how to make these changes?

Alter the car object to display its engines RPM using displayRPM().

Have car display its RPM as the last thing in startYourEngines()

Alter the Racetrack object to give each car a different number and a different color.

Code 1:

public class Car

{

private int number = 0;

private String color = "unknown";

Engine e = new Engine();

public int getNumber()

{

return number;

}

public void setNumber(int number)

{

this.number = number;

}

public String getColor()

{

return color;

}

public void setColor(String color)

{

this.color = color;

}

public void startYourEngine()

{

e.start();

System.out.println("VVrooooooooom!!");

}

}

#2

public class Engine

{

private int size = 1500;

private int cylinders = 4;

private int rpm = 0;

public int getSize()

{

return size;

}

public void setSize(int size)

{

this.size = size;

}

public int getCylinders()

{

return cylinders;

}

public void setCylinders(int cylinders)

{

this.cylinders = cylinders;

}

public int getRpm()

{

return rpm;

}

public void setRpm(int rpm)

{

if((this.rpm + rpm) < 0)

{

this.rpm = 0;

}

else

{

this.rpm = this.rpm + rpm;

}

}

public void start()

{

if(getRpm() > 0)

{

stop();

}

setRpm(800);

System.out.println("Engine has started");

}

public void stop()

{

setRpm(-getRpm());

System.out.println("Engine has Stopped");

}

public void changeSpeed(int r)

{

setRpm(r);

}

}

#3.

public class TestCar

{

public static void main(String[] args)

{

Car c = new Car();

System.out.println("Car in beginning condition:");

System.out.println(c.getNumber() + " " + c.getColor());

c.setNumber(15);

c.setColor("Red");

System.out.println(" Car in altered condition:");

System.out.println(c.getNumber() + " " + c.getColor());

c.startYourEngine();

}

}

#4

public class TestEngine

{

public static void main(String[] args)

{

Engine e = new Engine();

e.start();

System.out.println("RPM is now set to: " + e.getRpm());

e.start();

System.out.println("RPM is now set to: " + e.getRpm());

e.stop();

System.out.println("RPM is now set to: " + e.getRpm());

e.start();

e.changeSpeed(1000);

System.out.println("RPM is now set to: " + e.getRpm());

e.changeSpeed(-2000);

System.out.println("RPM is now set to: " + e.getRpm());

}

}

#5.

public class Racetrack

{

public static void main(String[] args)

{

Car[] cars = new Car[10];

for(int x = 0; x < 10; x++)

{

cars[x] = new Car();

}

System.out.println("Welcome to Race Day!!!!");

System.out.println("Today we have a race between 10 cars");

System.out.println("Drivers! Start your engines!! ");

for(int x = 0; x < 10; x++)

{

cars[x].startYourEngine();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

Students also viewed these Databases questions