-2

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)"

Fryguy
  • 17
  • 4

4 Answers4

1

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();
        }
    }
}
  • Oh wow. I understand what I did wrong in creating new objects in each class. I definitely need to go back and study this chapter more! Also, these concepts explained here are better than in the book. It now makes sense in overriding the equals method. – Fryguy Apr 16 '15 at 20:15
0

You create 2 additional NewCars for every NewCar instance you create

public class NewCar extends Car
{
// [...]

NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");

Those cars try to create 2 more NewCars which will try to create 2 additional NewCars, and so on. That will only work until you reach a certain level (you get a StackOverflow). You need to remove the initialisation of the 2 fields new1 and new2, if you want to avoid the exception.

You also have similar problem in UsedCar.

Also if you want to override the equals method, the signature should be public boolean equals(Object UsedCar) and not public boolean equals(Car UsedCar).

Tipp: Add the @Override annotation to a every method that should override a method in a superclass/interface and the compiler will tell you, if you got a signature wrong.


Also you may want to change the type of price from Double to double. You should only use Double instead of double if you know that you need it. Autoboxing and Unboxing could reduce your performance. See Autoboxing and Unboxing

fabian
  • 80,457
  • 12
  • 86
  • 114
0

I can see a couple of problems off the bat with NewCar.

First, you are instantiating (creating) two NewCars with the code

NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");

It's correct to create these in the main routine, as you have. It's probably not correct to create two new, local instances, within the class itself.

Second, your equals statement says equals(Car NewCar). My guess is that you want equals(Car otherCar). (Please note the lower case for an instance variable as a Java convention.)

Then in your return you would say something like

return (otherCar.getPrice() == this.getPrice()) && (otherCar.color.equalsIgnoreCase(this.color);
rajah9
  • 11,645
  • 5
  • 44
  • 57
0

Try removing these lines

NewCar new1 = new NewCar(8000.33, "silver");
NewCar new2 = new NewCar(8000.33, "silver");

from the NewCar class and these lines

UsedCar used1 = new UsedCar(2500, 100000);
UsedCar used2 = new UsedCar(2500, 100000);

from the UsedCar class. You're creating instances of those classes inside the classes themselves and I don't think that's what you want to be doing.

Also, your implementation of equals() isn't correct. For example, in NewCar you have

public boolean equals(Car NewCar)

Note how the name you have for your variable is actually the name of your class. You want, instead,

public boolean equals(Object obj)

Also, you should test that the object being passed is an instance of the appropriate Car subclass before you compare the variables. The same is true for the equals() in the UsedCar class.

fabian
  • 80,457
  • 12
  • 86
  • 114
wltrup
  • 778
  • 1
  • 4
  • 14