
|
Exploring the Math class, more formatting practice.
|
 |
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.
- understand the two meanings of +
|
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 three "starter
programs".
Concat.java
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
Create a new folder for this
lab, and download your materials from
the Materials section above into the folder.
Part 2: Two meanings of +
This part relates to the quiz question and gives you a chance to see concatenation in action.
In Java, the symbol + can be used to add
numbers
or to concatenate strings. This lab illustrates both uses.
When using a String literal (a sequence of characters enclosed in
double
quotation marks) in Java the complete String must fit on one line. The
following is NOT legal (it would result in a compile-time error).
System.out.println ("It is NOT okay to go to the next line
in a LONG string!!!");
The solution is to break the long String up into two shorter strings
that
are joined using the concatenation operator (which is the + symbol). So the
following would be legal.
System.out.println ("It is OKAY to break a long string into "
+ "parts and join them with a '+' symbol.");
(Note how the continued line is indented one space to indicate it is
a
continuation line. Also note that the operator is placed at the
beginning of
the continuation line instead of at the end of the first line, to
improve
readability.)
So, when working with strings the + symbol means
to concatenate the Strings (join them). BUT, when working with numbers
the + means
what it has always meant, add!
- Download the program Concat.java . Do not compile or run the program (yet).
- Unlike primitive data types (do you remember the 8 primitive data
types?), Strings are Java classes just like the Hello program was a
class. As such, it has methods or actions that can be done on Strings.
- This program uses the length method of
the String class. length is a method that returns a value which, in this example, is the number
of
characters in sample. Or another way to read
this is "sample's length". (See chapter 2.9 for more information about Strings)
- "Hand trace" the program. That is, act as if you were a computer
and execute each statement in the program like a computer would. Draw
storage boxes on a piece of paper to simulate variable/constant storage
containers.
- Write the output your hand tracing produces on your paper.
- Compile and run the actual java program. Compare your "expected result" with
the "actual result".
- For each line where the "expected result" differed from the
"actual result", do you understand why your prediction was wrong? If
you cannot figure it out so you know for sure, ASK!
There is nothing to turn in for this part.
Part C: 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.
- Study the program to understand what it does.
- 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.
-
Add the import statements necessary to make the DecimalFormat and classes.
- 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.
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.
- Add a declaration for money as a NumberFormat object.
- Assign to money the currency instance as described above.
- 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.
- Calculate the correct number of pounds based on the ounces that are read in. (Use a constant for the conversion factor).
- 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.
- Calculate the total price of the deli item.
- 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.
- 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 D: Distance and using the Math class methods.
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 MathClassMethods. 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.
- 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.
- Add an assignment statement
to compute the distance between the
two points.
- 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.
- Echoing is a technique in which you display the data that was input to insure that it is being read in correctly.
- 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.
- Test your program using your
examples.
Part E:
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.
- Upload your final assignment
to the Canvas assignment for
this lab. You should submit both working programs (DeliFormat and Distance).
Part E: OPTIONAL - Using
JOptionPane - Refer to book, chapter 2.14.
- Save a copy of your
DeliFormat lab with a different name
(such as
DeliFormatV2.java).
- Replace the input with
JOptionPane input dialog boxes.
- Replace the output with a
SINGLE JOptionPane message box.
What displays in the message must match the format shown
above.
- Upload this assignment to
Blackboard when complete. Be sure
to Submit before class on Monday.