Write an application program CarTester that does the following: Creates three in
ID: 3681748 • Letter: W
Question
Write an application program CarTester that does the following:
Creates three instances of Car, pass the following values to the constructor
first car object – 1HGBH42JXMN109186, true, 34675
second car object – JM1NA3535V0729833, false, 87211
third car object – JM1NA3535V0729833, false, 87211
Uses the equals method to determine if two Car objects are the same.
Compare first Car object to second Car object, and second Car object to third Car object.
Print the following after comparing the Car objects:
If the cars are the same:
Found the car!
VIN: 1HGBH42JXMN109186
Anti-theft: Yes
Mileage: 34675
If the cars are different:
Cars are not the same!
First car:
VIN: 1HGBH42JXMN109186
Anti-theft: Yes
Mileage: 34675
Second car:
VIN: JM1NA3535V0729833
Anti-theft: No
Mileage: 87211
To Be Submitted
Explanation / Answer
Solution:
package com.nancy.chegg.qa;
public class Car {
private String VIN;
private boolean antiTheft;
private int mileage;
public Car(String vin, boolean antiTheft, int mileage) {
super();
this.VIN = vin;
this.antiTheft = antiTheft;
this.mileage = mileage;
}
public String getVIN() {
return VIN;
}
public boolean isAntiTheft() {
return antiTheft;
}
public int getMileage() {
return mileage;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((VIN == null) ? 0 : VIN.hashCode());
result = prime * result + (antiTheft ? 1231 : 1237);
result = prime * result + mileage;
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 (VIN == null) {
if (other.VIN != null)
return false;
} else if (!VIN.equals(other.VIN))
return false;
if (antiTheft != other.antiTheft)
return false;
if (mileage != other.mileage)
return false;
return true;
}
}
____________________________________________
package com.nancy.chegg.qa;
public class CarTester {
public static void main(String[] args) {
Car c1 = new Car("1HGBH42JXMN109186", true, 34675);
Car c2 = new Car("JM1NA3535V0729833", false, 87211);
Car c3 = new Car("JM1NA3535V0729833", false, 87211);
if (c1.equals(c2)) {
System.out.println("Found the car!");
System.out.println("VIN : " + c1.getVIN());
System.out.println("Anti-theft : " + c1.isAntiTheft());
System.out.println("Mileage : " + c1.getMileage());
} else {
System.out.println(" Cars are not the same!");
System.out.println(" First Car : ");
System.out.println("VIN : " + c1.getVIN());
System.out.println("Anti-theft : " + c1.isAntiTheft());
System.out.println("Mileage : " + c1.getMileage());
System.out.println(" Second Car : ");
System.out.println("VIN : " + c2.getVIN());
System.out.println("Anti-theft : " + c2.isAntiTheft());
System.out.println("Mileage : " + c2.getMileage());
}
System.out.println("___________________________________________");
if (c2.equals(c3)) {
System.out.println(" Found the car!");
System.out.println("VIN : " + c2.getVIN());
System.out.println("Anti-theft : " + c2.isAntiTheft());
System.out.println("Mileage : " + c2.getMileage());
} else {
System.out.println("Cars are not the same!");
System.out.println(" First Car : ");
System.out.println("VIN : " + c2.getVIN());
System.out.println("Anti-theft : " + c2.isAntiTheft());
System.out.println("Mileage : " + c2.getMileage());
System.out.println(" Second Car : ");
System.out.println("VIN : " + c3.getVIN());
System.out.println("Anti-theft : " + c3.isAntiTheft());
System.out.println("Mileage : " + c3.getMileage());
}
}
}
--------------------------------------------------------------------------------------------
sample run:
--------------------------------------------------------------------------------------------
Cars are not the same!
First Car :
VIN : 1HGBH42JXMN109186
Anti-theft : true
Mileage : 34675
Second Car :
VIN : JM1NA3535V0729833
Anti-theft : false
Mileage : 87211
___________________________________________
Found the car!
VIN : JM1NA3535V0729833
Anti-theft : false
Mileage : 87211
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.