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

Using Java, have results display exactly like the SAMPLE OUTPUT at the bottom Cr

ID: 3692870 • Letter: U

Question

Using Java, have results display exactly like the SAMPLE OUTPUT at the bottom

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.

Explanation / Answer

Hello there ,

Please find below code and it's output .

Let me know if you have any quereis.

Thanks.


/**
*
* @author welcome
*/
public class Vehicle {

String make;
String model;
int year;
double 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() {
System.out.println("make=" + make + ", model=" + model + ", year=" + year + ", price=" + price );
}

@Override
public String toString() {
return "Vehicle{" + "make=" + make + ", model=" + model + ", year=" + year + ", price=" + price + '}';
}

}


/**
*
* @author dipal.prajapati
*/
public class PassCar extends Vehicle {

int numPass;
boolean AC;
Motor 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;
this.AC = AC;
this.motor = motor;
}

@Override
public void description() {
super.description();
System.out.println("numPass=" + numPass + ", AC=" + AC + " motor=" + motor.toString());
}

@Override
public String toString() {
return "PassCar{" + "make=" + make + ", model=" + model + ", year=" + year + ", price=" + price + ", numPass=" + numPass + ", AC=" + AC + ", motor=" + motor.toString() + '}';
}

}

/**
*
* @author welcome
*/
public class Motor {
String name;
int cylinders;
int bhp;
double displacement;

public Motor(String name, int cylinders, int bhp, double displacement) {
this.name = name;
this.cylinders = cylinders;
this.bhp = bhp;
this.displacement = displacement;
}

@Override
public String toString() {
return "name=" + name + ", cylinders=" + cylinders + ", bhp=" + bhp + ", displacement=" + displacement ;
}
  
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author welcome
*/
public class Truck extends Vehicle {

String type;
int capacity;
Motor 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;
}

@Override
public void description() {
super.description();
System.out.println("type=" + type + ", capacity=" + capacity + " motor=" + motor.toString());
}

@Override
public String toString() {
return "Truck{" + "make=" + make + ", model=" + model + ", year=" + year + ", price=" + price + ", type=" + type + ", capacity=" + capacity + ", motor=" + motor.toString() + '}';
}

}


/**
*
* @author dipal.prajapati
*/
public class TestVehicle {

public static void main(String args[]) {
Motor motor1 = new Motor("EcoBoost", 6, 310, 2.3);
Motor motor2 = new Motor("Hemi", 8, 707, 5.7);
Motor motor3 = new Motor("P90D", 0, 762, 0.0);

Vehicle vehicle1 = new PassCar("Ford", "Mustang", 2016, 44500.0, 5, true, motor1);
Vehicle vehicle2 = new PassCar("Tesla", "Model S", 2016, 121000.0, 2, true, motor2);
Vehicle vehicle3 = new Truck("Dodge", "Ram", 2016, 46000.0, "pickup", 1500, motor3);

Vehicle[] arrayOfVehicles = new Vehicle[3];
arrayOfVehicles[0] = vehicle1;
arrayOfVehicles[1] = vehicle2;
arrayOfVehicles[2] = vehicle3;
showVehicle(arrayOfVehicles);
}

public static void showVehicle(Vehicle[] vehicles) {
for (int i = 0; i < vehicles.length; i++) {
vehicles[i].description();

System.out.println();
//System.out.println(vehicles[i].toString());
}
}
}

===O/P==

run:
make=Ford, model=Mustang, year=2016, price=44500.0
numPass=5, AC=true
motor=name=EcoBoost, cylinders=6, bhp=310, displacement=2.3
PassCar{make=Ford, model=Mustang, year=2016, price=44500.0, numPass=5, AC=true, motor=name=EcoBoost, cylinders=6, bhp=310, displacement=2.3}
make=Tesla, model=Model S, year=2016, price=121000.0
numPass=2, AC=true
motor=name=Hemi, cylinders=8, bhp=707, displacement=5.7
PassCar{make=Tesla, model=Model S, year=2016, price=121000.0, numPass=2, AC=true, motor=name=Hemi, cylinders=8, bhp=707, displacement=5.7}
make=Dodge, model=Ram, year=2016, price=46000.0
type=pickup, capacity=1500
motor=name=P90D, cylinders=0, bhp=762, displacement=0.0
Truck{make=Dodge, model=Ram, year=2016, price=46000.0, type=pickup, capacity=1500, motor=name=P90D, cylinders=0, bhp=762, displacement=0.0}
BUILD SUCCESSFUL (total time: 0 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote