|
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
due Oct.10 (Wed) at the start of class.
However, try to see if you can complete it by Tuesday.
A Treasure is a relatively simple class: every treasure has four attributes: a name, a description (a complete sentence), a weight (in lbs), and an image-URL. Some examples might be:
name: a small nugget
description: Upon close inspection, the nugget glints of gold!
weight: 0.05 lbs
image-URL: “http://americanhistory.si.edu/dynamic/images/collections_large/98-4804_225px.jpg”name: a fancy pen
description: This pen can write in purple, green, and plaid.
weight: 0.1 lbs
image-URL: “”name: chocolate egg
description: The size of an ostrich egg, but made entirely of dark chocolate.
weight: 35 lbs
image-URL: “”
(6pts) Create a class with these fields.
Before each field,
include a short javadoc comment describing what each field holds (including units, if applicable).
Also, be sure that your class extends
(4pts)
Write a function
As you begin each of the functions below,
start by writing a test-case and adding it to
(5pts) Make a named
constant2
(5pts)
Write a function
(8pts) Write
Although we will not be doing this for this class,
here is one way a
Include test cases and javadoc for each function you write. You can have all your tests in a single function (so that you can re-use the same variables/examples-of-data for all your tests). As always, write your tests and comments before writing the actual code; the comments will help you focus on what the method does. PIs have been instructed to not help you on a method, if you don't have those comments already written.
Here is some code to help get you started.
Note that
/** Run tests for all Treasure functions (besides the constructor). */ void testTreasure() { Treasure t1 = new Treasure( "fancy pen", "This pen can write in purple, green, and plaid.", 0.1, "" ); // Make at least two more examples. // Have at least 1-2 additional tests per function. assertEquals( toPrettyString(t1), "chocolate egg (35.0 lbs)" ); assertEquals( isLint(t1), false ); assertEquals( isBetterThan(t1,t1), false ); assertEquals( counterfeit(t1), new Treasure( "fancy pen", "This pen can write in purple, green, and plaid, but it feels a bit chintzy.", 0.05, "" ) ); } /** Check if an actual result is equal to the desired result. * If not, print an error message to stderr6; * if so, print something minimal to stdout7 (to give some slight feedback that a test was made). * @param actual The actual result from a function call. * @param desired The desired result for that function call. */ static void assertEquals( Object actual, Object desired ) { if (Object120.equals(actual,desired)) { System.out.print("."); } else { System.err.println( "\n***Unit test failed:"); System.err.println( "Actual : " + Object120.toString(actual) ); System.err.println( "Desired: " + Object120.toString(desired) ); } } |
Put the answer to these questions in a block-comment at the end of your file.
01. class Glarkzle { 02. 03. int numFloobs; 04. String name; 05. 06. static final int MAX_ORDER_SIZE = 25; 07. 08. Glarkzle( int nfs, String nm ) { 09. this.numFloobs = nfs; 10. this.name = nm; 11. } 12. 13. static String placeOrder( Glarkzle g, int numOrdered ) { 14. |
1We'll see later how to override
2Recall that we've decided named constants should actually be fields, not local variables. ↩
3“iff” means “if and only if”. ↩
4
I'd prefer calling the parameters
“
5If you want to implement some different criterion for what makes one treasure better than another, go ahead. Be sure to document it in your comments. ↩
6“stderr” is geekspeak for “standard error” — in BlueJ this is the rarely-seen bottom half of the terminal window and it prints in red. In some environments, stderr and stdout happen to map to the same device (and output sent to those two logically-different places ends up interleaved). ↩
7“stdout” is geekspeak for “standard output” — the terminal window. ↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
©2012, Ian Barland, Radford University Last modified 2012.Oct.12 (Fri) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |