Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with class declaration...

Status
Not open for further replies.

GT500FOMOCO

Programmer
Jul 5, 2001
143
0
0
US
I am writing a program that calculates your weight on another planet. I have to use the
Code:
Planet
class, but it doesn't work right. Here is the code:

Code:
public class Weight {

    public static void main(String[] args) {
        ConsoleReader keyboard = new ConsoleReader(System.in);

        System.out.print("\n\nWelcome to Weight-Watching on Other Planets!");

        int choice = 0;
        int weight = 0;
        int newWeight = 0;
        String prompt = "\n\n[1] Mercury\n" +
                        "[2] Venus\n" +
                        "[3] Mars\n" +
                        "[4] Jupiter\n" +
                        "[5] Saturn\n" +
                        "[6] Uranus\n" +
                        "[7] Neptune\n" +
                        "[8] Pluto\n" +
                        "[9] Quit\n\n" +
                        "Enter option: ";
        String prompt2 = "\nEnter earthly weight (in pounds): ";

        while (choice != 9) {
            choice = getChoice(prompt, keyboard);

            if (choice != 9) {
                weight = getWeight(prompt2, keyboard);
                Planet planet1 = new Planet(getPlanet(choice), getWeight(choice));
                newWeight = planet1.convert(weight);
                System.out.print("\nYour weight on " + getPlanet(choice)
                + " is " + newWeight + ".");
            }
        }
        System.out.print("\nGood-bye!");
    }

    public static int getChoice(String prompt, ConsoleReader keyboard) {
        System.out.print(prompt);
        int input = keyboard.readInt();

        return input;
    }

    public static int getWeight(String prompt, ConsoleReader keyboard) {
        System.out.print(prompt);
        int input = keyboard.readInt();

        return input;
    }

    public static double getWeight(int choice) {

        if (choice == 1) {
            return .378;
        }

        else if (choice == 2) {
            return .906;
        }

        else if (choice == 3) {
            return .379;
        }

        else if (choice == 4) {
            return 2.533;
        }

        else if (choice == 5) {
            return 1.066;
        }

        else if (choice == 6) {
            return .905;
        }

        else if (choice == 7) {
            return 1.133;
        }

        else {
            return .067;
        }
    }

    public static String getPlanet(int choice) {

        if (choice == 1) {
            return "Mercury";
        }

        else if (choice == 2) {
            return "Venus";
        }

        else if (choice == 3) {
            return "Mars";
        }

        else if (choice == 4) {
            return "Jupiter";
        }

        else if (choice == 5) {
            return "Saturn";
        }

        else if (choice == 6) {
            return "Uranus";
        }

        else if (choice == 7) {
            return "Neptune";
        }

        else {
            return "Pluto";
        }
    }

    class Planet {

        public Planet(String name, double weight) {
        }

        public int convert(int eweight) {
            int newWeight = eweight * weight;

            return newWeight;
        }

        private int weight;
    }
}

Here is the error message:

Code:
C:\Lesson9\Weight.java:28: non-static variable this cannot be referenced from a static context
                Planet planet1 = new Planet(getPlanet(choice), getWeight(choice));
"and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Don;t make the Palanet class a inner class of Weight. Try placing that code in another file or declare it after the Weight class and it should compile fine.

-gc "I don't look busy because I did it right the first time."
 
I fixed that problem...

Although now I have a new problem. The double weight won't carry it's variable over. I know this should work, here is the code for the class:

Code:
class Planet {

    public Planet(String name, double weight) {
    }

    public int convert(int eweight) {
        int newWeight = (int)(eweight * weight);

        return newWeight;
    }

    private double weight;
}
"and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
In your constructor you should probably put the line:
this.weight = weight; It will proably work better if you assign your variable to what you passed in.

-gc "I don't look busy because I did it right the first time."
 
Try adding this.weight=weight; in your constructor.
The "weight" that your constructor is accepting is local to the constructot.
 
Thanks, but that just confused me...

Although I fixed the problem anyway. "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
GT500FOMOCO,

Try a google search on switch statements and local variables, or see Sun's Java Tutorial.

Are you putting that in an Applet somewhere? that looks like a fun project!

"Don't bother measuring my productivity by lines of code -- it would be negative"
 
Can't use switch statements. I haven't learned them yet.

No, I'm not puting it in an Applet. It's just a good old fashioned MS-DOS program. "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top