machinamonster
machinamonster

Reputation: 17

Incompatible Types error in reuturn giving me error?

So I have been trying to work on this code for a while, but two of my return functions refuse to work properly. I know I am not returning the right type, but I have absolutely no idea what to return in place of it, here is the code: The errors are on line 53 and 58. What code would I need to properly return the car and truck?

public class lab21composition
{
public static void main(String args[])
{
    CarFactory factory = new CarFactory ("Ford");
    Garage myGarage = new Garage();

    Car c = factory.produceCar("Fusion");
    Truck t = factory.produceTruck("F150");

    System.out.println(myGarage);

    myGarage.addVehicle(c);
    System.out.println(myGarage);

    myGarage.addVehicle(t);
    System.out.println(myGarage);

    Vehicle v = myGarage.removeVehicle();
    if (null !=v)
    {
        System.out.println(v.toString() + " was removed from garage.");
        System.out.println(myGarage);
    } 
        else
        {
            System.out.println("There was no vehicle in the garage to remove.");
        }

        myGarage.addVehicle(t);
        System.out.println(myGarage);

        CarFactory factory2 = new CarFactory ("Honda");
        Garage myGarage2 = new Garage();
        Car d = factory.produceCar("Odyssy");
        myGarage.addVehicle(d);
        System.out.println(myGarage2);

}
}

class CarFactory
{
private String name;

public CarFactory(String n)
{
    name = n; //ERROR, incompatible types/ Required:Truck/ Found: java.lang.String
}

public Car produceCar(String model)
{
    return name;  //ERROR, incompatible Types/ Required:Truck/Found: java.lang.String
}

public Truck produceTruck(String model)
{
    return name;
}
}

class Vehicle
{
public static String make, model;

public Vehicle()
{
    make = "Undefined";
    model = "Undefined";
}

public Vehicle(String _make, String _model)
{
    make = _make;
    model = _model;
}

public String toString()
{
    return make+" "+model;
}

}

class Car extends Vehicle
{
public Car()
{
    super (make,model);
}   
}

class Truck extends Vehicle
{
public Truck()
{
super (make,model);
}
}

class Garage
{
// Define a private variable that holds a Vehicle object. This will represent the vehicle being stored in the Garage. If the garage is empty, then this variable 
// should be null
private Vehicle Veh;

public void addVehicle(Vehicle v)
{
    if(v==Veh) /* replace FALSE with code to check if v is the same as vehicle */
    {
        //HINT: use a function inherited from the Object class!
        System.out.println(v.toString() + " is already parked in this garage");
    }
        else if (hasVehicle())
        {
            System.out.println("This garage is full!");
        }
        else
        {
            Veh = v;
            //store the vehicle that was passed to this function
            //in this class vehicle attribute
        }
}

public Vehicle removeVehicle()
{
    //store this class vehicle attribute in a temporary variable
    // set this class vehicle attribute to null
    // return the vehicle stored in the temporary variable
    return null;
}

public boolean hasVehicle()
{
    //change this return statement so that it
    //returns an appropriate boolean value
    return false;
}

public String toString()
{
    if (hasVehicle())
    {
        //replace ??? with the toString() method of the vehicle
        // that is in this garage
        return "This garage has a"+toString()+"in it!";
    }
    else
    {
        return "This garage is empty.";
    }
}

}

Upvotes: 0

Views: 483

Answers (2)

laune
laune

Reputation: 31300

You create a CarFactory for some specific brand, e.g. "Ford". You want to build certain models of Car or Truck. For this you call the constructor of Car and Truck, respectively.

class CarFactory {
    private String name; // brand
    public CarFactory(String n){
        name = n;
    }
    public Car produceCar(String model){
        return new Car( name, model );
    }
    public Truck produceTruck(String model){
        return new Truck( name, model );
    }
}

Car and Truck must have suitable constructors:

class Car extends Vehicle{
    public Car(String make, String model){
        super (make,model);
    }   
}

Same for Truck.

Upvotes: 1

Sbodd
Sbodd

Reputation: 11454

Your problems are here:

class CarFactory
{
private String name;
//snip    
public Car produceCar(String model)
{
    return name; 
}

public Truck produceTruck(String model)
{
    return name;
}
}

You declared CarFactory.produceCar to return a Car - but you're saying to return name; and name is of type String.

Upvotes: 2

Related Questions