Part1: Write a class named Car.java that has the following fields: yearModel . T
ID: 3593129 • Letter: P
Question
Part1:
Write a class named Car.java that has the following fields:
yearModel. The yearModel field is an int that holds the car’s year model
make. The make field references a String object that holds the make of the car
speed. The speed field is an int field that holds the car’s current speed
In addition, the class should have the following constructors and other methods
Constructor. The constructor should accept the car’s model and make as arguments. These values should be assigned to the object’s yearModel and make fields. The constructor should also assign 0 to the speed field.
Accessors. Appropriate accessor methods should get the values stored in an object’s yearModel, make, and speed fields.
accelerate. The accelerate method should add 5 to the speed field each time it is called.
brake. The brake method should subtract 5 from the speed field each time it is called.
Part2:
Demonstrate the Car.java class in the DriveCar.java which creates a Car object.
The program will ask the user for the yearModel and make of the car
The program will ask the user to enter the number of times to accelerate the car (0 - 10 times). After each call to the accelerate method, get the current speed of the car and display it.
The program will ask the user the number of times to place the brake (0 - 10 times). After each call to the brake method, get the speed of the car and display it. (10 points)
The program should ask the user whether he/she wants to continue driving or not
Input validation must include:
1.Number of times to accelerate: 0 -10 times
2.Number of times to brake: 0 – 10 times
3.Number of times to brake must not be greater than number of times to accelerate (or you will end with negative speed) (5 points)
Explanation / Answer
Car.java
public class Car {
public Car(int yearModel, String make) {
super();
this.yearModel = yearModel;
this.make = make;
this.speed = 0;
}
int yearModel;
String make;
int speed;
public int getYearModel() {
return yearModel;
}
public void setYearModel(int yearModel) {
this.yearModel = yearModel;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed < 0) speed = 0;
this.speed = speed;
}
public void accelerate() {
setSpeed(getSpeed() + 5);
}
public void brake() {
setSpeed(getSpeed() - 5);
}
}
DriveCar.java
import java.util.Scanner;
public class DriveCar {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter year model of car: ");
int yearModel = sc.nextInt();
sc.nextLine();
System.out.print("Enter make of car: ");
String make = sc.nextLine();
Car car = new Car(yearModel, make);
while(true) {
int accTimes = 0;
while(true) {
System.out.print("Enter the number of times to accelerate the car (0 - 10 times) ");
accTimes = sc.nextInt();
if (accTimes < 0 || accTimes > 10){
System.out.println("Chosse value from (0-10)");
}
else {
break;
}
}
for (int i = 0; i < accTimes; i++) {
car.accelerate();
System.out.println("Car speed now is: " + car.getSpeed());
}
int breakTimes = 0;
while(true) {
System.out.print("Enter the number of times to brake the car (0 - 10 times) ");
breakTimes = sc.nextInt();
if (breakTimes < 0 || breakTimes > 10){
System.out.println("Chosse value from (0-10)");
}
else if (breakTimes > accTimes){
System.out.println("Enter break times less than accelerate times.");
}
else {
break;
}
}
for (int i = 0; i < breakTimes; i++) {
car.brake();
System.out.println("Car speed now is: " + car.getSpeed());
}
System.out.println("Do you want to continue driving? (y/n) ");
String choice = sc.nextLine();
if (choice.equalsIgnoreCase("n")) {
break;
}
}
sc.close();
}
}