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

Write a class \"car\" with data fields \"make\" and \"speed.\" The constructor s

ID: 3848080 • Letter: W

Question

Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed. Create a class TestCar that instantiates a car and prints the default speed as well as a modified speed.

Explanation / Answer

Hi,

Please see below the classes and sample output.

Please comment for any queries/feedbacks.

Thanks.

Car.java


public class Car {
  
   private static double defaultSpeed = 50;
   private String make;
   private double speed;
  
  
   //Constructor
   public Car(String make) {
       super();
       this.make = make;
       this.speed = defaultSpeed;
   }
  
   /**
   * The method should return the defaultSpeed
   * return double
   */
   public double speed(){
       return defaultSpeed;
   }
  
   /**
   * Overloaded method for speed that takes a speed parameter.
   * return double
   */
   public double speed(double speed){
       //setting the speed
       setSpeed(speed);
      
       //this class should take a getSpeed method that returns the speed
       return getSpeed();
   }
  
   //Getters and setters
   public String getMake() {
       return make;
   }
   public void setMake(String make) {
       this.make = make;
   }
   public double getSpeed() {
       return speed;
   }
   public void setSpeed(double speed) {
       this.speed = speed;
   }
  
  

}

TestCar.java


public class TestCar {

   public static void main(String[] args) {
       //Creates Car object
       Car car = new Car("Steel");
      
       //Printing the default speed
       System.out.println("Default Speed : "+car.speed());
      
       //Printing the modified speed
       System.out.println("Modified Speed : "+car.speed(90));

   }

}

Sample output:

Default Speed : 50.0
Modified Speed : 90.0

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