symbol

The Java Programming Language

Jmad


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:

Use this "starter program".

CelsiusToFahrenheit.java

Prerequisites: You should have completed the homework assignment related to this lab from class.
Acknowledgement:: CelsiusToFahrenheit, Ms. Harris
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
This lab will extend what was done in class. You need to write a program that will convert Celsius temps to their equivalent Fahrenheit temps. You should have 3 examples ready for testing and should have outlined the algorithm and written pseudo code for the solution prior to this lab.

  1. To the CelsiusToFahrenheit.java source program, add in the code to solve the problem. Use the four sections to fill in the appropriate code.
  2. Test your code with the three examples that you came up with in class.

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.

  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.

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 3.1234.
  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 an insure that money prints with the $ symbol and only has 2 decimal places.

Part 5: Using printf to output currency (Optional)

  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.

Turning in your work

Upload your completed program to the Blackboard assignment.

AnswerKey
last updated - 09/21/2010 by NLH index