Objectives: |
|
Background: |
You have a Canadian friend with whom you talk frequently. Canadians use the metric system for many of their measurements and so use temperatures in the Celsius scale. You want to practice your Java skills by writing a program to help you quickly figure out what the equivalent temperature (in Fahrenheit) is here in Harrisonburg. |
New Terms: |
|
Materials Needed: |
Use this "starter program": |
Prerequisite |
You should read chapter 3.10 and 3.11 before beginning this lab. |
Submission: |
Upon completion, upload this item via the Canvas
assignment link:
|
Part 1: Setting up your environment
- Create a new folder for this lab.
- Download the materials listed in the Materials section above into the folder.
Part 2: CelsiusToFahrneheit
Write a program that will convert Celsius temperatures to
their equivalent Fahrenheit values. The appropriate formulas
to solve the problem are found on the following web site:
http://convertcelciustofarenheit.com/
- Decide which formula should be used to convert a Celsius temperature to its equivalent in Fahrenheit.
- Develop three examples to use in testing your program. Think carefully about the range of data for temperatures.
- Write your examples on a sheet of paper, in a format like xx.xC=yy.yF. Optionally, list your three test examples in the main method documentation of CelsiusToFahrenheit.java. Your instructor or TA may ask to see examples if you are having trouble with your code.
- Add your code to solve the problem to CelsiusToFahrenheit.java. Use the four sections outlined in the base code as a guide.
- Test your code with the three examples that you came up with earlier and make sure that it works correctly with each.
Part 3: Formatting your output using printf
Your program displays the temperature as a number with a
variable number of decimal places, depending upon the
calculated value. Ok, so how lame is it to say, "Wow, that's
like 87.199823 Fahrenheit." So you want to produce a number
that is more reasonable and only has one decimal point.
- printf is a method like print or println. Instead of just printing what is in the parentheses (the method parameter), printf allows for substitutions and formatting of output. printf takes two or more parameters, separated by commas. The first parameter is a formatting string. It contains the text that you want to use as well as "spots" where we will substitute other values. An expression beginning with the "%" symbol indicates in the format string wherever you want a value (like the temperature) to be placed. (A similar method is String.format() which uses the same type of formatting String as printf does.
- The format of the expression is %[width][.][precision]conversion
EXAMPLE: A double variable is named totalValue and we wanted to print it with three decimal places:
System.out.printf("The total value is %.3f\n", totalValue);
The format string is printed as shown, except that instead of %.3f, the value of totalValue is displayed with 3 decimal places.
Try this in your CelsiusToFahrenheit program, but print the value with 1 decimal position. More information about printf is found in Gaddis, chapter 3.11.
Part 4: Another way to format output
- The DecimalFormat class also provides number formatting options. To format a value in a preferred decimal format, you can create a DecimalFormat object that carries the correct properties.
- First, you must import the DecimalFormat class. To import DecimalFormat, include the import statement, import java.text.DecimalFormat; at the top of your program.
- At the bottom of the program (and this will not conform to our program standards, but this is a lab, not a formal PA), add in a declaration for amount as a double variable.
- Declare a variable, formatter, of type DecimalFormat.
- Instantiate formatter by creating a new DecimalFormat object, passing it the format String, "0.0" and assigning it to formatter. See Chapter 3.11 for some examples.
- Assign to amount the value 103.159456.
- Output amount by calling formatter's format method. Your line will look like System.out.println("The amount is: " + formatter.format(amount));
- Test your program to verify that it outputs the result with a single decimal place.
Part 5: Outputting currency
- How can you use printf to output a number in currency format? Currency values print with a "$" at the beginning of the number and 2 decimal places.
- Add a print statement following the Part 4 output to print the amount as if it were a currency value.
- How can you use DecimalFormat to output currency format? Create a new decimal format object with the appropriate formatting String. (There is also an easier way. See if you can find it online, as it is not in the text.).
- Add a print statement following the 5.2 output to test your new format.
Part 6: (OPTIONAL - Challenge) JOptionPane to input and output in a window
- If you choose to accept this challenge, copy your program to CelciusToFahrenheitV2.java. Remove the statements from Part4 and Part5 so that the program just deals with the Celcius to Fahrenheit conversion.
- JOptionPane is a Java class that permits you to have pop up windows for input and output. You can find out more about JOptionPane in your book in section 2.14.
- For your Celsius to Fahrenheit conversion program, use a popup window for your input value (degrees celcius) and a window for your output value. Make sure that you have your input window message make sense to the user and that your output is clear (x degrees Celsius is y degree Fahrenheit). This should use the same formatting as described in the prior part of the lab.
Turning in your work
Upload your completed program, CelsiusToFahrenheit.java to Canvas, using the lab assignment.