0

Towards the bottom of this code I am getting an "unreachable statement" error. I have tried a few things, but cannot figure out why this is happening. The error is towards the bottom of the code (I have commented with // where the error is) Please help point me in the right direction I'm stumped!

/**
* Describes a certain model.
* 
* @author (Joshua Baker) 
* @version (1.0)
*/
public class Model
{
public static final int IN_PER_FOOT = 12;
public static final int BASE_RATE = 60;
public static final int TALL_INCHES = 67;
public static final double THIN_POUNDS = 140.0;
public static final int TALL_THIN_BONUS = 5;
public static final int TRAVEL_BONUS = 4;
public static final int SMOKER_DEDUCTION = 10;

private String firstName;
private String lastName;
private int heightInInches;
private double weightInPounds;
private boolean travel;
private boolean smokes;
private String newHeight;
private int perHourRate;

/**
 * Default constructor
 */
public Model()
{
    setFirstName ("");
    setLastName  ("");
    setHeightInInches  (0);
    setWeightInPounds  (0.0);
    setTravel  (false);
    setSmokes  (false);
}

/**
 * 
 */
public Model (String whatIsFirstName, String whatIsLastName, int whatIsHeight, double whatIsWeight,
boolean canTravel, boolean smoker)
{
    setFirstName  (whatIsFirstName);
    setLastName  (whatIsLastName);
    setHeightInInches  (whatIsHeight);
    setWeightInPounds  (whatIsWeight);
    setTravel  (canTravel);
    setSmokes  (smoker);
}

/**
 *@return first name
 */
public String getFirstName()
{
    return firstName;
}

/**
 *@return last name
 */
public String getLastName()
{
    return lastName;
}

/**
 *@return height in inches
 */
public int getHeightInInches()
{
    return heightInInches;
}

/**
 *@return the converted height 
 */
public String getNewHeight()
{
    return newHeight;
}

/**
 *@return weight in pounds
 */
public double getWeightInPounds()
{
    return weightInPounds;
}

/**
 *@return models pay per hour rate
 */
public int getPerHourRate()
{
    return perHourRate;
}


/**
 *@return travel
 */
public boolean getTravel()
{
    return travel;
}

/**
 *@return smokes
 */
public boolean getSmokes()
{
    return smokes;
}

/**
 * models first name
 */
public void setFirstName(String whatIsFirstName)
{
    firstName = whatIsFirstName;
}

 /**
 * models last name
 */
public void setLastName(String whatIsLastName)
{
    lastName = whatIsLastName;
}

 /**
 * models height in inches
 */
public void setHeightInInches(int whatIsHeight)
{
    if (whatIsHeight >0){
    heightInInches = whatIsHeight;
    }

}

 /**
 * models weight in pounds
 */
public void setWeightInPounds(double whatIsWeight)
{
    if (whatIsWeight >0){
    weightInPounds = whatIsWeight;
    }
}

 /**
 * can model travel
 */
public void setTravel(boolean canTravel)
{
    travel = canTravel;
}

 /**
 * does model smoke
 */
public void setSmokes(boolean smoker)
{
    smokes = smoker;
}

/**
 * Converts to feet and inches
 */
public String convertheightToFeetInches() 
{
int leftOver = (heightInInches %= IN_PER_FOOT);
int newHeight = (heightInInches % IN_PER_FOOT);
return newHeight + "Foot" + leftOver + "Inches";
}

/**
 * 
 */
public int calculatePayPerHour(){
    if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
        perHourRate = BASE_RATE + TALL_THIN_BONUS;
        return perHourRate;
    }
    else
    {
        perHourRate = BASE_RATE;
        return perHourRate;
    }

    if (travel)  {          //unreachable statement


        perHourRate = BASE_RATE + TRAVEL_BONUS;   
        return perHourRate;
    }
    else
    {
        perHourRate = BASE_RATE;
        return perHourRate;

    }

    if (smokes) {             //unreachable statement
        perHourRate = BASE_RATE - SMOKER_DEDUCTION;
        return perHourRate;
    }

    else {}

    }






/**
 * Displays details
 */
public  void displayInfo()
{
    System.out.print("Name : " + getFirstName() + " ");
    System.out.println(getLastName());
    System.out.println("Height : " + getNewHeight() + "inches");
    System.out.println("Weight : " + getWeightInPounds() + "pounds");
    System.out.print("Travel : " + getTravel() + " " );
    System.out.print("Smokes : " + getSmokes() );
    System.out.println("Hourly rate : " + getPerHourRate() );
}

}

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Joshua Baker
  • 401
  • 1
  • 7
  • 15
  • Next time, please just add the code that has the issue. This is a lot of code to scroll through to find a single comment buried in a single method. – Wonko the Sane Feb 08 '13 at 15:13

5 Answers5

11

That is because your program will return from either your first if block or the corresponding else block: -

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate;
}
else
{
    perHourRate = BASE_RATE;
    return perHourRate;
}
System.out.println("This will never get printed. And will show compiler error");

So, either of the two return statement will be executed. And hence any further code is unreachable.


Seems that you want to have cumulative sum of all the service rates to get the final perHourRate, for that, you can remove the return statement from each of the if-else block. And then for all the if-else block after the first one, instead of assigning the current price to perHourRate, do a compound addition +=.

Also, since you are working on the instance field - perHourRate, you don't need to return it at all. The changes you did on perHourRate can be obtained using getPerHourRate(). So, change the return type to void.

May you can try updating your calculatePayPerHour method to the one below:

public void calculatePayPerHour(){
    if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
        perHourRate = BASE_RATE + TALL_THIN_BONUS;  // Initial assignment

    } else {
        perHourRate = BASE_RATE;  // Initial assignment
    }

    /** Rest of the assignment will be compound assignment, since you
        are now updating the `perHourRate` **/

    if (travel)  {      
        perHourRate += TRAVEL_BONUS;   
    } // You don't need an else now. Since BASE_RATE is already added

    if (smokes) {    
        perHourRate -= SMOKER_DEDUCTION;
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
3

Inside your calculatePayPerHour method you have an if/else and the statement will never be reached because in both cases you are returning a result:

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate; // you return in the if
}
else
{
    perHourRate = BASE_RATE;
    return perHourRate; // you return in the else
}

... the execution will never reach here
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3
if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
    return perHourRate; // <------------
}
else
{
    perHourRate = BASE_RATE;
    return perHourRate;  // <------------
}

Regardless of your height or weight, one of these 2 returns will trigger, so any statement after it will never be executed. This is identical to the below code.

if (heightInInches  >= TALL_INCHES && (weightInPounds <= THIN_POUNDS)) {
    perHourRate = BASE_RATE + TALL_THIN_BONUS;
}
else
{
    perHourRate = BASE_RATE;
}

return perHourRate;  
//unreachable
Karthik T
  • 31,456
  • 5
  • 68
  • 87
2

in the if/else above the code you have 2 return statements already... This means it will never reach the code below this..

Spons
  • 1,593
  • 1
  • 17
  • 46
2
  • In your method public int calculatePayPerHour() the first if else statement is returning a value in any case (in both if and else block).
  • If you dry run the program you will see that the control flow will never reach statements below this block hence your exception.
Shurmajee
  • 1,027
  • 3
  • 12
  • 35