In: Computer Science
Simple Java class.
Create a class Sportscar that inherits from the Car class includes the following:
a variable roof that holds type of roof (ex: convertible, hard-top,softtop)
a variable doors that holds the car's number of doors(ex: 2,4)
implement the changespeed method to add 20 to the speed each time its called
add exception handling to the changespeed method to keep soeed under 65
implement the sound method to print "brooom" to the screen.
create one constructor that accepts the car's roof,doors, year, and make as arguments and assigns the values appropriately. do not create any other constructors.
the interface methods should print messages using system.out.println()
car class:
abstract clas Car implements Vehicle(
private int year;
private int speed;
private string make;
private static int count=0;
public car(int year, string amake){
year = aYaer;
make = aMake;
speed =0;
count++;}
public void sound();
public static int getcount90{
return count;}
}
Implement the requirements as follows:
Program: Sportscar.java
interface Vehicle{}
abstract class Car implements Vehicle{ /* class Car */
private int year;
private int speed;
private String make;
private static int count=0;
public Car(int aYear, String aMake){
year = aYear;
make = aMake;
speed = 0;
count++;
}
public void accelerate(int aSpeed){ /* Method accelerate that increases the speed */
this.speed += aSpeed;
}
public int getSpeed(){ /* method getSpeed() that returns the current speed of the car */
return this.speed;
}
public abstract void sound();
public static int getcount90(){
return count;
}
}
public class Sportscar extends Car{ /* class Sportscar */
private String roof; /* Add an instance variable roof of type String */
private int doors; /* Add an instance variable doors pf type int */
public Sportscar(String roof, int doors, int year, String make){ /* define a constructor that accepts roos, doors, year and make */
super(year, make); /* call super class constructor and pass year and make to it */
this.roof = roof; /* assign roof and doors */
this.doors = doors;
}
public void changeSpeed(){ /* define the method changeSpeed() */
if(getSpeed()+20>65){ /* check if the speed increases beyond 65 */
System.out.println("Can not increate the speed "); /* print an error message if the speed increases beyond 65 */
}
else{
super.accelerate(20); /* otherwise increase the speed by 20 using accelerate() method */
}
}
public void sound(){ /* implement the method sound() */
System.out.println("brooom"); /* display "brooom" on the screen */
}
/*** THIS IS FOR TESTING *****/
public static void main(String[] args){
Sportscar sportscar = new Sportscar("softtop", 2, 2020, "Toyota"); /* create an object of type Sportscar */
sportscar.sound(); /* call the method sound() */
System.out.println("Increasing speed..."); /* increase the speed */
sportscar.changeSpeed(); /* call method changeSpeed() */
System.out.println("Speed :" + sportscar.getSpeed()); /* print speed */
System.out.println("Increasing speed..."); /* increase the speed */
sportscar.changeSpeed(); /* call method changeSpeed() */
System.out.println("Speed :" + sportscar.getSpeed()); /* print speed */
System.out.println("Increasing speed..."); /* increase the speed */
sportscar.changeSpeed(); /* call method changeSpeed() */
System.out.println("Speed :" + sportscar.getSpeed()); /* print speed */
System.out.println("Increasing speed..."); /* increase the speed */
sportscar.changeSpeed(); /* call method changeSpeed() */
System.out.println("Speed :" + sportscar.getSpeed()); /* print speed */
}
}
Screenshot:
Output:
Please don't forget to give a Thumbs Up.