Java

Guess My Weight

22 points

private instance variables
In this section, declare variables to store the X and Y coordinates of where you position a planet's name on the screen. For example:
private int mercuryX, mercuryY;

Declare variables to store each planet's distance from the Sun, measured in 'astronomical units' (AU). For example:
private double mercuryDist;

Declare variables to store each planet's size, for example:
private double mercurySize;
Also, declare a double 'earthWeight' for the user's weight on Earth, a double 'planetWeight' for the user's weight on another planet, and a String 'planet' to store a planet's name.

 

drawPlanets()
Here is a webpage that provides information on the size of the planets and their distance from the sun: click me. The relevant table is at the bottom of this page. Use the numbers in the first 2 columns in the table for each planet's size and distance from the sun.

For example:
mercurySize = 0.38;
mercuryDist = 0.39;

To position each planet horizontally, I defined an integer 'totalDist' and set it equal to 42. (42 is slightly greater than the number of AU out to Pluto.) I then set 'mercuryX' in this way, where 1800 is a typical horizontal screen width:
     mercuryX = (int)(mercuryDist / totalDist * 1800);

The Y coordinate of each planet can be assigned randomly, for example: mercuryY = 500;

 

fillCircle()
I don't much like BreezyGUI's 'fillOval' command, so I wrote a 'fillCircle' command to use instead. It is included in 'PlanetWeight.java'. The method 'fillCircle' takes as parameters the X and Y coordinates of the circle's center, the circle's radius, and the Graphics object g.

Use the 'g.setColor()' and 'fillCircle()' commands to set the color and draw the planets. You may want to make the names of the planets a different color from the planets themselves. Here's an example:
g.setColor(Color.green);

This program uses the integer 'mult' to help set the size of the planets. Here is how 'mult' is used when calling fillCircle():
fillCircle(mercuryX, mercuryY, (int)(mercurySize*mult), g);

To make the planets appear larger or smaller, just change 'mult'.

Use the 'g.drawString()' method to add the planets' names to your schematic. Here's an example:
g.drawString("Mercury", mercuryX, mercuryY, g);
Use the variables you defined earlier (for example: mercuryX, mercuryY) in the 'drawString()' method to position the names.

 

mouseClicked()
When the user clicks on one of your planets, you need to figure out which planet she clicked on. This happens in the 'mouseClicked()' method, which accepts parameters 'x' and 'y'. These parameters give the coordinates of the pixel on which the user clicked. You need to match up these parameters with the positions of the planets. I suggest you 'draw a box' around the point on which the user clicked. If the center of a given planet was drawn inside that box, then that is the planet the user chose. To draw this box, declare a variable 'offset', and set it equal to about 30 (pixels). Declare 4 integers (left, right, top and bottom) to store the positions of the left, right, top and bottom sides of the box. The left side of the box should equal 'x - offset', the right side of the box should equal 'x + offset' etc.

Then you need an IF - ELSE IF - ELSE structure to figure out which planet the user clicked on. In the IF statement, see if 'mercuryX' is larger than 'left' and smaller than 'right,' and if 'mercuryY' is larger than 'top' and smaller than 'bottom'. (Use && to say 'and'.) If all 4 comparisons are true, then the user clicked on Mercury, and you should assign "Mercury" to the private String variable 'planet'. In addition, multiply 'earthWeight' by the correct conversion factor and assign this result to 'planetWeight'. For example, if the user clicked on Mercury, multiply 'earthWeight' by 0.38 to get the value of 'planetWeight'.

In the first ELSE IF, do similar comparisons to see if the user clicked on Venus. Continue through Pluto, and in the ELSE (which runs if the user did not click on a planet's name) assign "no planet" to 'planet'.

At the end of the method, call the method output().

The Planets
Here are factors that you would use to calculate someone's weight on another planet. For example, if you weight 130 pounds on Earth, multiply 130 by 1.17 to find out what you would weigh on Saturn.
Earth 1.00
Jupiter 2.65
Mars 0.39
Mercury 0.38
Neptune 1.23
Pluto 0.05
Saturn 1.17
Uranus 1.05
Venus 0.78

 

output()
This method controls the execution of the program after the user clicks on a planet. Use an IF statement to see if 'planet' is NOT "no planet" -- you only want to print a result if the user clicked on a planet. Print the user's weight on the planet using a BreezyGUI messageBox. You might want to prepare the String to display using syntax like this:
String message = "some stuff\n";
message += "some more stuff\n";
message += "some final stuff";
messageBox(message);

 

Demo

You can download and run CompletedPlanets.jar to get an idea of how your program will run when it is complete.

 

Starting Point

You can download PlanetWeight.java to use as a starting point in writing your program.

 

Student Solar Systems

Here are solar systems done by students:


Rich Pearsall, 2007-2008

 

 


Yee-Hang, 2007-2008

 

 


Sean, 2015-2016

 

 


Solar system with asteroid belt, by RD, 2017-2018