Search code examples
javaincompatibletypeerror

Need a fresh set of eyes with my Java code


I am hoping the community can provide me with a fresh set of eyes, as i have gone a bit code blind,

I have attached my code from both the classes that I have to connect together for a udemy tutorial.

As I compiled them, the usual errors came up, mis-spelled variables and missing semi colons. It also through up an unexpected type, which was solved by changing a int to a String.

The problem I have is on line 25, the error it gives me is

incompatible types, There was an expression of a certain type required here. You provided an expression of a different type hat is not compatible. (e.g. you wrote a String where an int was expected.)

But variable being called is declared a string as far as I can see in all instances.

I wrote it in intelliJ and used that to generate the getter/setter methods so these should all be correct, and I just can't for the life of me see where the error is coming from.

I know it will be something simple but just can't see the woods for the trees.

Car class.

public class Car
{
   // instance variables
   private int numOfMilesDone; // a car has-a number of miles drive, "20000"
   private int yearBought; // a car has-a year it was bought "1997"
   private int carValue;  // a car has-a value of what it is worth "300"
   private Model modelName; // a car has-an model of type Model

   /**
    * Constructor for objects of class Car
    */

   public Car(int aNumMiles, int aYearBought, int aValue, Model aModelName)
   {
      this.numOfMilesDone = aNumMiles;
      this.yearBought = aYearBought;
      this.carValue = aValue;
      this.modelName = aModelName;
   }

   public Model getModelName()
   {
      if (this.modelName == null || this.getModelName() == null )
      {
         return ("needs to be checked");
      }

      return modelName;
   }

   /**
    * Getter method for number of miles the car has done
    * @return
    */

   public int getNumOfMilesDone()
   {
      return numOfMilesDone;
   }

   /**
    * Setter method for number of miles the car has done
    * @return
    */

   public void setNumOfMilesDone(int numOfMilesDone)
   {
      this.numOfMilesDone = numOfMilesDone;
   }

   /**
    * Getter method for the year the car was bought
    * @return
    */

   public int getYearBought()
   {
      if (this.yearBought == null)
      {
         return "Needs to be checked";
      }

      return yearBought;
   }

   /**
    * Setter method for the year the car was bought
    * @return
    */

   public void setYearBought(int yearBought)
   {
      this.yearBought = yearBought;
   }

   /**
    * Getter method for the year the cars value in pounds
    * @return
    */

   public int getCarValue()
   {
      return carValue;
   }

   /**
    * Setter method for the year the cars value in pounds
    * @return
    */

   public void setCarValue(int carValue) {
      this.carValue = carValue;
   }

   public boolean isClassic()
   {
      return(Integer.parseInt(this.modelName.getYearOfModel()) < 1969);
   }

   /**
    * returns the a String describing the object
    * @return
    */

   public String toSting()
   {
      return this.getModelName() + " has done " + this.numOfMilesDone + ", it is worth " + this.carValue + ", it is " 
              + this.isClassic() + " it's a classic.";
   }

}

My other class, Model, this compiles no problem.

public class Model
{
    private String modelName; // the model has a model name
    private String yearOfModel; // the year the model was created


    /**
     * constructor method for no model attributes
     */

    public Model()
    {
        this.modelName = null;
        this.yearOfModel = null;
    }


    /**
     * constructor method for known modelName attribute, but no yearOfModel attribute
     * @param bModelName
     */

    public Model(String bModelName)
    {
        this.modelName = bModelName;
        this.yearOfModel = null;
    }

    /**
     * constructor method for known modelName attribute, and known yearOfModel attribute
     * @param bModelName
     * @param yearOfModel
     */

    public Model(String bModelName, String yearOfModel)
    {
        this.modelName = bModelName;
        this.yearOfModel = yearOfModel;
    }

    /**
     * modelName getter method
     * @return
     */

    public String getModelName() {
        return modelName;
    }

    /**
     * modelName setter method
     * @param modelName
     */

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    /**
     * yearOfModel setter method
     * @return
     */

    public String getYearOfModel() {
        return yearOfModel;
    }

    /**
     * yearOfModel setter method
     * @param yearOfModel
     */

    public void setYearOfModel(String yearOfModel) {
        this.yearOfModel = yearOfModel;
    }

    /**
     * returns the modelName and yearOfModel variables as comprehensible information.
     * @return
     */

    public String toString()
    {
        return this.modelName + " was launched in " + this.yearOfModel;
    }
}

Thanks in advance for your help.


Solution

  • return "Needs to be checked" you are returning a string when what your method signature suggests is a ModelName.