This is probably a stupid mistake but I can't see the issue, it's with the if statement at the bottom:

/**
 * Write a description of class Order here.
 * 
 * @author David Taylor
 * @version 1.0
 */
import javax.swing.*;
import java.text.DecimalFormat;
public class Client
{
    // instance variables - replace the example below with your own
    private String surname;
    private double height;
    private double weight;
    private int age;
    private boolean smoker;
    

    /**
     * Constructor for objects of class Client
     */
    public Client()
    {
        surname="";
        height= 0;
        weight=0;
        age=0;
        smoker = false;     
    }

    public Client(String name, double personHeight, double personWeight, int personAge, boolean isSmoker)
    {
        surname = name;
        height = personHeight;
        weight = personWeight;
        age = personAge;
        smoker = isSmoker;
        
    }
    /**
     * Getter methods for class Client
     */
    public String getSurname()
    {
        return surname;
    }
    
    public double getHeight()
    {
        return height;
    }
    
    public double getWeight()
    {
        return weight;
    }
    
    public int getAge()
    {
        return age;
    }
    
    public boolean getSmoker()
    {
        return smoker;
    }
    /**
     * Setter methods for class Client
     */
    public void setHeight(double personHeight)
    {
        height = personHeight;
    }
    
    public void setWeight(double personWeight)
    {
        weight = personWeight;
    }
    
    public void setAge(int personAge)
    {
        age = personAge;
    }
    
    public void setSmoker(boolean isSmoker)
    {
        smoker = isSmoker;
    }
    /**
     * Calculate BMI and into Weight
     */    
    public double calcBMI()
    {
        double BMI;
        BMI = weight / (height * height);
        return BMI;
    }
    /**
     * Calculate age group
     */
    public String calcAge()
    {
    if (age >=60) {
            return "senior";
        }
        else if (age >18) {
            return "adult";
        }
        else if (age <18) {
           return "junior";
        }
    }  
}

Recommended Answers

All 9 Replies

What is the return value if the age is 18?

Either make the last one a simple "else", not an "else if" so the compiler is able to ensure that the method will always return something.

When I do that it flags the error "not a statement" it won't let me compile it either way, "else if" or "else".

/**
     * Calculate age group
     */
    public String calcAge()
    {
    if (age >=60) {
       return "senior";
    }
    else if (age >18) {
              return "adult";
           }
           else if (age <18) {
                     return "junior";
                  }
    }

Your function returns nothing if the age == 18
Remove if (age < 18)
or add = to one of the last 2 if statements
else if (age >= 18){
//...

cheers bud, I just got it working before you posted lol however onto another problem now, double cannot be dereferenced:

public double calcBMI()
    {
        double BMI;
        BMI = weight / (height * height);
        return BMI;
        
       if (BMI >=30) {
            return BMI.toString + "Obese";
        }
        else if (BMI >=25) {
            return BMI.toString + "Overweight";
        }
        else if (BMI >=18.5) {
            return BMI.toString + "Normal Weight";
        }
        else {
            return BMI.toString + "Underweight";
        }
    
    }

You can't use BMI.toString ... because BMI is a double (primitive data type)
use Double.toString(BMI) instead. or just BMI (I think :) )

You can't use BMI.toString ... because BMI is a double (primitive data type)
use Double.toString(BMI) instead. or just BMI (I think :) )

If I do that it for double.toString(BMI) I get the error:
class expected
for Double.toString(BMI) I get:
incompatible types.

Oh yes, my bad, sorry for that.

Ok then, but what's with BMI.toString statement, don't you get an error there?

leave it just BMI, without toString, java will convert the value to a string and append the string that comes after the + sign

and more, your method should return a double, your if statements are returning strings ... anyway you wont get there since the method ends at this line:

BMI = weight / (height * height);
return BMI; // exit here

Oh yes, my bad, sorry for that.

Ok then, but what's with BMI.toString statement, don't you get an error there?

leave it just BMI, without toString, java will convert the value to a string and append the string that comes after the + sign

and more, your method should return a double, your if statements are returning strings ... anyway you wont get there since the method ends at this line:

BMI = weight / (height * height);
return BMI; // exit here

I'm using blueJ so it allows me to call just that double to calculate the BMI. Can you rewrite the code if possible to show me how it's meant to be done, as I am seriously confused lol.

I'm using blueJ so it allows me to call just that double to calculate the BMI. Can you rewrite the code if possible to show me how it's meant to be done, as I am seriously confused lol.

I'm not sure what you are trying to do, but a method can return only 1 type, in your case double or string. You have to decide what your method needs to return
1. a double value... then:

public double calcBMI(){
        double BMI;
        BMI = weight / (height * height);
        return BMI; // returns a double value
}

2. a string:

public String calcMBI(){
        double BMI;
        BMI = weight / (height * height);
        // no return statement
        if (BMI >=30) {
                return BMI + "Obese"; // returns a string
        }
        else if (BMI >=25) {
                        return BMI + "Overweight"; // returns a string
                }
                else if (BMI >=18.5) {
                                return BMI + "Normal Weight"; // returns a string
                        }
                        else {
                                return BMI + "Underweight"; // returns a string
                        }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.