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

need help creating prg in java Car Class Write a class named Car that has the fo

ID: 3761217 • Letter: N

Question

need help creating prg in java

Car Class Write a class named Car 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 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.

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

Demonstrate the class in a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

Explanation / Answer


//Car.java
public class Car
{
  
   //instance variables
   private int yearModel;
   private String make;
   private int speed;
  
   //constructor that takes year and make
   public Car(int yearModel,String make)
   {
       this.yearModel=yearModel;
       this.make=make;
       //set speed to zero
       speed=0;
   }
  
   //Set model of car
   public void setModel(int yearModel)
   {
       this.yearModel=yearModel;
   }
  
   //Set maker of car
   public void setMake(String make)
   {
       this.make=make;
   }
  
  
   //set speed of car
   public void setSpeed(int speed)
   {
       this.speed=speed;
   }

   //Returns yearModel
   public int getModel()
   {
       return yearModel;
   }
  
   //Returns maker of car
   public String getMake()
   {
       return make;
   }
  
  
   //Returns speed
   public int getSpeed()
   {
       return speed;
   }
   //accelerate method that increase the speed by 5
   public void accelerate()
   {
       //call setSpeed with increment of 5 by speed
       setSpeed(speed+5);
   }
  
   //brake method that decrease the speed by 5
   public void brake()
   {
       speed=speed-5;
   }  
}//end of class car

-------------------------


/**The java program CarDriver that creates an instance
* of Car class and call accelerate five times
* and print speeds
* Then call brake and print speed five times*/
//CarDriver.java
public class CarDriver
{
   public static void main(String[] args)
   {

       //Create an instance of Car class
       Car car=new Car(2011, "Wolksvan");

       //call accelerate method five times and print the
       //current speed
       System.out.println("call accelerate speed ");
       car.accelerate();
       System.out.println("Speed : "+car.getSpeed());
       car.accelerate();
       System.out.println("Speed : "+car.getSpeed());
       car.accelerate();
       System.out.println("Speed : "+car.getSpeed());
       car.accelerate();
       System.out.println("Speed : "+car.getSpeed());
       car.accelerate();
       System.out.println("Speed : "+car.getSpeed());

       //call brake method five times and print the
       //current speed
       System.out.println("call brake method ");
       car.brake();
       System.out.println("Speed : "+car.getSpeed());
       car.brake();
       System.out.println("Speed : "+car.getSpeed());
       car.brake();
       System.out.println("Speed : "+car.getSpeed());
       car.brake();
       System.out.println("Speed : "+car.getSpeed());
       car.brake();
       System.out.println("Speed : "+car.getSpeed());

   }//end of the main method

}//end of the class


--------------------------

Sample output:

call accelerate speed
Speed : 5
Speed : 10
Speed : 15
Speed : 20
Speed : 25
call brake method
Speed : 20
Speed : 15
Speed : 10
Speed : 5
Speed : 0