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

CW code in java •Create a class Car that stores the make and year •Add a static

ID: 3755912 • Letter: C

Question

CW code in java

•Create a class Car that stores the make and year

•Add a static instance field: carsCreated that keeps track of how many cars have been created so far. The constructors should update this field

•Write the set/get methods for make and year

•Implement the default constructor

•Implement a constructor that takes the make and year

•Implement the copy constructor

•Implement the equals method

•Implement the toString method

•Write a main method that:

–Creates a Car, c1, using the default constructor. Use the set methods to set the make and year.

–Creates a Car, c2, using the constructor that takes the make and year

–Creates a Car, copy, which is a copy of c2, using the copy constructor

–Test if copy and c2 are the same using the equals method and print whether they are equal or not

- Print c1, c2 and copy

Write a class Maker that stores two strings: make and model.

•Modify the Car class written in CW to store a Maker object that stores the make and model of the Car.

•Write the necessary methods in Maker

•Modify the necessary code in Car to get the same code written in the CW to compile and run

•Modify the Car class written in the previous work to include the following enum data types:

CarColor: RED, BLACK, BLUE, SILVER

CarType: PORSCHE, FERRARI, JAGUAR

•Modify the Car class to store the CarColor and CarType.

•Modify the Constructor, copy constructor, toString, and equals methods as needed.

•Create a class CarGarage that stores an ArrayList of Car objects.

•Write a finalize method in CarGarage that deletes the ArrayList by setting all the Car objects in it to null, and the ArrayList to null as well.

•In CarGarage, write a main method that:

–Creates 3 different cars with different colors and types and adds them to the CarGarage ArrayList.

–Uses the copy constructor to create 3 more cars (copies of the first three cars) and adds them to the ArrayList.

–Modifies the first 3 cars to all be RED.

–Uses the toString method to print the contents of the ArrayList.

Explanation / Answer

import java.lang.Object.*;

import java.util.ArrayList;

enum CarColor

{

RED,BLACK,BLUE,SILVER,NONE;

}

enum CarType

{

PROSCHE,FERRARI,JAGUAR,NONE;

}

class CarGarage

{

ArrayList<Car> cars;

CarGarage()

{

cars = new ArrayList<Car>();

}

public void addCar(Car car)

{

cars.add(car);

}

public void finalize()

{

int i;

for(i=0;i<cars.size();i++)

{

Car obj = this.cars.get(i);

System.out.println(obj.toString());

try

{

obj.finalize();

}

catch(Throwable e)

{

e.printStackTrace();

}

}

}

public static void main(String args[])

{

CarGarage garage =new CarGarage();

Car c1 = new Car("Danger",999,"ranger","dask",CarColor.BLACK,CarType.FERRARI);

Car c2 = new Car("Dan",1999,"nger","sk",CarColor.BLUE,CarType.JAGUAR);

Car c3 = new Car("Danr",2999,"anger","dsk",CarColor.RED,CarType.PROSCHE);

Car c4 = new Car(c1);

Car c5 = new Car(c2);

Car c6 = new Car(c3);

garage.addCar(c1);

garage.addCar(c2);

garage.addCar(c3);

garage.addCar(c4);

garage.addCar(c5);

garage.addCar(c6);

int i;

for(i=0;i<3;i++)

{

Car ob = garage.cars.get(i);

ob.color = CarColor.RED;

}

garage.finalize();

}

}

class Car

{

String make;

int year;

Maker maker;

static int carsCreated=0;

CarColor color;

CarType type;

Car()

{

this.make=new String("");

this.year=0000;

this.carsCreated+=1;

this.maker = new Maker("","");

this.color = CarColor.NONE;

this.type = CarType.NONE;

}

Car(String make,int year,String maker,String model,CarColor color,CarType type)

{

this.make = make;

this.year = year;

this.carsCreated+=1;

this.maker = new Maker(maker,model);

this.color = color;

this.type = type;

}

Car(Car copy)

{

this.make = copy.make;

this.year = copy.year;

this.carsCreated+=1;

this.maker = new Maker(copy.maker.make,copy.maker.model);

this.color = copy.color;

this.type = copy.type;

}

public void setMake(String make)

{

this.make = make;

}

public String getMake()

{

return this.make;

}

public void setYear(int year)

{

this.year = year;

}

public int getYear()

{

return this.year;

}

public void setColor(CarColor color)

{

this.color = color;

}

public CarColor getcolor()

{

return this.color;

}

public void setType(CarType type)

{

this.type = type;

}

public CarType getType()

{

return this.type;

}

public void setMaker(String make,String model)

{

this.maker = new Maker(make,model);

}

public Maker getMaker()

{

return this.maker;

}

public boolean equals(Object obj)

{

if(obj==this)

{

System.out.println("S");

return true;

}

if(obj instanceof Car==false)

{

System.out.println("In");

return false;

}

Car ob = (Car) obj;

return this.make.equals(ob.make) && this.year==ob.year && this.color==ob.color && this.type==ob.type && this.maker.make.equals(ob.maker.make) && this.maker.model.equals(ob.maker.model);

}

public String toString()

{

return this.make+","+Integer.toString(this.year)+","+this.color.toString()+","+this.type.toString()+","+this.maker.make+","+this.maker.model;

}

@Override

protected void finalize() throws Throwable

{

super.finalize();

}

}

class Maker

{

String make,model;

Maker(String make,String model)

{

this.make = make;

this.model = model;

}

public void setMake(String make)

{

this.make = make;

}

public String getMake()

{

return this.make;

}

public void setModel(String model)

{

this.model = model;

}

public String getModel()

{

return this.model;

}

}