Write a class that represents a car object A Car has the fol

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. Nutators - 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. 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. 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.

Solution

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: Tberson 2006
New car created! Tberson, 2006
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 0 and the brakes were aaplied 8 times

 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site