In: Computer Science
Problem Statement
Design a class named Car that has the following fields:
year: The year field is an integer that holds the car’s year.
make: the make field is a String that holds the make of the car.
speed: the speed field is an integer that holds the car’s current speed.
In addition, the class should have the following constructor and other methods:
Constructor: the constructor should accept the car’s year >model and make as parameters. These values should be assigned to the object’s yearand make fields. The constructor should also assign 0 to the speed field.
Accessors: design appropriate accessor methods to get the values stored in an object’s year, 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.
Your solution must include these components:
UML Class diagram
Flowchart
Pseudo-code (Gaddis Pseudo Code)
Program Coded (Java Code)
// Screenshot of the code & output
// Code to copy
import java.util.Scanner;
public class Car {
// instance variables declaration
private int year;
private String make;
private int speed;
// constructor
public Car(int year, String make) {
this.year= year;
this.make = make;
this.speed=0;
}
public int getYear() {
return year;
}
public String getMake() {
return make;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
// accelerate method
public void accelerate() {
speed += 5;
}
// brake method
public void brake () {
speed-=5;
}
public static void main(String[] args) {
int year=0;
int speed=0;
String make=null;
Scanner keyboard = new Scanner
(System.in);
System.out.print("What is the year of your car? ");
year = keyboard.nextInt();
System.out.print("What is the make
of your car? ");
make = keyboard.next();
System.out.print("How fast is your
car going? ");
speed = keyboard.nextInt();
Car car = new Car(year,
make);
car.setSpeed(speed);
car.accelerate();
System.out.println("\nYearModel:" +
car.getYear() + " Make:" + car.getMake() +
" Speed:" + car.getSpeed() + "
MPH.");
keyboard.close();
}
}