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
  • use functions of the Math class to manipulate numbers.
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 these two "starter programs".

DeliFormat.java

Distance.java

Prerequisites: No prerequisites to this lab.
Acknowledgement:: Lab adapted from a lab by Mike Norton and Lewis and Loftus.
Turning in
your work:
See your instructor for specific instructions for turning in your work or getting credit for this lab.

Part A: 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 B: DeliFormat - DecimalFormat and NumberFormat classes

The file DeliFormat.java contains a partial program that computes the cost of buying an item at the deli. Save the program to your directory and do the following steps. Notice that some of the steps are included to check your work. These will not be in the final version of the program.

  1. Study the program to understand what it does.
  2. Create a few examples that you can use for testing. You should have some examples of taking ounces and converting it to decimal pounds and then some examples of taking pounds and calculating the total price.
  3. Add the import statements necessary to make the DecimalFormat and classes.
  4. Add the statement to declare fmt to be a DecimalFormat object as specified in the comment. This is the formatting object that will be used for the display of the pounds.
  5. NumberFormat is another class that provides formatting objects. It contains a method that returns a complete object in money format. This method is named getCurrencyInstance() and the documentation for this method is shown below. You do not need to make a "new" number format, but instead can call this method with the call NumberFormat.getCurrencyInstance() just like you might call Math class methods. NumberFormat also has a format method that works just like the DecimalFormat class does.

    static NumberFormat getCurrencyInstance()
    Returns a currency format for the current default locale.
  6. Add a declaration for money as a NumberFormat object.
  7. Assign to money the currency instance as described above.
  8. CHECK: Check your work by creating a variable and assign it a decimal value then try printing it out using the two different formats. Once you have verified that these work correctly, you can remove or comment out the Check code.
  9. Calculate the correct number of pounds based on the ounces that are read in. (Use a constant for the conversion factor).
  10. CHECK: Check your work by printing the result of this calculation and comparing it to some known examples. You can remove or comment out this statement after you are sure it is working correctly.
  11. Calculate the total price of the deli item.
  12. CHECK: Check your work by printing the result of this calculation and comparing it to your known examples. You can remove or comment out this statement after you are sure it is working correctly.
  13. Add the statements to print a label in the following format (the numbers in the example output are correct for input of $4.25 per pound and 41 ounces). Use the formatting object money to print the unit price and total price and the formatting object fmt to print the weight to 2 decimal places.
       
       ***** CS Deli *****
       Unit Price: $4.25 per pound
       Weight: 2.56 pounds
       
       TOTAL: $10.89
       
Part C: Distance The file Distance.java contains an incomplete program to compute the distance between two points. Recall from math that the distance between the two points (x1, y1) and (x2, y2) is computed by taking the square root of the quantity (x1 – x2) 2 + (y1 – y2) 2 The program already has code to get the two points as input.   For some helpful methods, go to the Resources tab on Canvas and open the document labelled Math Class methods.  Math is a class that provides helpful math funtions.  To use the function, you do not need an object....you simply use Math.xxxxx() where xxxxx() is the name of the method passing whatever value you need.

  1. Provide several additional examples (to the one that is shown below), showing that you understand the problem.  Perhaps draw a graph diagram and use several different coordinates to test.  YOU MAY USE A CALCULATOR for this part.
  2. Add an assignment statement to compute the distance between the two points.
  3. Add output statements to echo the points entered followed by the distance.  You may use your own format for this, but it must include labels describing the output and must include the points (exactly as entered) and the distance (as a double formatted to show only 4 digits to the right of the decimal point.
  4. Echoing is a technique in which you display the data that was input to insure that it is being read in correctly.
  5. Test your program using the following data: The distance between the points (3, 17) and (8, 10) is 8.6023; the distance between (–33, 49) and (–9, –15) is 68.3520.
  6. Test your program using your examples.  
Part D: Submitting your work
NOTE if you are going to do the optional exercise, Save your work to Canvas at this point; you do not need to submit the optional part.
  1. Upload your final assignment to the Canvas assignment for this lab. You should submit both working programs.

Part E: OPTIONAL - Using JOptionPane - Refer to book, chapter 2.14.

  1. Save a copy of your DeliFormat lab with a different name (such as DeliFormatV2.java).
  2. Replace the input with JOptionPane input dialog boxes.
  3. Replace the output with a SINGLE JOptionPane message box.  What displays in the message must match the format shown above.  
  4. Upload this assignment to Blackboard when complete.  Be sure to Submit before class on Monday.

    Updated 09/22/14 (nlh)