Question: fix the errors in the code, the 2 errors in the code are ain.java: 1 0 : error: class Engine is public, should be declared

fix the errors in the code, the 2 errors in the code are ain.java:10: error: class Engine is public, should be declared in a file named Engine.java public class Engine {^ Main.java:49: error: class Car is public, should be declared in a file named Car.java public class Car {^2 errors.
// Engine class
public class Engine {
private int engineNum;
private int ccRating;
// Default constructor
public Engine(){
this.engineNum =0;
this.ccRating =0;
}
// Parameterized constructor
public Engine(int engineNum, int ccRating){
this.engineNum = engineNum;
this.ccRating = ccRating;
}
// Method to set engine details
public void setEngine(int engineNum, int ccRating){
this.engineNum = engineNum;
this.ccRating = ccRating;
}
// Getter for engineNum
public int getEngineNum(){
return this.engineNum;
}
// Getter for ccRating
public int getCcRating(){
return this.ccRating;
}
// Display engine details
public void showEngine(){
System.out.println("Engine Number: "+ this.engineNum +", CC Rating: "+ this.ccRating);
}
}
// Car class
public class Car {
private int numDoors;
private String make;
private String model;
private Engine engine; // Composition relationship with Engine
// Default constructor
public Car(){
this.numDoors =0;
this.make ="";
this.model ="";
this.engine = new Engine(); // Create a default engine
}
// Parameterized constructor
public Car(int numDoors, String make, String model, int engineNum, int ccRating){
this.numDoors = numDoors;
this.make = make;
this.model = model;
this.engine = new Engine(engineNum, ccRating);
}
// Copy constructor
public Car(Car otherCar){
this.numDoors = otherCar.numDoors;
this.make = otherCar.make;
this.model = otherCar.model;
this.engine = new Engine(otherCar.engine.getEngineNum(), otherCar.engine.getCcRating());
}
// Set car information
public void setInfo(int numDoors, String make, String model){
this.numDoors = numDoors;
this.make = make;
this.model = model;
}
// Update car details
public void updateCar(int numDoors, String make, String model){
this.numDoors = numDoors;
this.make = make;
this.model = model;
}
// Update engine details
public void updateEngine(int engineNum, int ccRating){
this.engine.setEngine(engineNum, ccRating);
}
// Display car details
public void displayCar(){
System.out.println("Car Details:");
System.out.println("Doors: "+ this.numDoors);
System.out.println("Make: "+ this.make);
System.out.println("Model: "+ this.model);
this.engine.showEngine(); // Display engine details
}
// Calculate car cost
public int calCost(){
return this.engine.getCcRating()* this.numDoors *100;
}
// Accessor to get the cost of the car
public int getCost(){
return calCost();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!