Question

In: Computer Science

PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...

PLEASE CODE THIS IN JAVA

Create a driver class Playground that contains the function, public static void main(String[] args) {}.

  • Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION.
  • Add all 4 instances into a single array called, “elements.”
  • Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed method.
  • Run the getCount() method (GET COUNT METHOD SHOWN IN CAR CLASS BELOW THIS QUESTION) to display how many total cars were created.

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;
        }
        
}

Solutions

Expert Solution

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();
                }
   }
}

Related Solutions

Create a new Java file, containing this code public class DataStatsUser { public static void main...
Create a new Java file, containing this code public class DataStatsUser { public static void main (String[] args) { DataStats d = new DataStats(6); d.append(1.1); d.append(2.1); d.append(3.1); System.out.println("final so far is: " + d.mean()); d.append(4.1); d.append(5.1); d.append(6.1); System.out.println("final mean is: " + d.mean()); } } This code depends on a class called DataStats, with the following API: public class DataStats { public DataStats(int N) { } // set up an array (to accept up to N doubles) and other member...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
Task 2/2: Java program Based upon the following code: public class Main {   public static void...
Task 2/2: Java program Based upon the following code: public class Main {   public static void main( String[] args ) {     String alphabet = "ABCDEFGHIJKLMNMLKJIHGFEDCBA";     for( <TODO> ; <TODO> ; <TODO> ) {       <TODO>;     } // Closing for loop   } // Closing main() } // Closing class main() Write an appropriate loop definition and in-loop behavior to determine if the alphabet string is a palindrome or not. A palindrome is defined as a string (or more generally, a token) which...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static...
In java. Please explain. Consider the following program: } import java.util.*; public class Chapter7Ex12 { static Scanner console = new Scanner(System.in); public static void main(String[] args) { double num1; double num2; System.out.print("Enter two integers: "); num1 = console.nextInt(); num2 = console.nextInt(); System.out.println(); if (num1 != 0 && num2 != 0) System.out.printf("%.2f\n", Math.sqrt(Math.abs(num1 + num2 + 0.0))); else if (num1 != 0) System.out.printf("%.2f\n", Math.floor(num1 + 0.0)); else if (num2 != 0) System.out.printf("%.2f\n",Math.ceil(num2 + 0.0)); else System.out.println(0); }} a. What is the...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT