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

IN JAVA. Create the four classes as per the above UML diagram. Do NOT add any at

ID: 3669704 • Letter: I

Question

IN JAVA. Create the four classes as per the above UML diagram. Do NOT add any attributes or methods that don't appear in the UML diagrams. Don't modify anything, either. Create an executable class named TestVehicle test your work. In TestCollege, add code that accomplishes the tasks below. The sample output might assist you to understand these tasks.

In the main method of TestVehicle:

instantiate a Motor object for each vehicle you will code.

instances as follows:

make two objects with declared type Vehicle but actual type PassCar.

make at least one object of declared type Vehicle but actual type Truck.

make an array of type Object with the instances created above.

execute a void method named showVehicle (see below) with the Object array as sole argument.

create an Object Arraylist from the array already created. See page 439.

use a for loop to run through the ArrayList and display each object's data.

In the showVehicle method:

use a foreach loop to process the Object array.

for each Object, display the description method followed by the toString method. This will require
using the instanceof operator and casting.

SAMPLE OUTPUT

Output from the showVehicle method

In this application, a passenger car is an every day vehicle registered to an individual
make=Ford, model=Mustang, year=2016, price=44500.0
PassCar numPass=5, AC=true
Motor EcoBoost, cylinders=6, bhp=310, displacement=2.3

In this application, a Truck is a vehicle designed to transport cargo
make=Dodge, model=Ram, year=2016, price=46000.0
Truck type=pickup, capacity=1500
Motor Hemi, cylinders=8, bhp=707, displacement=5.7

In this application, a passenger car is an every day vehicle registered to an individual
make=Tesla, model=Model S, year=2016, price=121000.0
PassCar numPass=2, AC=true
Motor P90D, cylinders=0, bhp=762, displacement=0.0

Output from ArrayList in main

make=Ford, model=Mustang, year=2016, price=44500.0
PassCar numPass=5, AC=true
Motor EcoBoost, cylinders=6, bhp=310, displacement=2.3

make=Dodge, model=Ram, year=2016, price=46000.0
Truck type=pickup, capacity=1500
Motor Hemi, cylinders=8, bhp=707, displacement=5.7

make=Tesla, model=Model S, year=2016, price=121000.0
PassCar numPass=2, AC=true
Motor P90D, cylinders=0, bhp=762, displacement=0.

Vehicle -make: String -model : String -year: int -price: double +Vehicle(make in rice double +description):void +toString):String PassCar Truck numPass: int AC: boolean -motor: Motor : String -capacity:int -motor: Motor +PassCar(make:String,model:String,year:int,price: double,numPass:int,AC:Boolean,motor:Motor) +description():void +toString():String +Vehicle(make:String,model:String,year:int,price: double,type:String,capacity:int,motor:Motor) +description):void +toString):String Motor -name: String -cylinders: int bhp: int -displacement: double +Motor(na displacement:double +toString):String ri

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class Vehicle {

   String make;
   String model;
   int year;
   double price;

   /**
   * @param make
   * @param model
   * @param year
   * @param price
   */
   public Vehicle(String make, String model, int year, double price) {

       this.make = make;
       this.model = model;
       this.year = year;
       this.price = price;
   }

   /**
   *
   */
   public void description() {

   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Vehicle [make=" + make + ", model=" + model + ", year=" + year
               + ", price=" + price + "]";
   }

}

/**
* @author Srinivas Palli
*
*/
public class Motor {

   String name;
   int cylinders;
   int bhp;
   double displacement;

   /**
   * @param name
   * @param cylinders
   * @param bhp
   * @param displacement
   */
   public Motor(String name, int cylinders, int bhp, double displacement) {

       this.name = name;
       this.cylinders = cylinders;
       this.bhp = bhp;
       this.displacement = displacement;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Motor [name=" + name + ", cylinders=" + cylinders + ", bhp="
               + bhp + ", displacement=" + displacement + "]";
   }

}

/**
* @author Srinivas Palli
*
*/
public class PassCar extends Vehicle {

   int numPass;
   boolean AC;
   Motor motor;

   /**
   * @param make
   * @param model
   * @param year
   * @param price
   * @param numPass
   * @param aC
   * @param motor
   */
   public PassCar(String make, String model, int year, double price,
           int numPass, boolean aC, Motor motor) {
       super(make, model, year, price);
       this.numPass = numPass;
       AC = aC;
       this.motor = motor;
   }

   /**
   *
   */
   public void description() {

   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "numPass=" + numPass + ", AC=" + AC + ", motor="
               + motor + ", make=" + make + ", model=" + model + ", year="
               + year + ", price=" + price + "";
   }

  
  

}

/**
* @author Srinivas Palli
*
*/
public class Truck extends Vehicle {

   String type;
   int capacity;
   Motor motor;

   /**
   * @param make
   * @param model
   * @param year
   * @param price
   * @param type
   * @param capacity
   * @param motor
   */
   public Truck(String make, String model, int year, double price,
           String type, int capacity, Motor motor) {
       super(make, model, year, price);
       this.type = type;
       this.capacity = capacity;
       this.motor = motor;
   }

   /**
   *
   */
   public void description() {

   }

   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "type=" + type + ", capacity=" + capacity + ", motor="
               + motor + ", make=" + make + ", model=" + model + ", year="
               + year + ", price=" + price + "";
   }

  

}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* @author Srinivas Palli
*
*/
public class TestVehicle {
   /**
   * @param args
   */
   public static void main(String[] args) {

       // Motor EcoBoost, cylinders=6, bhp=310, displacement=2.3

       Motor motor1 = new Motor("EcoBoost", 6, 310, 2.3);
       Motor motor2 = new Motor("Hemi", 8, 707, 5.7);

       // make=Dodge, model=Ram, year=2016, price=46000.0
       // Truck type=pickup, capacity=1500
       // Motor Hemi, cylinders=8, bhp=707, displacement=5.7

       Vehicle vehicle1 = new PassCar("Ford", "Mustang", 2016, 44500.0, 5,
               true, motor1);
       // Vehicle vehicle2 = new PassCar("", "", 2016, 32234, 322, true,
       // motor2);

       Vehicle vehicle3 = new Truck("Dodge", "Ram", 2016, 46000.0, "pickup",
               8, motor2);

       Object objects[] = { vehicle1, vehicle3 };

       TestVehicle testVehicle = new TestVehicle();
       testVehicle.showVehicle(objects);

   }

   /**
   * @param objects
   */
   public void showVehicle(Object[] objects) {

       List<Vehicle> list = new ArrayList<Vehicle>();

       for (int i = 0; i < objects.length; i++) {
           list.add((Vehicle) objects[i]);

       }

       Iterator<Vehicle> it = list.iterator();
       while (it.hasNext()) {

           Vehicle vehicle = it.next();
           System.out.println(vehicle);
       }

   }
}

OUTPUT:
numPass=5, AC=true, motor=Motor [name=EcoBoost, cylinders=6, bhp=310, displacement=2.3], make=Ford, model=Mustang, year=2016, price=44500.0
type=pickup, capacity=8, motor=Motor [name=Hemi, cylinders=8, bhp=707, displacement=5.7], make=Dodge, model=Ram, year=2016, price=46000.0