This method must return a result of type int

All is working fine apart from the last method getPrice() I am returning int but I keep getting the same error. Also if I set warranty to be false it still returns (base+((base/100)*10))

public class Machinery extends SaleGroup {

private float serial;
public int base;
private static boolean hasWarranty;

public Machinery(String newItemDescription, float newProductCode,
        float newSerial, int newBasePrice) {

    super(newItemDescription, newProductCode);
    serial = newSerial;
    base = newBasePrice;

}

public boolean IncludeWarranty() {
    return hasWarranty=true;
}

public boolean ExcludeWarranty() {
    return hasWarranty=false;
}

public float getSerial() {
    return serial;
}



public int getPrice()
    {
        if (hasWarranty==true)
        {
            return (base+((base/100)*10));
        } 
        else if (hasWarranty==false) 
        {
            return base;
        }
    }
}

I have 3 classes, SaleGroup.java, Machinery.java and MachineryTest.java

public abstract class SaleGroup {

     private String item;
     private float code;

     //Constructor with name and code parameters for specifying 
     //access methods that return the name and code
     public SaleGroup(String newItemDescription, float newProductCode)
     {
          item = newItemDescription;
          code = newProductCode;
     }

     public String getItemDescription() 
     {
          return item;
     }

     public float getProductCode() 
     {
          return code;
     }

     public abstract int getPrice();

     public String toString()
     {
          return "Item " + item + "has product code " + code + " and price is" + getPrice();
     }

}

MachineryTest.java

import javax.swing.JOptionPane;
public class MachineryTest {
    public static void main(String[] args) {
        String newItemDescription = "Item";
        float newSerial = 4234;
        float newProductCode = 3424;
        int newBasePrice = 1000;
        boolean hasWarranty=true;

        Machinery test1 = new Machinery(newItemDescription, newProductCode,
                newSerial, newBasePrice);
        JOptionPane.showMessageDialog(
                null,
                "Item: " + test1.getItemDescription() + " Serial: "
                        + test1.getSerial() + " Code: "
                        + test1.getProductCode() + " Warranty Included: "
                        + hasWarranty + " Price " + test1.getPrice());
    }
}

*Update: *

All is working fine apart from the last method getPrice() I am returning int but I keep getting the same error. Also if I set warranty to be false it still returns (base+((base/100)*10))

Jon Skeet
people
quotationmark

The problem is that the compiler won't work out that this:

if (hasWarranty == true) {
   ...
} else if (hasWarranty == false) {
   ...
}

will always execute exactly one of those paths. It thinks you might get to the end of the if statement without taking either brance, which means that you could get to the end of the method without returning anything. Indeed, it would be possible for hasWarranty to be false to start with, then another thread to change it to true before the second condition was executed.

You could just remove the second condition:

if (hasWarranty == true)  {
    ...
} else {
    ...
}

And you can also remove the comparison with boolean literals:

if (hasWarranty) {
    ...
} else {
    ...
}

You should also consider using a conditional operator:

public int getPrice() {
    return hasWarranty ? base + ((base/100) * 10) : base;
}

people

See more on this question at Stackoverflow