Names: _________________________________
COSC 135 A Activity on Data Classes
Tom Linton, Central College, April 17, 2003
The purpose of this activity is to gain experience using the BlueJ object bench and inspector to investigate data fields of reference type and to gain experience using and developing instantiable classes (data classes).

Go to G:\Lintont\prog1\ and grab a copy of the checking folder. In this folder you will find:
  • The nearly complete source code file for the Money data class, Money.java.
  • The finished source code file for a data class named Checkbook, Checkbook.java.
  • An almost empty source code file for a data class named Check, Check.java.
  • A folder holding the HTML files generated by the JavaDoc comments in the above source code files (actually, some of the files used to generate these help pages have been edited to remove parts that you are to complete).
There are links on Blackboard and the class webpage to these documents as well.

Copy the checking folder to your H drive. Start BlueJ and open the checking project you just copied to your H drive.
  1. The first step of this activity will be to familiarize yourself with the data class Money.java. You should be able to understand this source code. Open the Money.java file in a BlueJ editing window. What 2 data fields does each Money object have, and what are their types?




  2. Open the file index.html in the doc subfolder of your checking folder with a browser (i.e. use Internet Explorer). On the left frame, click on the "Money" class link. In the right frame, scroll down to the section named "Field Summary" and verify that you correctly answered question 1. Now, scroll down a bit farther to the Constructor section of the HTML file. See if you can't figure out how to utilize each of the four constructors (looking just at the JavaDoc generated web page) to create 4 Money objects as follows:
    1. Recall that to create an Object on the object bench in BlueJ, you right-click the class and select one of the methods. Then, you fill needed values in the windows that appear.
    2. Use the no input constructor to create a Money object named m1 with 0 dollars and 0 cents.
    3. Use the single double input constructor with value = 100.75 to create a second Money object named m2.
    4. Use the single int input constructor with pennies = 4580 to create a third Money object named m3.
    5. Use the two input constructor to create a Money object named m4 with dollars = 25 and cents = 90.
  3. Right-click the Money object m2 and select inspector. What gets displayed?



  4. Invoke the add method of m2 and enter m3 as the input parameter. A result window should pop-up with a line stating that the result is a reference type value (at this point you can't see the dollars and cents fields of the result). Click once on the object reference line in the pop-up window and select inspect. You should see the dollars and cents of the result object. Record the values of the result object's dollars and cents field, then close the inspector window for the result object, but do NOT close the result window itself.




  5. If you want a copy of the result object on the object bench, make sure the object reference line is highlighted in the result  pop-up window and select the "get" button. What happens when you do this?





  6. Invoke the calculatePercent method of result and enter 0.5 (for 50%). Get the result of this method onto the object bench (see the last question for how). What name was given to this second result?



  7. Invoke the setDollars method of m1 and set m1.dollars to 73. Invoke the setCents method of m1 and reset the cents field of m1  to 28. Invoke the equals method of m1, using result_2 as the Money object for comparison. What is the result of this call to m1.equals()?




  8. To complete the Money class, you need to write two additional methods. The first is the subtract method. Model your code on the source code for the add method (look in the BlueJ editor window for this source code), and base your code on the documentation for the subtract method in the JavaDoc HTML file. In the method summary section of the HTML file for the Money class, find the documentation for the subtract method. Click on the subtract link to see more detailed information, including a proper declaration line for this method. You should NOT check for bad subtractions (like when money2 is larger than the invoking Money object's value), and your code should allow for negative Money amounts. Add the subtract method to your Money.java file (the comments for it are near the bottom of the file) and re-compile the source code for the Money class. Be sure to compile ONLY the money class, (use the compile button in the editor window, not the compile button in the project window) since your Checkbook and Check classes are not complete yet. All of your objects on the bench will dissapear. Create some new Money objects and check that your subtract method works properly. When it does, copy the code for your subtract method below.
















  9. Similar to the last question, look at the source code (in Money.java) for the equals method and the documentation for the isLessThan method in the HTML file. Write the isLessThan method for the Money class (the comments for this method are near the bottom of the source code file), test it out, and copy your code below.











  10. The source code for the Check class has almost nothing in it. Hopefully you can read about the Check class, it's purpose, data fields, constructors, and methods in the HTML file (on the left frame, click once on the Check link to read the information about the Check class). Implement the data class Check. You might proceed as follows:
    1. Declare the three data fields (as private) checkNumber, amount, to.
    2. Define the constructor. It should simply give values to the three data fields.
    3. Write the accessor methods getCheckNumber, getAmount, and getTo. These simply return one of the data fields.
    4. Write the mutator methods setCheckNumber, setAmount, and setTo. These simply re-set one of the data fields to the input parameter.
    5. Write the toString method. You'll need to concatenate Strings like "Check number: " with the data fields to create the String that you return.
    6. Write the equals method. Here, you're simply checking if the current checkNumber is equal to check2.getCheckNumber().

      When you finish writing the Check data class, create some Check objects on the object bench and test out your methods. Print out the source code for your Check data class and hand that in with this file.


  11. Look at the HTML file for the Checkbook class. That file contains enough information for experienced programmers to use Checkbook objects without seeing (or understanding) the source code (you can look at the source code too, if that helps, sometimes source code is better than JavaDoc help files). The Checkbook class requires a completed version of the Check data class and a completed version of the Money class. If you haven't completed these classes, you cannot compile or fully utilize the Checkbook class. Write a driver class named UseChecking.java that performs the following tasks (in the order given). Note, code in the driver class uses the dot operator to call methods (instead of right-clicking methods on the object bench), so for example, to add money1 to money2 and store the result in money3, you'd type:
        Money money3 = money1.add(money2);
    and so forth.
    1. Declare and create a MainWindow, InputBox, OutputBox, and MessageBox.
    2. Create a Checkbook object named myCheckbook.
    3. Print the next check number and balance of myCheckbook in your OutputBox.
    4. Ask the user who they would like to write a first check to, and how much the check is for. Make sure their check amount is less than the current balance of myCheckbook. If it isn't, repeatedly display a message (in your MessageBox) and ask for a lower amount, until they enter a legal value. Create an appropriate Money object and add a check (use the 2 parameter version of the addCheck method) with these values, to myCheckbook.
    5. Repeat part (d) for a second check.
    6. Ask the user for a checknumber, who they would like to write a third check to, and how much the check is for. Check that the amount is less than the current balance again. Create an appropriate Money object and then a Check object from their values. Add this check (use the 1 parameter version of the addCheck method) to myCheckbook.
    7. Remove check number 2001 from myCheckbook and then print (in your outputBox) the last 2 checks.
    8. Deposit 15% of the current balance into myCheckbook and then print the current balance.
    9. Print (in your OutputBox) the check with checknumber 2000.
  12. Print out your UseChecking.java file and turn it in with this paper.