You have everything you need to solve this problem. Take some time and work through it step by step.
Using this method signature:
public void print(boolean kg)
We know that if kg is true you want to print out the kg weight. If kg is false you want to print out the lb weight. Simple as that.
So we can start with the case you already have. kg is false. Just print out pounds.
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
}
}
Now we have to handle the case where kg is true. This is slightly more complex, but if you go step by step again it isn't too difficult. Think about what you want to do.
If kg is true print out the kg weight.
if kg is true
Code
if (kg == true) {
//calculate kg weight
//print out kg weight
}
Now to calculate the weight in kg we multiply lbs by a conversion ratio. Roughly 0.45359237.
Code
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);
Put all of this together and we get the following method:
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
}
if (kg == true) {
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);
}
}
Now we can simplify this slightly by using an if/else statement
Code
public void print(boolean kg) {
if (kg == false) {
System.out.println(pounds);
} else {
double kgWeight = pounds * 0.45359237;
System.out.println(kgWeight);
}
}
CONTINUED...
This post was edited by waraholic on Feb 10 2018 01:03pm