Checkpoint 1
-
Read all three flowcharts, and try out some sample values by hand. Decide which one or ones are correct, and which are incorrect. For any that are incorrect, give a specific value for the weight on which it gives you the wrong price.
- Write Java code implementing the logic of the first flowchart. Put your code into the
computePostage
method of the skeleton class PostageUtil.java
in your package postage1
. This is a simple mathematical function that calculates an output from an input. No instance variables are needed, so we can make it a static
method. When you have written the code, go to the class PostageTest.java
in your lab4
package. Make sure the import
statement refers to package postage1
, and run the main method. Verify that you are getting the results you expect (which, if you believe the flowchart logic to be incorrect, may not match the given "expected" values).
- You may be alarmed to see output such as "0.8899999999999999" where you're expecting ".89". This is a normal consequence of using floating-point arithmetic: most values are not quite exact. If you want to see the results, say, rounded to two decimal places, we can format the output. The simplest way to do this is with
System.out.printf()
. See the alternate test class
PostageTestFormatted.java
for an illustration.
- Next, write Java code for the logic of the second flowchart. Don't modify the code you have in the
postage1
package; instead, copy
PostageUtil.java
into the package postage2
and
edit the package
declaration to be postage2
. Delete the method body for computePostage
so you can rewrite it.
- Now edit the import statement of
PostageTest.java
so it refers to postage2.PostageUtil
, and run it again.
- Now you can probably see why we're using different packages: you can have the test code run a completely different implementation of
PostageUtil
just by changing the import statement.
- Do the same for the third flowchart, using package
postage3
.
- Finally, use
postage1.PostageUtil
to create a Java class with a main method that reads a weight from the console, and prints out the postage.
-
Note that the
Scanner
class includes a method nextDouble
that is similar to
nextInt
but can read floating-point numbers.