y program is not running in my eclipse, can you let me know how runs and if they're any errors. Also, is my input of my Vehicle program put in correctly ( public static void Vehicle(String[] args) {) The instructions are:
My program is not running in my eclipse, can you let me know how runs and if they're any errors. Also, is my input of my Vehicle program put in correctly ( public static void Vehicle(String[] args) {)
The instructions are:
Design and save your Motor class before you make Vehicle, so you can specify Motor as a Vehicle data attribute.
Class Motor (make this class first because Motor is an attribute of class Vehicle)
Attributes (all private)
- int cylinders
- int hp
- String type (possible values being gas, deisel, electric etc)
Methods (all public)
- a constructor that can assign values to all attributes
- getters and for each attribute (setters not needed)
- a toString method that returns the status of a Motor instance, all attributes.
Class Vehicle
Attributes (all private)
- String make
- String model
- int year
- double price
- Motor motor (see below)
Methods (public)
- a constructor that can assign values to all attributes
- a setter for the price
- a getter for the price
- a toString method that returns the status of a Vehicle instance, all attributes.
Class TestVehicle
This is the executable class. In the main method, make an arraylist of five or six Vehicle instances and then use a foreach loop to display them.
Motor:
private int cylinders;
private int hp;
private String type;
public Motor (int cylinders, int hp, String type) {
this.cylinders = cylinders;
this.hp = hp;
this.type = type;
}
public int getCylinders() {
return cylinders;
}
public int getHp() {
return hp;
}
public String getType() {
return type;
}
@Override
public String toString() {
return "Motor (" + "Cylinders: " + cylinders + "HP: " + hp + "Type: " + type + ')';
}
}
Vehicle:
private String make;
private String model;
private int year;
private double price;
private Motor motor;
public Vehicle (String make, String model, int year, double price, Motor motor) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
this.motor = motor;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Vehicle: " + "Make = " + make + "Model = " + model + "Year = " + year + "Price = " + price + "Motor = " + motor;
}
}
TestVehicle:
import java.util.ArrayList;
class TestVehicle {
//PSEUDOCODE
//This is the executable class
// In the main method, make an arraylist of five or six Vehicle instances and then use a foreach loop to display them
public static void Vehicle(String[] args) {
ArrayList<Vehicle> vehicleType = new ArrayList<Vehicle>();
vehicleType.add(new Vehicle ("Nissan", "GT-R", 2018, 80000.0, new Motor(6, 565, "Gasoline Fuel")));
vehicleType.add(new Vehicle ("Nissan", "350z", 2009, 14279.0, new Motor(6, 306, "Gasoline Fuel")));
vehicleType.add(new Vehicle ("Nissan", "370z", 2018, 43349.0, new Motor(6, 332, "Gasoline Fuel")));
vehicleType.add(new Vehicle ("Nissan", "Z", 2023, 42990.0, new Motor(6, 400, "Electric or Gasoline Fuel")));
vehicleType.add(new Vehicle ("Lamborghini", "Aventador S", 2018, 418000.0, new Motor(12, 691, "Gasoline Fuel")));
vehicleType.add(new Vehicle ("Chevrolet", "Camaro", 2022, 43845.0, new Motor(8, 650, "Gasoline Fuel")));
for (Vehicle eachVeh: vehicleType) {
System.out.println(eachVeh.toString());
}
}
}
Step by step
Solved in 3 steps with 1 images