Lab 03 - Thermometer
Overview
In this assignment you'll use the ACM graphics libraries to generate an image of a thermometer.
Assignment
Write a GraphicsProgram
that reads in a temperature in degrees Fahrenheit and calculates the corresponding temperature in degrees Celsius. Display the results visually by drawing a thermometer.
The program should display a thermometer with the correct amount of mercury showing, as well as a GLabel
underneath with a message outputting the degrees Celsius.
The thermometer should be centered vertically and horizontally on the screen, similar to the diagram provided below:
As with most labs in this class, up to 10% extra credit may be earned by going beyond the basic requirements, as long as the basic requirements are met.
Hints
- The formula to convert from Fahrenheit to Celsius is:
$$C = (F - 32) * \frac{5}{9}$$
-
While testing, it may be easier to "hard code" in a value for Fahrenheit, until you get the thermometer drawing correctly, but don't forget to modify your final code so that it will prompt the user for the Fahrenheit value.
-
Figuring out the layout of everything will be easier if you use variables instead of "magic numbers". For example, instead of writing:
GRect thermometer = new GRect(400, 200, 100, 800);
Define some variables you can use to help you in later calculations:
// Get the temp in F
double F = 89;//readDouble("Enter the temperature in F: ");
// Convert temp to C
// ...
// Calculating screen size
int WIDTH = 600;
int HEIGHT = 800;
resize(WIDTH, HEIGHT);
// Calculate X's and the Width
float centerX = WIDTH / 2.0f;
float thermWidth = 100f;
float thermX = centerX - (thermWidth / 2.0f);
// Calculate Y's and the Height
float centerY = HEIGHT / 2.0f;
float thermHeight = 300f;
// Need to account for title bar
float titleBarFudgeFactor = 20f;
float thermY = centerY - (thermHeight / 2.0f);
float drawingHeight = thermHeight - titleBarFudgeFactor;
// Draw thermometer
GRect thermometer = new GRect(thermX, thermY, thermWidth, drawingHeight);
thermometer.setFilled(false);
thermometer.setColor(Color.BLACK);
add(thermometer);
// Draw mercury
// ...
// Draw the scale
// ...
// Display the celsius at the bottom
// ...
This makes it easier to know the position that labels and other elements should go. For example, you can figure out where the bottom of the thermometer is by adding its top to its height.
References
The following sites might assist you as you decide how best to draw the thermometer:
Submission
To submit your assignment, follow the instructions for code submission.