Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your programming task is to write a program which creates two new classes, “Car”

ID: 3596322 • Letter: Y

Question

Your programming task is to write a program which creates two new classes, “Car” and “CarGame”

The Car class will keep track of three things about the car:

Model (e.g. “Corvette”)

Year (e.g. “2016”)

Speed (as an integer)

The Car class should have a constructor that takes in two parameters, one for model and one for year. The initial speed should be set to 0.

The Car class will have the following accessor and mutator methods

setModel

setYear

setSpeed

getModel

getYear

getSpeed

It will also have two methods: accelerate and decelerate. The accelerate method will increase the speed by 5 and the decelerate method will decrease it by 5.

The CarGame class will contain the main method should prompt the user to enter a Model and Year for the car. After the car is created it will prompt the user to determine whether the user wants to accelerate or decelerate (prompt should read: “Enter 1 to accelerate, 2 to decelerate and 0 to end the game:”).

Keep looping until the user enters “0”, “1” or “2”. Based on the input do the following:

0 – end the program

1 – call the car’s accelerate method and display the new speed.

2 – call the car’s decelerate method and display the new speed

Explanation / Answer

Please find my implementation.

public class Car {

   private int year;

   private String model;

   private double speed;

   //constructor

   public Car(int year, String model) {

       super();

       this.year = year;

       this.model = model;

   }

   public int getYear() {

       return year;

   }

   public String getModel() {

       return model;

   }

   public double getSpeed() {

       return speed;

   }

   public void setYear(int year) {

       this.year = year;

   }

   public void setModel(String model) {

       this.model = model;

   }

   public void setSpeed(double speed) {

       this.speed = speed;

   }

   //accelerator

   public void accelerate(){

       speed+=5;

   }

   //break

   public void decelerate(){

       speed-=5;

   }

}

#############

import java.util.Scanner;

public class CarGame {

   public static void main(String[] args) {

       Car c = new Car(2016, "Corvette");

       Scanner sc = new Scanner(System.in);

       while(true) {

           System.out.print("Enter 1 to accelerate, 2 to decelerate and 0 to end the game: ");

           int op = sc.nextInt();

           if(op == 0)

               break;

           else if(op == 1) {

               c.accelerate();

           }

           else if(op == 2) {

               c.decelerate();

           }

           System.out.println(c.getModel()+", "+c.getYear()+", "+c.getSpeed());

       }

       sc.close();

   }

}

/*

Sample run:

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 1

Corvette, 2016, 5.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 1

Corvette, 2016, 10.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 1

Corvette, 2016, 15.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 1

Corvette, 2016, 20.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 2

Corvette, 2016, 15.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 2

Corvette, 2016, 10.0

Enter 1 to accelerate, 2 to decelerate and 0 to end the game: 0

*/