Hello,
I have a class called Car and a main class that implements this class.
Both are pasted below.
I expect the implementation to print out the price of the car but it
is giving me price '0'.
Can anyone please advise as to what I am doing wrong.
Would be extremely thankful.
Ros
package InheritanceHW;
public class Car {
int speed;
int regularPrice;
String colour;
int getSalePrice(){
return regularPrice;
}
/** Creates a new instance of Car */
public Car(int regularPice, int speed, String colour) {
this.regularPrice = regularPrice;
this.colour = colour;
this.speed = speed;
}
}
package InheritanceHW;
public class MyOwnAutoShop {
public static void main(String args[]){
Car car1 = new Car(2003, 134, "Red");
//Sedan sedan1 = new Sedan(2134, 100, "blue", 13);
//Ford ford1 = new Ford(1970, 123, "Green", 1998, 400);
//Ford ford2 = new Ford(1980, 156, "White", 2001, 350);
System.out.println("The price of car1 is " +
car1.getSalePrice());
//System.out.println("The price of sedan1 is " +
sedan1.getSalePrice());
//System.out.println("The price of ford1 is " +
sedan1.getSalePrice());
//System.out.println("The price of ford2 is " +
sedan1.getSalePrice());
}
}
Ingo R. Homann - 09 May 2007 10:47 GMT
Hi,
> public Car(int regularPice, int speed, String colour) {
^^
> this.regularPrice = regularPrice;
Nice typo! Use an IDE like Eclipse - it would warn you on such errors!
Ciao,
Ingo
JB - 09 May 2007 10:49 GMT
A typo in the constructors parameter list leads to assigning the
instance variable to itself.
> public Car(int regularPice, int speed, String colour) {
> this.regularPrice = regularPrice;
Regards
Jens
ros - 09 May 2007 10:51 GMT
> A typo in the constructors parameter list leads to assigning the
> instance variable to itself.
[quoted text clipped - 4 lines]
> Regards
> Jens
Thanks a lot JB and Ingo. I think it's time I went to sleep!!
Cheers
ros