Write a class called Car. There should be three member variables called model, y
ID: 3789483 • Letter: W
Question
Write a class called Car. There should be three member variables called model, year and tank. The variable model is type String and year is integer and tank is double. Make sure to have getter and setter methods. Have a method called pumpGas that accepts a double value and add the value to the tank member variable. Note that tank can hold max gallons of 15. Have a method called display that displays the values in each member variable. In main creates 3 cars and use setter methods to place different values in each member variable. Write equals methods that override the equals method of Object class. Also write the toString method overriding the Object class. Make sure to use Override. Then display each car and check if they are equal using equals method. Do the following problem after Feb-7 Modify the above class Car so that it is comparable. Make sure to override the toString. There should be three member variables called model, year and tank. The variable model is type String and year is integer and tank is double. Make sure to have getter and setter methods. Have a method called pumpGas that accepts a double value and add the value to the tank member variable. Note that tank can hold max gallons of 15. In main creates 3 cars and compare them. Display the largest and smallest car.Explanation / Answer
Answer 1:
public class Car
{
private final double MAX_TANK = 15;
private String model;
private int year;
private double tank;
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public double getTank()
{
return tank;
}
public void setTank(double tank)
{
this.tank = tank;
}
public void pumpGas(double gas)
{
if(tank + gas <= MAX_TANK)
tank = tank + gas;
else
System.out.println("You can add only " + (MAX_TANK - tank) + " gallons of gas.");
}
public void display()
{
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Tank: " + tank + " gallons");
}
@Override
public boolean equals(Object otherObj)
{
Car otherCar = (Car) otherObj;
if (this.model.equals(otherCar.model) && this.year == otherCar.year && this.tank == otherCar.tank)
{
return true;
}
else
{
return false;
}
}
@Override
public String toString()
{
return "Model: " + model + ", Year: " + year + ", Tank: " + tank + " gallons";
}
public static void main(String[] args)
{
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
car1.setModel("Swift");
car1.setYear(2015);
car1.setTank(12);
car2.setModel("Honda City");
car2.setYear(2013);
car2.setTank(10);
car3.setModel("Swift");
car3.setYear(2015);
car3.setTank(12);
System.out.println("Car1...");
System.out.println(car1);
System.out.println(" Car2...");
System.out.println(car2);
System.out.println(" Car3...");
System.out.println(car3);
System.out.println();
if(car1.equals(car3))
System.out.println("Car1 is equal to Car3");
else
System.out.println("Car1 is not equal to Car3");
if(car1.equals(car2))
System.out.println("Car1 is equal to Car2");
else
System.out.println("Car1 is not equal to Car2");
}
}
Output 1:
Car1...
Model: Swift, Year: 2015, Tank: 12.0 gallons
Car2...
Model: Honda City, Year: 2013, Tank: 10.0 gallons
Car3...
Model: Swift, Year: 2015, Tank: 12.0 gallons
Car1 is equal to Car3
Car1 is not equal to Car2
Answer 2:
import java.util.Arrays;
public class Car implements Comparable
{
private final double MAX_TANK = 15;
private String model;
private int year;
private double tank;
public Car()
{
model = "";
year = 0;
tank = 0.0;
}
public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public int getYear()
{
return year;
}
public void setYear(int year)
{
this.year = year;
}
public double getTank()
{
return tank;
}
public void setTank(double tank)
{
this.tank = tank;
}
public void pumpGas(double gas)
{
if(tank + gas <= MAX_TANK)
tank = tank + gas;
else
System.out.println("You can add only " + (MAX_TANK - tank) + " gallons of gas.");
}
public void display()
{
System.out.println("Model: " + model);
System.out.println("Year: " + year);
System.out.println("Tank: " + tank + " gallons");
}
@Override
public boolean equals(Object otherObj)
{
Car otherCar = (Car) otherObj;
if (this.model.equals(otherCar.model) && this.year == otherCar.year && this.tank == otherCar.tank)
{
return true;
}
else
{
return false;
}
}
@Override
public String toString()
{
return "Model: " + model + ", Year: " + year + ", Tank: " + tank + " gallons";
}
@Override
public int compareTo(Object otherObj)
{
Car otherCar = (Car) otherObj;
if(this.tank < otherCar.tank)
return -1;
if(this.tank == otherCar.tank)
return 0;
else
return 1;
}
public static void main(String[] args)
{
Car car1 = new Car();
Car car2 = new Car();
Car car3 = new Car();
car1.setModel("Swift");
car1.setYear(2016);
car1.setTank(12);
car2.setModel("Honda City");
car2.setYear(2014);
car2.setTank(13);
car3.setModel("Amaze");
car3.setYear(2015);
car3.setTank(10);
Car[] cars = new Car[3];
cars[0] = car1;
cars[1] = car2;
cars[2] = car3;
for(int i = 0; i < 3; i++)
{
System.out.println("Car"+ (i + 1) +"...");
System.out.println(cars[i]);
System.out.println();
}
Arrays.sort(cars);
System.out.println("Smalles Car...");
cars[0].display();
System.out.println(" Largest Car...");
cars[cars.length - 1].display();
}
}
Output 2:
Car1...
Model: Swift, Year: 2016, Tank: 12.0 gallons
Car2...
Model: Honda City, Year: 2014, Tank: 13.0 gallons
Car3...
Model: Amaze, Year: 2015, Tank: 10.0 gallons
Smalles Car...
Model: Amaze
Year: 2015
Tank: 10.0 gallons
Largest Car...
Model: Honda City
Year: 2014
Tank: 13.0 gallons
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.