1

I have three classes. Class Vehicle, class Truck and class Car. Class Truck and class Car extend from class Vehicle. class Vehicle has three fields:

protected String model;
protected int year;
protected int price;

Class Truck has one field:

private String color; 

and the method:

public String getColor(){
    return color; 
  }

Class Car has one field:

private boolean rusty;

I have an array called vehicles(which stores 30 objects) to store Truck objects as well as Car objects.

I have another class called VehicleDealer in which I have created the array and different methods including:

 int VEHICLE_COUNTER= 0;

public void addTruck(String m, int y, int p,String c){
    Vehicle truck1 = new Truck( m, y, p, c);
    vehicles[VEHICLE_COUNTER] = truck1; 
    VEHICLE_COUNTER = VEHICLE_COUNTER + 1;
}

public void addCar(String m, int y, int p,boolean r){
    Vehicle car1 = new Car( m, y, p, r);
    vehicles[VEHICLE_COUNTER] = car1; 
    VEHICLE_COUNTER = VEHICLE_COUNTER + 1;
}

 public String printAllVehiclesOfColor(String c){

    String s = "";
    for (int i=0; i < VEHICLE_COUNTER; i++) {
         String x = vehicles[i].getColor();
       if(x == c){
           s += vehicles[i].printThis(i) + "\n";
} 
        }
        return s;
} 

I have a main class which looks like this:

public class TestDealer{
public static void main(String[] args){

    VehicleDealer dealer = new VehicleDealer();

dealer.addTruck("Ford", 2012, 55000, "Green");
dealer.addTruck("Ford", 2001, 23050, "Black");
dealer.addCar("BMW", 2010, 17000, false);

System.out.println(dealer.printAllVehiclesOfColor("Green"));

    }
}

In this line of code I get an error which says that cannot find symbol for getColor().

for (int i=0; i < VEHICLE_COUNTER; i++) {
         String x = vehicles[i].getColor();<---------------
       if(x == c){
           s += vehicles[i].printThis(i) + "\n";
} 
        }

So I put the getColor() method in class Vehicle and then tried to compile the code but it did not work as I have not declared a color field in class Car. My question requires me to declare the color field in class Truck only.

What is wrong with my code? I am new to Java. Any help is appreciated. Thank you!!

vivi s
  • 11
  • 1
  • 1
    This is definitely a different question than the one in "marked as duplicate". – jHilscher Feb 06 '15 at 18:26
  • @Eran I don't understand. I replaced == with .equals() but I am still getting an error :-( – vivi s Feb 06 '15 at 18:28
  • 1
    Your array has the type Vehicles (your base class), but that class does not have the getColor method. Move the method to the base class. – Koshinae Feb 06 '15 at 18:28
  • @Koshinae I did but class Vehicle does not hve the field color and so I am getting an error again. I don't want to put the color field in class Vehicle. – vivi s Feb 06 '15 at 18:30
  • I forgot it to say, also move the field `color` to Vehicle. (The "private String color;") – Koshinae Feb 06 '15 at 18:31
  • Then you can't use the method on an array with vehicles. Only on an array of trucks, – jHilscher Feb 06 '15 at 18:32
  • @Koshinae My question says that put field color in class Truck and not in class Vehicle. That's the problem here. – vivi s Feb 06 '15 at 18:33
  • @jHilscher Exactly! That's what I am trying to do. But its not working. – vivi s Feb 06 '15 at 18:33
  • Your array of Vehicles can contain Cars and Trucks. You can't call getColor, as Vehicle does not have getColor, only Car. You can only call methods that are in Vehicles, evne if you had put Cars in there. – Koshinae Feb 06 '15 at 18:35
  • @Koshinae So this method won't work? I need to print all the objects of classCar with color green. Is there any other way to do it? – vivi s Feb 06 '15 at 18:39
  • If you only care for the Trucks (has color), and can skip the Cars, then you can get the color by: if (vehicles[i] instanceof Truck) {s += ((Truck) vehicles[i]).getColor() + "\n";} – Koshinae Feb 06 '15 at 18:42
  • @Koshinae It's not working. What do you mean by: if (vehicles[i] instanceof Truck) – vivi s Feb 06 '15 at 18:54
  • @Koshinae I got it! I just changed the getColor() to toString() and it compiled perfectly! – vivi s Feb 06 '15 at 19:47

0 Answers0