I am beginning to learn JAVA. I was asked to create a Car Program that tracks new and used cars. I am supposed to create a super class called car, two derived classes called UsedCar and NewCar, and a Driver class that tests those 3 classes.
All the classes compile and run. However. When I output it, I get garbage output. I don't understand where I went wrong. I know the Driver class is fine, and the also the super "Car" class. Somewhere in the UsedCar and NewCar classes it is causing the output to be wrong. Any advice or suggestions would be helpful.
Here is my driver class:
public class CarDriver
{
public static void main(String[] args)
{
NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");
if (new1.equals(new2))
{
new1.display();
}
UsedCar used1 = new UsedCar(2500, 100000);
UsedCar used2 = new UsedCar(2500, 100000);
if (used1.equals(used2))
{
used1.display();
}
} // end main
}//end class
Here is my Car Class:
import java.util.*;
public class Car
{
//Variables
public Double price;
//Constructor
public Car(Double cost)//constructor to create instances of SavingsAccount
{
price = cost *2;
}
//GetPrice method
public Double getPrice()//method to get the cars' price
{
return price;//returns the value of the price
}
}//end class Car
Here are the derived classes: NewCar
import java.util.*;
public class NewCar extends Car
{
//Variables
public String color = "silver";
NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");
//Constructor - Two Parameter
public NewCar (Double price, String color)//constructor to create instances of new car
{
super(price);
color = this.color;
}
//Equals Method
public boolean equals(Car NewCar)
{
if (NewCar == null)
{
return false;
}
else
{
return
price.equals(new1.price) &&
color.equals(new2.color);
}
} // end equals
//Display method
public void display ()
{
System.out.println(" " + new1.price + new1.color);
}//end display method
}//end class NewCar
UsedCar
import java.util.*;
public class UsedCar extends Car
{
//Variables
private double mileage;
public String color = "silver";
UsedCar used1 = new UsedCar(2500, 100000);
UsedCar used2 = new UsedCar(2500, 100000);
//Constructor -Two Parameter
public UsedCar (double price, double mileage)//constructor to create instances of new car
{
super(price);
mileage = this.mileage;
}
//Equals Method
public boolean equals(Car UsedCar)
{
if (UsedCar == null)
{
return false;
}
else
{
return
price.equals(used1.price) &&
color.equals(used2.color);
}
} // end equals
//Display method
public void display ()
{
System.out.println(" " + used1.price + used1.mileage);
}//end display
}//end class
I can't paste my output, but it looks like this on the command line and it continues nonstop:
"at NewCar .(NewCar. java:11)"
In the class NewCar and UsedCar you make one recursive class.
Caused by error.
Remove the:
NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver")
and
UsedCar used1 = new UsedCar(2500, 100000);
UsedCar used2 = new UsedCar(2500, 100000);
In your costructor you used
color = this.color;
when in fact you should used
this.color = color.
In the method equals you implemented so incorect.
It is correct:
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
NewCar other = (NewCar) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
Correct Class
Car.java
public class Car {
private Double price;
private String color;
public Car(String color, Double cost) {
this.color = color;
this.price = cost * 2;
}
public Double getPrice() {
return price;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
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 (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
return true;
}
@Override
public String toString() {
return "Car: \nColor:" + color + "\nPrice: " + price;
}
public void display() {
System.out.println(toString());
}
}
NewCar.java
public class NewCar extends Car {
private String color = "silver";
public NewCar(String color, Double coast) {
super(color, coast);
this.color = color;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((color == null) ? 0 : color.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
NewCar other = (NewCar) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
@Override
public String toString() {
return super.toString() + "\nType: New\nMileage:0\n";
}
}
UsedCar.java
public class UsedCar extends Car {
private double mileage;
private String color = "silver";
public UsedCar(String color, double price, double mileage) {
super(color, price);
this.mileage = mileage;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((color == null) ? 0 : color.hashCode());
long temp;
temp = Double.doubleToLongBits(mileage);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
UsedCar other = (UsedCar) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (Double.doubleToLongBits(mileage) != Double.doubleToLongBits(other.mileage))
return false;
return true;
}
@Override
public String toString() {
return super.toString() + "\nType: Used\nMileage: " + mileage + "\n";
}
}
CarDriver.java
public class CarDriver {
public static void main(String[] args) {
Car new1 = new NewCar("silver", 8000.33);
Car new2 = new NewCar("silver", 8000.33);
if (new1.equals(new2)) {
new1.display();
}
Car used1 = new UsedCar("silver", 2500, 100000);
Car used2 = new UsedCar("silver", 2500, 100000);
if (used1.equals(used2)) {
used1.display();
}
}
}