CS21 Spring 2005
Homework 4
Due Monday, February 13 before 11:30pm


Do Self-Review Exerciese 4.1-4.9 (except 4.3) and check your answers with those at the end of the chapter (don't turn these in).
Next, you should copy over all my starting point files for the two programming assignments below, and then use these for completing the implementation of your solutions:
# from your cs21/hw04 subdirectory:
% cp ~newhall/public/cs21/hw04/* .
% ls 
You can use the makefile to compile your programs (you don't have to use it, but it is easier to use a makefile). To compile just enter the make command, make will read the compilation rules in the Makefile to compile your .java files to .class files. To remove all .class files, type 'make clean' (this is a good way to force recompilation on next make):
% make	
% ls
Account.class  ManageAccounts.class  Sphere.java
Account.java   ManageAccounts.java   SphereDriver.class
Makefile       Sphere.class          SphereDriver.java

% make clean
% ls
Account.java  Makefile  ManageAccounts.java  Sphere.java  SphereDriver.java

Please complete the following 2 programs and turn them in using cs21handin. You should work on this assignment by yourself (no partners).

  1. Open up the Account.java file. It contains a partial implementation of a class for representing bank accounts. Complete the method implementations for the withdraw, deposit, and toString methods, add high-level comment, and remove my TODO comments.

    Next, open up ManageAccounts.java and modify it so that it

    1. creates three new Accounts initialized as specifed in the comments.
    2. Prints out Account info for all three
    3. Deposits $50 in the first account, and prints resulting balance
    4. Withdraws $400 from the second account, and prints resulting balance
    5. Deposits $75 in the first account, and prints resulting balance
    6. Changes the name on the third account
    7. Prints out Account info for all three accounts

    See the comments in the code for more information about these.

  2. Problem 4.1 on page 195. Use my starting point files Sphere.java and SphereDriver.java for your solution. The SphereDriver class should contain a main method that create 3 Sphere objects, using diameter values read in by the user, and then prints out the volume and the surface area of the three spheres (add a toString method to the Sphere class, that is called to print out this info).

    Once this works, add a method named compareTo to your Sphere class. The compareTo method should take a Sphere object as a parameter, and return one of the following values:

    In your Driver program, add code to print out the spheres from largest to smallest in volume by making calls to your new method. Try running your program with different input values to make sure it is correct.
    This program creates three Sphere objects
       Enter the diameter of the first Sphere: 8.9
       Enter the diameter of the second Sphere: 5
       Enter the diameter of the third Sphere: 2.3
    
       ------------------------------------
       Sphere with diameter 8.9, volume 369.12, surface area 248.85
       Sphere with diameter 5.0, volume 65.45, surface area 78.54
       Sphere with diameter 2.3, volume 6.37, surface area 16.62
       ------------------------------------
       Spheres from smallest to largest:
       ---------------------------------
       Sphere with diameter 2.3, volume 6.37, surface area 16.62
       Sphere with diameter 5.0, volume 65.45, surface area 78.54
       Sphere with diameter 8.9, volume 369.12, surface area 248.85
    
    

    Remember to comment all methods in your class with the following:

    1. What does this method do (1-2 sentence description)
    2. What are its input values (parameters)? This is not a listing of their type, but a description of what each value is/is used for in terms of the method.
    3. What value does it return?
    For example, here is a method comment using javadoc commenting style (the /** is important, and the @param and @return allow javadoc to interpret parts of the comment in different ways and as a result, generate different types of documentation for the method):
    	/**
             * ComputeAveGrade:  this method calculates the student's 
    	 *                   average grade, allowing up to two new
             *                   grade values to be included in the ave
             *
             * @param grade1: a new grade value between 0 and 100, pass in -1 if
             *                you want to compute gpa without including a new grade 
             * @param grade2: a new grade value between 0 and 100, pass in -1 if 
             *                want to compute gpa without including a 2nd new grade 
             *
             * @return the student's average grade as a value between 0.0 and 100.0
             */