must use java Programming Assignment #5 – CarDemo Write a class named Car that h
ID: 3828664 • Letter: M
Question
must use java
Programming Assignment #5 – CarDemo
Write a class named Car that has the following fields:
year. The year field is a String that holds the car’s year model.
make. The make field is a String that holds the make of the car.
speed. The speed field is a int that holds the car’s current speed.
In addition, the class should have the following methods.
Constructor. The constructor should accept the car’s year model and make as arguments. These values should be assigned to the object’s year and make fields. The constructor should also initialize 0 to the speed instance field.
Accessors/Mutators. The appropriate getters/setters methods get the values stored in an object’s year, make, speed fields.
accelerate(). The accelerate() method should add 5 to the speed field each time it is called. There are no inputs or outputs to accelerate().
brake(). The brake() method should subtract 5 from the speed field each time it is called. There are no inputs or outputs to brake().
Write a Driver Program (with a main() method) to demonstrate the Car class. The driver program (CarDemo.java) will:
Prompt user to enter the Year of the Car. Your choice to use Scanner or JOptionPane.
Prompt user to enter the Make of the Car. Your choice to use Scanner or JOptionPane.
Create a Car object, and
Then call the accelerate() method five times on the car object.
After each call to the accelerate method, get the current speed of the car and display it.
Then, call the brake() method five times on the car object.
After each call to the brake method, get the current speed of the car and display it.
The Driver Program will create output as demonstrated below:
Console Input….you can use JOptionPane if preferred:
Enter year of car: 2004[ENTER] // this is prompt and keyboard entry
Enter Make of car: Porsche[ENTER] // this is prompt and keyboard entry
Console Output:
Current status of the car:
Year: 2004
Make: Porsche
Speed: 0
2004 Porsche Accelerating... // notice the year and make of car printed
Now the speed is 5
Now the speed is 10
Now the speed is 15
Now the speed is 20
Now the speed is 25
2004 Porsche Braking... // notice the year and make of car printed
Now the speed is 20
Now the speed is 15
Now the speed is 10
Now the speed is 5
Now the speed is 0
Explanation / Answer
Please find my implementation without JOption.
######### Car.java ######
public class Car {
private int yearModel;
private String make;
private double speed;
//constructor
public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
this.speed = 0;
}
//Accessors methods
public int getYearModel() {
return yearModel;
}
public String getMake() {
return make;
}
public double getSpeed() {
return speed;
}
//accelerator
public void accelerate(){
speed+=5;
}
//break
public void brake(){
speed-=5;
}
}
######### CarDemo.java ########
public class CarDemo {
public static void main(String[] args) {
Car c = new Car(2015, "Car-345");
System.out.println("Current status of the car: ");
System.out.println("Year: "+c.getYearModel());
System.out.println("Make: "+c.getMake());
System.out.println("Speed: "+c.getSpeed());
System.out.println();
System.out.println(c.getYearModel()+" "+c.getMake()+" Accelerating...");
c.accelerate();
System.out.println("Now the speed is: "+c.getSpeed());
c.accelerate();
System.out.println("Now the speed is: "+c.getSpeed());
c.accelerate();
System.out.println("Now the speed is: "+c.getSpeed());
c.accelerate();
System.out.println("Now the speed is: "+c.getSpeed());
c.accelerate();
System.out.println("Now the speed is: "+c.getSpeed());
System.out.println();
System.out.println(c.getYearModel()+" "+c.getMake()+" Braking...");
c.brake();
System.out.println("Now the speed is: "+c.getSpeed());
c.brake();
System.out.println("Now the speed is: "+c.getSpeed());
c.brake();
System.out.println("Now the speed is: "+c.getSpeed());
c.brake();
System.out.println("Now the speed is: "+c.getSpeed());
c.brake();
System.out.println("Now the speed is: "+c.getSpeed());
}
}
/*
Sample run:
Current status of the car:
Year: 2015
Make: Car-345
Speed: 0.0
2015 Car-345 Accelerating...
Now the speed is: 5.0
Now the speed is: 10.0
Now the speed is: 15.0
Now the speed is: 20.0
Now the speed is: 25.0
2015 Car-345 Braking...
Now the speed is: 20.0
Now the speed is: 15.0
Now the speed is: 10.0
Now the speed is: 5.0
Now the speed is: 0.0
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.