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

Write a class definition for an abstract class, Vehicle, that contains: a double

ID: 3567216 • Letter: W

Question

Write a class definition for an abstract class, Vehicle, that contains:

a double instance variable, maxSpeed
a protected double instance variable, currentSpeed
a constructor accepting a double used to initialize the maxSpeed instance variable
an abstract method, accelerate, that accepts no parameters and returns nothing.
a method getCurrentSpeed that returns the value of currentSpeed
a method getMaxSpeed that returns the value of maxSpeed
a method, pedalToTheMetal, that repeatedly calls accelerate until the speed of the vehicle is equal to maxSpeed. pedalToTheMetal returns nothing.

The main program written below will call the class methods. You need to just write the class as mentioned above.

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

import java.util.Scanner;

///{
//write your code here
//start


//end
///}
public class testdemo extends Vehicle{

public testdemo(double no){
super(no);
}
void accelerate(){
System.out.print(currentSpeed+" ");
currentSpeed++;
}

public static void main(String[] arg){
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the number:");
int n=scanner.nextInt();
testdemo td=new testdemo(n);
System.out.println("Current Speed:"+td.getCurrentSpeed());
System.out.println("Max Speed:"+td.getMaxSpeed());
td.pedalToTheMetal();

}

}

Explanation / Answer

public abstract class Vehicle{
   double maxSpeed;
   protected double currentSpeed;
   Vehicle(double ms){
       maxSpeed = ms;
   }
   abstract void accelerate();
   double getCurrentSpeed(){
       return currentSpeed;
   }
   double getMaxSpeed(){
       return maxSpeed;
   }
   void pedalToTheMetal(){
       while(currentSpeed != maxSpeed){
           accelerate();
       }
   }
}

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