First question: Why am having an error pop up when running my program that says: "Editor does not contain main type" on my TestVehicle program? Second question: Does the program follow what my professor wants in his instructions? I have gone over them but would like another opinion. Instructions: 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. My code for all three files: 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; public static void main(String[] args) { Vehicle veh1 = new Vehicle ("Nissan", "GT-R", 2018, 80000.0, new Motor(6, 565, "Gasoline Fuel")); Vehicle veh2 = new Vehicle ("Nissan", "350z", 2009, 14279.0, new Motor(6, 306, "Gasoline Fuel")); Vehicle veh3 = new Vehicle ("Nissan", "370z", 2018, 43349.0, new Motor(6, 332, "Gasoline Fuel")); Vehicle veh4 = new Vehicle ("Nissan", "Z", 2023, 42990.0, new Motor(6, 400, "Electric or Gasoline Fuel")); Vehicle veh5 = new Vehicle ("Lamborghini", "Aventador S", 2018, 418000.0, new Motor(12, 691, "Gasoline Fuel")); Vehicle veh6 = new Vehicle ("Chevrolet", "Camaro", 2022, 43845.0, new Motor(8, 650, "Gasoline Fuel")); ArrayList vehicleType = new ArrayList() { { add(veh1); add(veh2); add(veh3); add(veh4); add(veh5); add(veh6); } }; for (Vehicle eachVeh: vehicleType) { System.out.println(eachVeh); } } }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
First question:
Why am having an error pop up when running my program that says: "Editor does not contain main type" on my TestVehicle program?
Second question:
Does the program follow what my professor wants in his instructions? I have gone over them but would like another opinion.
Instructions:
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.
My code for all three files:
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;
public static void main(String[] args) {
Vehicle veh1 = new Vehicle ("Nissan", "GT-R", 2018, 80000.0, new Motor(6, 565, "Gasoline Fuel"));
Vehicle veh2 = new Vehicle ("Nissan", "350z", 2009, 14279.0, new Motor(6, 306, "Gasoline Fuel"));
Vehicle veh3 = new Vehicle ("Nissan", "370z", 2018, 43349.0, new Motor(6, 332, "Gasoline Fuel"));
Vehicle veh4 = new Vehicle ("Nissan", "Z", 2023, 42990.0, new Motor(6, 400, "Electric or Gasoline Fuel"));
Vehicle veh5 = new Vehicle ("Lamborghini", "Aventador S", 2018, 418000.0, new Motor(12, 691, "Gasoline Fuel"));
Vehicle veh6 = new Vehicle ("Chevrolet", "Camaro", 2022, 43845.0, new Motor(8, 650, "Gasoline Fuel"));
ArrayList<Vehicle> vehicleType = new ArrayList<Vehicle>() {
{
add(veh1);
add(veh2);
add(veh3);
add(veh4);
add(veh5);
add(veh6);
}
};
for (Vehicle eachVeh: vehicleType) {
System.out.println(eachVeh);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images