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

Part I: Write a Java class named Car.java to represent a Car type Create a new J

ID: 3845719 • Letter: P

Question

Part I:

Write a Java class named Car.java to represent a Car type

Create a new Java class named Car that has the following fields:

year - The year field is an int that holds a cars year model (e.g. 2010)

make - The make field is a String object that holds the make of the car (e.g. "Porsche")

speed - The speed field is an double that holds a cars current speed (e.g. 25.0)

In addition, the Car class should have the following methods.

Constructor - The constructor should accept the cars year, make, and beginning speed as arguments

These values should be used to initialize the Cars year, make, and speed fields

Getter Methods - Write three accessor (getter) methods to get the values stored in an object fields:

getYear(), getMake(), getSpeed()

accelerate - Write an accelerate method that has no arguments (parameters) passed to it and adds 1 to the speed field each time it is called For example: if the car was going 3 mph, accelerate would set the speed to 4 mph

Part II: Write driver (tester) class named CarTester.java that uses your Car class:

1. create a new Car object

2. call accelerate method twice

3. printout car speed

Explanation / Answer

/*--------------------Part I: Car.java-------------------------------------------------------*/

class Car {
  
   private int year;/*year field holds a cars year model (e.g. 2010)*/
   private String make;/*make field holds the maker of the car (e.g. "Porsche")*/
   private double speed;/*speed field holds car's current speed (e.g. 25.0)*/
  
   /*constructor accepts the cars year, make, and beginning speed as arguments
   * & initializes the Cars year, make, and speed fields
   */
   public Car(int y,String m,double s)
   {
       this.year=y;
       this.make=m;
       this.speed=s;
   }
   /*three accessor (getter) methods to get the values stored in an object fields*/
public int getYear()
{
   return this.year;
}

public String getMake()
{
   return this.make;
}

public double getSpeed()
{
   return this.speed;
}
/*accelerate method that has no arguments (parameters) passed to it
* and adds 1 to the speed field */
public void accelerate()
{
   double newSpeed= this.getSpeed() + 1;
   this.speed = newSpeed;
}
}

/*-------------------------Part II : CarTester.java---------------------------------------------------------*/

/*driver (tester) class named CarTester.java that uses Car class*/
public class CarTester
{

   public static void main(String[] args)
   {
       /*new Car object is created with proper arguments*/
       Car myCar = new Car(2010,"Porsche",25.0);
      
       System.out.println("Car's begging speed was "+myCar.getSpeed());
       System.out.println("After calling accelerate method twice ");
      
       /*accelerate method is called twice*/
       myCar.accelerate();
       myCar.accelerate();
      
       /*Displaying car's current speed*/
       System.out.println("Car's current speed is "+myCar.getSpeed());

   }

}