Objectives: |
|
Background: |
You have a Canadian friend with whom you talk frequently. They use the metric system for many of their measurements and so use temperatures in the Celsius scale. You want to practice your Java skills and write a program to help you quickly figure out what the equivalent temperature 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: |
Upload these items via the Blackboard 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
You need to write a program that will convert Celsius
temps to their equivalent Fahrenheit temps. The appropriate
formulas to solve the problem are found on the following web
site:
http://convertcelciustofarenheit.com/
- Decide which formula you need to convert a Celsius temperature to its equivalent in Fahrenheit.
- Develop 3 examples to use in testing your program. Think carefully about the range of data for temperatures.
- List your three test examples in the main method documentation of CelsiusToFahrenheit.java.
- 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, printf method
The output from your program produces the temperature as a
number with as many decimal points as calculated. 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 parameter), it allows for substitutions and formatting of output. The first parameter of printf is a formatting String. It contains the text that you want to use as well as "spots" where we will substitute other values. In the String wherever you want a value (like the temperature), you include the % symbol and the formatting instruction.
- The format of the substitution is % [width][.][precision]conversion
EXAMPLE: If the double variable were value and we wanted to print it with three decimals, I would have the statement:
System.out.printf("The value is %.3f\n", value);
This says, print the String as shown in the printf, but instead of %.3f, substitute value and format it with 3 decimal positions.
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 contains number formatting options. To format a value as value in a preferred decimal format, you can create a decimal format object which carries the correct properties.
- 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, that is a DecimalFormat object.
- 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 that your program outputs the result with a single decimal place.
Part 5: (OPTIONAL) Outputting currency
- How can you use the printf and substitutions to output currency format? Currency prints a "$" at the beginning of the number and rounds the result to 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.
- 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
- 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 celcius 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 Celcius is y degree Fahrenheit).
Turning in your work
Upload your completed program to the Blackboard assignment.