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

6) Complete the following Car class according to requirements. Public class Car

ID: 3839244 • Letter: 6

Question

6) Complete the following Car class according to requirements.

Public class Car {

     int year;

     String model;

     double speed;

} // end of Car

//define a constructor which accepts a car’s year and model as arguments and use them to initialize the values for the year and maker attributes. (2 points)

//define a public method, getSpeed, that returns a double value for the instance variable, speed

// and this method does not have an argument. (1 point)

//define public method, setSpeed; that has double type argument (1 point)

//define a public method, goBreak, which does not have any argument and not return anything.

// When this method is called, if the instance variable, speed, is greater than 10, it reduces the speed

// by 10; otherwise, it sets the speed to zero. (2 Pts)

Explanation / Answer

Car.java

public class Car {
   private int year;
   private String model;
   private double speed;
   public Car1(int year, String model) {
       super();
       this.year = year;
       this.model = model;
   }
   public double getSpeed()
   {
       return speed;
   }
   public void setSpeed(double speed)
   {
       this.speed=speed;
   }
   public void goBreak()
   {
       if(speed>10)
           speed-=10;
       else
           setSpeed(0);
   }
   @Override
   public String toString() {
       return "Car [Year=" + year + ",Model=" + model + ", Speed=" + speed+ "]";
   }
  
}

___________________

TestCar.java

import java.util.Scanner;

public class TestCar {

   public static void main(String[] args) {
       //Declaring variables
       int year;
       String model;
       double speed;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the Inputs entered by the user
System.out.print("Enter the Year :");
year=sc.nextInt();
  
System.out.print("Enter the Model :");
model=sc.next();
  
System.out.print("Enter the Speed :");
speed=sc.nextDouble();
  
//Creating a car class Object
Car c=new Car(year, model);
  
//Setting the speed by calling the setter methid
c.setSpeed(speed);

System.out.println("_____ Before Applying Brake _____");
//Displaying the Car class info
System.out.println(c.toString());
  
//Applying brakes
c.goBreak();
System.out.println("_____ After Applying Brake _____");
//Displaying the Car class info
System.out.println(c.toString());

   }

}

_______________________

Output:

Enter the Year :2012
Enter the Model :Ford
Enter the Speed :60
_____ Before Applying Brake _____
Car [Year=2012,Model=Ford, Speed=60.0]
_____ After Applying Brake _____
Car [Year=2012,Model=Ford, Speed=50.0]

______________

Output#2:

Enter the Year :2010
Enter the Model :Maruthi
Enter the Speed :10
_____ Before Applying Brake _____
Car [Year=2010,Model=Maruthi, Speed=10.0]
_____ After Applying Brake _____
Car [Year=2010,Model=Maruthi, Speed=0.0]

________Thank YOu

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