GT500FOMOCO
Programmer
I am writing a program that calculates your weight on another planet. I have to use the
class, but it doesn't work right. Here is the code:
Here is the error message:
"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
Code:
Planet
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));
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