Navigation

Home

Weekly Schedule

TA Schedule

Prof Harris Schedule

Prof Capaccio Schedule

General Policies

Resources

CodingBat, practice site

Tutorial directory

Lab directory

Temperature Conversion


Objectives:

At the conclusion of  this lab students should:
  • be able to format their output numeric values using DecimalFormat and NumberFormat objects or use printf for basic formatting

Background:

This lab will have students work with formatting and mathematical operations in Java.

New Terms:

instantiate
 
The process of creating a new instance of an class.
object
A single instance of a class.

Materials Needed:

Use this "starter program".

CelsiusToFahrenheit.java

Turning in your work:

See your instructor for specific instructions for turning in your work or getting credit for this lab.

Part 1: Setting up your environment

  1. Create a new folder for this lab.
  2. Download your materials from 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/

  1. Decide which formulat you need to convert a Celsius temperature to its equivalent Fahrenheit.
  2. Develop 3 examples to use in testing. Think carefully about the range of data for temperatures.
  3. In the main method documentation of CelsiusToFahrenheit.java, fill in your three examples.
  4. To the CelsiusToFahrenheit.java source program, add in the code to solve the problem. Use the four sections to fill in the appropriate code.
  5. Test your code with the three examples that you came up with earlier.

Part 3: Formatting your output, printf method - Gaddis

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.

  1. 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.
  2. 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

  1. The NumberFormat class contains number formatting options. To format a value as a currency format, you can create a number format object which is like currency.
  2. You must import the NumberFormat class. This class provides for formatting of values. The one that we are focusing on today is the currency format. To import NumberFormat, include the import statement, import java.text.NumberFormat; at the top of your program.
  3. 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.
  4. Declare a variable, money, that is a NumberFormat object.
  5. Instantiate money by calling the method NumberFormat.getCurrencyInstance() and assign its value to money.
  6. Assign to amount the value 103.123456.
  7. Output amount by calling money's format method.  Your line will look like System.out.println("The amount is: " + money.format(amount));
  8. Test your program to insure that money prints with the $ symbol and only has 2 decimal places.

Part 5: (OPTIONAL) Using printf to output currency

  1. How can you use the printf and substitutions to output currency format without needing to use the NumberFormat class?
  2. Add a second print statement following the first amount one that prints the same thing, but uses printf instead of the format method of NumberFormat.

Part 6: (OPTIONAL - Challenge)JOptionPane to input and output in a window

  1. 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.
  2. 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.