1. Member
Join Date
Feb 2013
Posts
5
Rep Power
0

## Unreachable statement???

Here is the program I'm trying to write:

Write a WeightConverter class. An instance of this class is created by passing the gravity of an object relative the the Earth's gravity. For example, the Moon's gravity is approximately 0.167 of the Earth's gravity, so we create a WeightConverter instance for the Moon as
WeightConverter moonWeight;
moonWeight = new Weight Converter(0.167);
To compute how much you weigh on the Moon, you pass your weight on Earth to the convert method as
yourMoonWeight = moonWeight.convert.160;

Use this class and redo Exercise 16: Write a program that inputs the user's Earth weight and outputs her or his weight on Mercury, Venus, Jupiter, and Saturn.
Mercury= 0.4
Venus= 0.9
Jupiter= 2.5
Saturn= 1.1

Code:

public class WeightConverter{

private double gravity;
private double weight;

public WeightConverter(int g, double w){
gravity= g;
g=1;
weight=w;
weight=0;
}

public double convertWeight(double w){
return 0.4*w;
return 0.9*w;
return 2.5*w;
return 1.1*w;
}
}

I keep on getting unreachable statement for the return statements. Am I on the right track...this is my third week starting Java (no programming experience beforehand). Thank you in advance.
Last edited by dangolf; 02-12-2013 at 04:33 AM.

2. ## Re: Unreachable statement???

db

* Need help with program
* Writing a program that adds 2 rational numbers (in the form of fractions) in BlueJ.

3. Member
Join Date
Feb 2013
Posts
5
Rep Power
0

## Re: Unreachable statement???

No I'm not a slow learner. I didn't perceive that I was doing anything incorrect. So what do you want me to do?

4. ## Re: Unreachable statement???

Originally Posted by dangolf
No I'm not a slow learner. I didn't perceive that I was doing anything incorrect. So what do you want me to do?
um... use code tags perhaps? Please check the forum FAQ, or the links in my signature below, or the multiple suggestions in your previous threads.

5. Member
Join Date
Feb 2013
Posts
3
Rep Power
0

## Re: Unreachable statement???

Firstly, constructor here has vague assignments. And for the unreachable code : once a return statement is encountered control is returned to the calling method and all the code after the return statement is unreachable.

6. Senior Member
Join Date
Oct 2010
Posts
317
Rep Power
4

## Re: Unreachable statement???

To put it simply the return statement should be the last thing the method will do.

One way to resolve this is to use an array to return all the values at the same time.
Another would be to pass the gravitational multiplier into the method along with the weight of the person. The method would the calculate the result and pass the result back to the calling method, something which would have to be done four times to get values for the four planets.

Regards.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•