L08

Due by the end of Tuesday, October 31

Starter Files:


SUBMIT / CHECK

Note that today you are going to be creating your first non-program class. I will give you a program that you need to run, but you shouldn't make any changes to it (except for deleting the lines I tell you delete when I tell you to delete them). You are going to create a brand new class (called Item), and all the code you write is going to be in that class.

Today's Activities

Activity 1

Create a new project (L08) for the program ItemTest.java. Download the program. You can run it, but it doesn't do anything.

To begin, create the class Item in your l08 package. (Remember, it's a Java Class, not a Java Main Class. There shouldn't be a main method in the Item class.)

Add four instance variables to the Item class:

Add the constructor for the class. It allows any String for the name, but only non-negative values for the other fields. For invalid values, have it print a warning message and use zero. (Do not throw an exception! ItemTest is not ready to handle that!)

In ItemTest, delete the line saying DELETE THIS LINE FOR ACTIVITY 1. Run the project, and you should see output like this:

Activity 1: Data type + Constructor Illegal quantity: -1 Illegal price: -5.0 Illegal cost: -20.0 Press enter...
Note: The "Illegal ..." messages are not mistakes! They're what we expect to see.

If you don't see them, then there's a mistake in your Item.java file.

Activity 2
Create getters for each of the four fields of Item. Remember, all a getter does is return the corresponding value. Its name starts with get and ends with the name of the field (but capitalized in the first letter).

In ItemTest, delete the three lines saying DELETE THIS LINE FOR ACTIVITY 2. (One of them is in main; the other two are around the method testGetters.) Run the program. You should see this output:

Activity 1: Data type + Constructor Illegal quantity: -1 Illegal price: -5.0 Illegal cost: -20.0 Press enter... Activity 2: getters Press enter...
If there are any messages like
*** Unexpected quantity: (0 should have been 20)
then there is a mistake in your code. The mistake is probably in the constructor, because the getter is only one line long -- and how hard is it to mess that up. (But check the getter just in case! You might have made a mistake, especially if you copied and pasted code.)
Activity 3
Create setters for each of the four fields in Item. The name setter accepts any name, but the others accept only non-negative values. If the client asks for a negative value, then a warning message is printed and the value remains unchanged (don't change it to zero).

Remember that the name of a setter starts with set and ends with the name of the field (except capitalized in the first letter).

In ItemTest, delete the two lines saying DELETE THIS LINE FOR ACTIVITY 3. Run the program. You should see this output:

Activity 1: Data type + Constructor Illegal quantity: -1 Illegal price: -5.0 Illegal cost: -20.0 Press enter... Activity 2: getters Press enter... Activity 3a: setters (valid values) Press enter... Activity 3b: setters (invalid values) Illegal quantity: -1 Illegal price: -0.01 Illegal cost: -0.03 Press enter...

If there are any messages like

*** Unexpected price: (0.0 should have been 1.25)
then there is a mistake in your code. The mistake is probably in the setter, because because mistakes in the constructor and getter should have been found in Activity 2. But if you changed you constructor or getters during Activity 3, well then that's probably where the mistake is -- because they were working JUST FINE before!

Submit this/these files:

You will be graded on the following:

  1. Item.java has the correct instance variables ...
  2. ... and no extras
  3. ... all declared private
  4. It has a constructor ...
  5. ... and has the correct parameters ...
  6. ... and sets fields to correct valid values ...
  7. ... but to zero for invalid values
  8. It has the correct getters
  9. ... which return the correct values ...
  10. It has the correct setters
  11. ... which sets the correct field to the given (valid) value ...
  12. ... but does not change any field when invalid values are requested
  13. All constructors, getters and setters declared public
  14. No methods do more than they're supposed to; code meets style guidelines

SUBMIT   /   CHECK