Write a class that represents a car object. A Car has the following data fields:
ID: 3764877 • Letter: W
Question
Write a class that represents a car object. A Car has the following data fields:
yearModel – The model year of the car
make – The manufacturer of the car
speed – The speed the car is currently travelling
A Car should have the following as well:
A Constructor that takes as arguments the car’s year and make. The speed of the care should be set to 0.
Accessors – there should be an appropriate accessor method for each of the data fields.
Mutators – there should be an accelerate method and a brake method. Each of these methods adds or subtracts 5
from the speed of the car respectively. Note: The speed of the car should never be less than 0 and never more than
210 (assume km/h). Your methods should make sure these limits are respected.
***Write JavaDoc comments for this class.***
Write a tester class that:
Prompts the user for the make and model of the Car
Constructs a Car with the input given
Accelerate once and show the speed of the Car
Use a loop to accelerate 5 (or better yet a random number of) times and show the speed of the Car.
Use a loop to decelerate until the Car is stopped. Display the number of times the brakes were applied.
Answer should look like this:
Please enter the make and model of the car: Tiberon 2006 New car created! Tiberon, 2006
The current speed is 5.
The current speed is 30.
The current speed is 0, and the brakes were applied 6 times.
Explanation / Answer
CAR class
public class Car {
private int yearModel;
private String make;
private double speed;
/**
* @param yearModel year model of car
* @param make make of car
*/
public Car(int yearModel, String make) {
this.yearModel = yearModel;
this.make = make;
this.speed = 0;
}
/**
* @return year model of car
*/
public int getYearModel() {
return yearModel;
}
/**
* @return make of car
*/
public String getMake() {
return make;
}
/**
* @return speed of car
*/
public double getSpeed() {
return speed;
}
/**
* Increases speed by 5 km/hr
*/
public void accelerate() {
if (this.speed <= 215)
this.speed += 5;
}
/**
* Decreases speed by 5 km/hr
*/
public void brake() {
if (this.speed >= 5)
this.speed -= 5;
}
}
TESTER Class
import java.util.Scanner;
public class CarTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter make of car");
String make = scanner.nextLine();
System.out.println("Please enter year model of car");
String year = scanner.nextLine();
// Makes new car
Car car = new Car(Integer.parseInt(year), make);
System.out.println("New car created! " + make + ", " + year);
// Accelerate once
car.accelerate();
System.out.println("The current speed is " + car.getSpeed());
// Acclerate 5 times
for (int i = 1; i <= 5; i++)
car.accelerate();
System.out.println("The current speed is " + car.getSpeed());
int counter = 0; // to count number of times brakes are applied
// Declerates car until speed is 0
while (car.getSpeed() > 0) {
car.brake();
counter++;
}
System.out.println("The current speed is " + car.getSpeed() + ", and the brakes were applied " + counter + " times.");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.