An introduction to methods

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.
  • create and use a method that he/she has created
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.
method
A named block of code that is executed by caling the name of the method and passing in the required argument values.
parameter
A variable that receives a value in a method header.
argument
A value that is passed to a called method.
return statement
A statement that returns a value from a method.
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:

   1. Study the program to understand what it does.
   2. Add the import statements necessary to make the DecimalFormat object.
   3. Add the statement to declare money to be a DecimalFormat object as specified in the comment.
   4. Add the statement to declare fmt to be a DecimalFormat object as specified in the comment.
   5. 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 Blackboard 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 and the parentheses hold the value(s) you are passing to the function.
  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. In the method, calcDistance, fill in the computation for the distance. You should use the parameters (in the header) as the variables in the calculation. You will calculate the result in the variable result and return its value as shown in the return statement.
  3. In the main method, add a call to the calcDistance() function and assign the result to the variable, distance.
  4. 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.
  5. Test your program using the following data: The distance between the points (3, 17) and (8, 10) is 8.6023... (lots more digits); the distance between (–33, 49) and (–9, –15) is 68.352.…
  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 Blackboard at this point and Submit when you have completed the optional exercise.
  1. Upload your final assignment to the Blackboard assignment for this lab.

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.
AnswerKey
last updated - 09/22/2010 by NLH index