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

Write a class named Car that has the following fields: yearModel The year Model

ID: 3576584 • Letter: W

Question

Write a class named Car that has the following fields: yearModel The year Model 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 sped field is an int 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 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. Remember in the method header to capitalize the field after prepending 'get' or ' set' to the field name. accelerate. The accelerate method should add 5 to the speed field each times it is called. brake. The brake method should should subtract 5 from the speed field each time it is called.

Explanation / Answer

CarTester.java


import java.util.Random;
import java.util.Scanner;

public class CarTester {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter the make and model of the case: ");
       String make = scan.next();
       int year = scan.nextInt();
       Car c = new Car(make, year);
       System.out.println("New car created! "+c.getMake()+", "+c.getYearModel());
       Random r = new Random();
       for(int i=0; i<r.nextInt(42)+1;i++){
           c.accelerate();
           System.out.println("The current speed is "+c.getSpeed());
       }
       int j=0;
       for(j=0;c.getSpeed()>0;j++){
           c.brake();
       }
       System.out.println("The current speed is "+c.getSpeed()+" and the brakes were aaplied "+j+" times");
   }

}

Car.java


public class Car {
   private int yearModel;
   private String make;
   private int speed;
   public Car(String make, int year){
       this.make=make;
       this.yearModel = year;
       this.speed=0;
   }
   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) {
       this.speed = speed;
   }
   public void accelerate(){
       if(speed + 5 <= 210){
       speed+=5;
       }
   }
   public void brake(){
       if(speed - 5 >= 0){
       speed-=5;
       }
   }
}  

Output:

Please enter the make and model of the case: Lenovo 2005
New car created! Lenovo, 2005
The current speed is 5
The current speed is 10
The current speed is 15
The current speed is 20
The current speed is 25
The current speed is 30
The current speed is 35
The current speed is 40
The current speed is 45
The current speed is 50
The current speed is 55
The current speed is 60
The current speed is 65
The current speed is 70
The current speed is 75
The current speed is 80
The current speed is 0 and the brakes were aaplied 16 times

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote