In: Computer Science
PLEASE CODE THIS IN JAVA
Create a driver class Playground that contains the function, public static void main(String[] args) {}.
SportsCar Class:
class SportsCar extends Car{
private String roof;
private int doors;
public SportsCar(String r,int d,String m) {
super(0,m);
roof=r;
doors=d;
}
public void ChangeSpeed() {
speed+=20;
if(speed>65) {
throw new
IllegalArgumentException("Invalid Speed");
}
}
public void sound() {
System.out.println("broooom");
}
@Override
public void Start() {
}
@Override
public void Stop() {
}
}
Airplane Class:
//Airplane.java public class Airplane implements Vehicle{ int speed; public Airplane() { this.speed = 0; } public void Start() { System.out.println("Start method called"); } public void Stop() { System.out.println("Stop method called"); } public void ChangeSpeed() { speed += 5; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } }
Car Class:
interface Vehicle{ void Start(); void Stop(); void ChangeSpeed(); } abstract class Car implements Vehicle{ 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++; } abstract void sound(); public static int getCount() { return count; } }
class SportsCar extends Car{
private String roof;
private int doors;
public SportsCar(String r,int d,String m) {
super(0,m);
roof=r;
doors=d;
}
public void ChangeSpeed() {
speed+=20;
if(speed>65) {
throw new IllegalArgumentException("Invalid Speed");
}
}
public void sound() {
System.out.println("broooom");
}
@Override
public void Start() {
}
@Override
public void Stop() {
}
}
class Airplane implements Vehicle{
int speed;
public Airplane() {
this.speed = 0;
}
public void Start() {
System.out.println("Start method called");
}
public void Stop() {
System.out.println("Stop method called");
}
public void ChangeSpeed() {
speed += 5;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
interface Vehicle{
void Start();
void Stop();
void ChangeSpeed();
}
abstract class Car implements Vehicle{
private int year;
public int speed;
private String make;
private static int count=0;
public Car(int aYear, String aMake) {
year = aYear;
make = aMake;
speed=0;
count++;
}
abstract void sound();
public static int getCount() {
return count;
}
}
public class Playground {
public static void main(String args[]) {
SportsCar car1 = new SportsCar("Roof1",1000,"Door1");
SportsCar car2 = new SportsCar("Roof1",1000,"Door1");
Airplane plane1 = new Airplane();
Airplane plane2 = new Airplane();
String elements[]= {"car1","car2","plane1","plane2"};
for(int i = 0; i < elements.length; i++) {
if(elements[i] == "car1") {
car1.sound();
}
else if(elements[i] == "car2") {
car2.sound();
}
else if(elements[i] == "plane1") {
plane1.ChangeSpeed();
}
else if (elements[i] == "plane2"){
plane2.ChangeSpeed();
}
Car.getCount();
}
}
}