Radford University ITEC

ITEC120-ibarland (incl. office hrs)infolectureslabshws

hw04i:
Practice Designing a Class

See the hw04 grading guide—2006.Nov.03

Due Oct.24 (Tue) 17:00 (WebCT, and hardcopy soonafter), to be done individually.
Make all fields private, and all methods public. Of course, use javadoc comments (@param etc.). As usual, turn in a hard copy (of both files) and submit on WebCT; please include your name (in comments) at the top of each file.

Description: You'll design and write a class to represent a grocery store item, including a test method.

The Item class: Write a class which represents an item in a grocery store. The data associated with a grocery store item:

Behaviors of a grocery store item:

NOTES: There are two different types of items -- items sold by weight and items which simply have a price. herefore, the price stored in the object can mean two different things. For items sold by weight, price means price per pound. (Items sold by weight have a 5 as the last digit of the item number.) Other items just have a price which is the price. This means that several different methods have to take this into account. For example, the toString method should return a string like:

456925  Apples, Fuji    2.32 pounds @ $1.29 per pound $2.99
or like:
456920  Cracker Jacks                                 $2.49
depending on whether the item is sold by weight (apples) or not (Cracker Jacks).

Similarly, the getPrice method will have to figure out if the item is sold by weight. In the apple example above, the price stored in the object would be 0.29, and the price returned by the getPrice method would be 2.99. One of the premises of object oriented design is that is should be impossible to create invalid objects or have invalid data. So, the constructors should check to make sure that if the item has a weight, the last digit of the item number is 5. The other constructor, which sets the weight to 0, should make sure that the item number does NOT end in 5. Your constructors should print an error message to System.err if there is an error with the item number (and then continue, as reasonably as it can).2 We will also see in the last couple of weeks of the course, how to make items-sold-by-weight and items-sold-individually as separate classes, yet they are still both share the same interface methods.


1 Be aware, if talking with PIs, that other sections of this class might be using an int or long to represent the item-number.      back

2 A better way to handle this is for the constructor to throw an exception if there is a problem, and therefore not create an object. This ensures that all objects created are valid -- they contain valid data. We have not yet learned about throwing exceptions. We may do this as a lab excercise later.      back

ITEC120-ibarland (incl. office hrs)infolectureslabshws


©2006, Ian Barland, Radford University
Last modified 2006.Nov.03 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme