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

Programming Exercises from chap 11 (q3 pg.575) Write a class named \'Car with da

ID: 3917662 • Letter: P

Question

Programming Exercises from chap 11 (q3 pg.575) Write a class named 'Car with data attributes: make, model, color and price initialize the value of price to integer. Also, implement a method car type which returns a string value 'new. Next, write a class named ElectricCar' that is subclass of 'Car' class. The ElectricCar' class should have a float data attribute for Mileage, and a Boolean data attribute indicating whether the Engine has Transmission or not. Override the price attribute and initialize it to a new integer value also override the car type method which should return a string value 'electric'. Once you have written the classes, write the main program that prompts the user to enter data for each of the ElectricCar object's data attributes and create multiple object of the ElectricCar class. The number of objects to be created is determined by the user. Store the data in the object, then use the object's accessor methods to retrieve each of the object. Also, try to retrieve the overridden attribute and method by using the object and display them on the screen in a tabular form Sample Output: nter the make: Honda nter the model: Civic nter the color: Blue nter the mileage: 202 s the car of transmission type Automatic ?(Yes/No) Yes ar information: lake: Honda odel: Civic olor: Blue ileage: 202.0 ransmission Type: True ar Condition: 80000 ar Type:Electric

Explanation / Answer

/**

*

*/

package chegg;

/**

* @author COREi5

*

*/

public class Car {

private String make;

private String model;

private String color;

private int price;

public String getMake() {

return make;

}

public void setMake(String make) {

this.make = make;

}

public String getModel() {

return model;

}

public void setModel(String model) {

this.model = model;

}

public String getColor() {

return color;

}

public void setColor(String color) {

this.color = color;

}

public int getPrice() {

return price;

}

public void setPrice(int price) {

this.price = 10000;

}

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result + ((color == null) ? 0 : color.hashCode());

result = prime * result + ((make == null) ? 0 : make.hashCode());

result = prime * result + ((model == null) ? 0 : model.hashCode());

result = prime * result + price;

return result;

}

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Car other = (Car) obj;

if (color == null) {

if (other.color != null)

return false;

} else if (!color.equals(other.color))

return false;

if (make == null) {

if (other.make != null)

return false;

} else if (!make.equals(other.make))

return false;

if (model == null) {

if (other.model != null)

return false;

} else if (!model.equals(other.model))

return false;

if (price != other.price)

return false;

return true;

}

@Override

public String toString() {

return "Car [make=" + make + ", model=" + model + ", color=" + color

+ ", price=" + price + "]";

}

public Car(String make, String model, String color, int price) {

super();

this.make = make;

this.model = model;

this.color = color;

this.price = price;

}

public Car() {

this.make = "BMW";

this.model = "Q5";

this.color = "Black";

this.price = 100000;

}

public String cartype() {

return "New";

}

}

************************************************

/**

*

*/

package chegg;

/**

* @author COREi5

*

*/

public class ElectricCar extends Car {

private float Mileage;

private boolean engineTransmission;

private int price;

/**

* @return the price

*/

public int getPrice() {

return price;

}

/**

* @param price

* the price to set

*/

public void setPrice(int price) {

this.price = 18000;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#hashCode()

*/

@Override

public int hashCode() {

final int prime = 31;

int result = super.hashCode();

result = prime * result + Float.floatToIntBits(Mileage);

result = prime * result + (engineTransmission ? 1231 : 1237);

return result;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (!super.equals(obj))

return false;

if (getClass() != obj.getClass())

return false;

ElectricCar other = (ElectricCar) obj;

if (Float.floatToIntBits(Mileage) != Float

.floatToIntBits(other.Mileage))

return false;

if (engineTransmission != other.engineTransmission)

return false;

return true;

}

/**

* @param make

* @param model

* @param color

* @param price

* @param mileage

* @param engineTransmission

*/

public ElectricCar(String make, String model, String color, int price,

float mileage, boolean engineTransmission) {

super(make, model, color, price);

Mileage = mileage;

this.engineTransmission = engineTransmission;

}

public ElectricCar() {

super();

}

/**

* @return the mileage

*/

public float getMileage() {

return Mileage;

}

/**

* @param mileage

* the mileage to set

*/

public void setMileage(float mileage) {

Mileage = mileage;

}

/**

* @return the engineTransmission

*/

public boolean isEngineTransmission() {

return engineTransmission;

}

/**

* @param engineTransmission

* the engineTransmission to set

*/

public void setEngineTransmission(boolean engineTransmission) {

this.engineTransmission = engineTransmission;

}

public String cartype() {

return "electric";

}

}

***********************************************

/**

*

*/

package chegg;

import java.util.ArrayList;

import java.util.Scanner;

/**

* @author COREi5

*

*/

public class Main {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ArrayList<ElectricCar> arrayList = new ArrayList<ElectricCar>();

Scanner in = new Scanner(System.in);

System.out.println("Enter the number of Objects??");

int numOfCarDetails = in.nextInt();

System.out.println("**************Car Details**************");

for (int i = 0; i < numOfCarDetails; i++) {

String carType;

boolean tType;

int price;

System.out.println("Enter " + (i + 1) + "Car Details");

System.out.println("Enter the make : ");

String make = in.next();

System.out.println("Enter the model : ");

String model = in.next();

System.out.println("Enter the color : ");

String color = in.next();

System.out.println("Enter the Mileage : ");

float mileage = in.nextFloat();

System.out

.println("Is the car of transmission type Automatic ?(Yes/No) ");

String tranmissionType = in.next();

if (tranmissionType.equals("Yes")) {

tType = true;

ElectricCar car = new ElectricCar();

carType = car.cartype();

price = car.getPrice();

} else {

tType = false;

Car car = new Car();

carType = car.cartype();

price = car.getPrice();

}

ElectricCar car = new ElectricCar();

car.setMake(make);

car.setPrice(price);

car.setColor(color);

car.setModel(model);

car.setMileage(mileage);

car.setEngineTransmission(tType);

arrayList.add(car);

}

System.out.println("Car Information :");

for (ElectricCar electricCar : arrayList) {

System.out.println("Make : " + electricCar.getMake());

System.out.println("Model : " + electricCar.getModel());

System.out.println("Color : " + electricCar.getColor());

System.out.println("Mileage : " + electricCar.getMileage());

System.out.println("Car Condition : " + electricCar.getPrice());

System.out.println("Car Type : " + electricCar.cartype());

}

}

}