133k views
0 votes
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();

}

}

1 Answer

1 vote

Answer:

Required Java class is given below

Step-by-step explanation:

abstract class Vehicle{

double maxSpeed;

protected double currentSpeed;

//This is the constructor. For your informatin, abstract classes can't be initialized

public Vehicle(double maxSpeed){

this.maxSpeed = maxSpeed;

}

//This is an abstract method. This will be implemented in the class which extends this class

abstract void accelerate();

double currentSpeed(){

return this.currentSpeed;

}

double getMaxSpeed(){

return this.maxSpeed;

}

void pedalToTheMetal(){

//This loop runs as long as the currentSpeed is less than the max speed

while(currentSpeed<=maxSpeed){

accelerate();

}

}

}

User Andris
by
7.6k points