Programming Assignment #8-Automobile In this assignment, we will have 3 java cla
ID: 3817355 • Letter: P
Question
Programming Assignment #8-Automobile In this assignment, we will have 3 java class files. CarModeljava, Automobile java, AutomobileDemojava Step 1: Design an enum (chp 6.12) called CarModel in a CarModel java file). Think of at least three values for the constants e.g., CAMRY, CIVIC FOCUS etc. Step 2: Next, design a class called Automobile (in a Automobile java file), which has the following class attributes A class private attribute for the year what data type are you going to use?) A class private attribute for the model (of type CarModel the enum created in Step 1) Ano argument constructor that initializes year to 0 and model to null. An overloaded constructor which takes 2 inputs (int year and CarModel model) and will initialize the instance fields A copy constructor for Automobile A copy0 method to create a copy of the Automobile object. An equals0 method to compare 2 different Automobile objects. Atostring method which will display the automobile's year and model. Because Model is an enumerated type, use a switch statement to initialize the model String to print. The output formatting is completely up to you. For example: "Year: 2016 Model: Maxima" getters (Accessors) and setters (Mutators) for year and model.Explanation / Answer
public enum CarModel{CAMRY,CIVIC,FOCUS};
public class Automobile
{
private int year;
private CarModel cm;
public Automobile()
{
year = 0;
this.cm =null;
}
public Automobile(int year,CarModel cm)
{
this.year = year;
this.cm = cm;
}
public Automobile(Automobile am)
{
this.year = am.year;
this.cm = am.cm;
}
public Automobile copy(Automobile AM)
{
this.year = AM.year;
this.cm = AM.cm;
return this;
}
public boolean equals(Automobile am)
{
if (year == am.year && cm == am.cm)
return true;
else
return false;
}
public String toString()
{
String cmValue;
switch(cm)
{
case CAMRY: cmValue = "CAMRY";
break;
case CIVIC: cmValue = "CIVIC";
break;
case FOCUS: cmValue = "FOCUS";
break;
default : cmValue = " ";
break;
}
return "Year :"+year + " Model : "+cmValue;
}
public void setYear(int year)
{
this.year = year;
}
public void setCM(CarModel cm)
{
this.cm = cm;
}
public int getYear()
{
return year;
}
public CarModel getCM()
{
return cm;
}
}
public class AutomobileDemo
{
public static void main (String[] args)
{
CarModel cm = CarModel.CAMRY;
Automobile car1 = new Automobile(2000,cm);
Automobile car2= new Automobile(car1);
Automobile car3 = new Automobile();
car3.copy(car2);
car3.setYear(2015);
if(car1.equals(car2))
System.out.println(" Car1 is equal to Car2");
else
System.out.println(" Car1 is not equal to Car2");
if(car2.equals(car3))
System.out.println(" Car2 is equal to Car3");
else
System.out.println(" Car2 is not equal to Car3");
if(car1.equals(car3))
System.out.println(" Car1 is equal to Car3");
else
System.out.println(" Car1 is not equal to Car3");
System.out.println("Car1 = "+car1);
System.out.println("Car2 = "+car2);
System.out.println("Car3 = "+car3);
}
}
Output:
Car1 is equal to Car2
Car2 is equal to Car3
Car1 is equal to Car3
Car1= Year : 2000 Model : CAMRY
Car2= Year : 2000 Model : CAMRY
Car3= Year : 2015 Model : CAMRY
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.