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