Results 1 to 6 of 6
  1. #1
    dangolf is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default 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 03:33 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Unreachable statement???

    In both your earlier threads*, you were advised about using code tags. Are you a slow learner, or just plumb lazy?

    db

    * Need help with program
    * Writing a program that adds 2 rational numbers (in the form of fractions) in BlueJ.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    dangolf is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default 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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unreachable statement???

    Quote Originally Posted by dangolf View Post
    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. #5
    Vamsi Priya is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default 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. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default 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.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  2. Strange unreachable statement error...
    By silafirion in forum New To Java
    Replies: 5
    Last Post: 12-11-2010, 05:05 AM
  3. Unreachable statement
    By lala in forum New To Java
    Replies: 3
    Last Post: 11-25-2010, 03:32 PM
  4. Unreachable statement Pig Dice Game
    By noturn10 in forum New To Java
    Replies: 2
    Last Post: 11-22-2009, 12:36 AM
  5. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM

Posting Permissions

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