Lab 9 In-Class: Writing Classes

Lab Objectives

  • Gain experience writing a class and using the class in a client program.

Log onto the Linux system and create a lab9 subdirectory of your cs120/Labs directory for today's work. As usual, you will need to have three windows open: an xterm, Emacs, and Firefox.

Writing Classes: The Account Class

Program contains the Account class from the pre-lab and pages 193-194 from the textbook.

  1. In the Account class ( do the following: Make sure your class has no compile-time errors by going to your xterm window and compiling

  2. Program contains the shell of the program from the prelab that uses the Account class to create and manipulate bank accounts using methods from the Account class. Add code as indicated by the comments. Note that this program asks you to use getBalance to print the balance in two places that were not in the prelab and it has added some interactive input (and asks you to add statements to deposit and withdraw amounts entered). Compile and run your program to make sure it works.

  3. Although chargeFee returns the new balance, your ManageAccounts program currently does not save or use that new balance, it basically throws that value away. Modify ManageAccounts so that each time it calls chargeFee it stores the returned balance in a variable (you'll have to declare a new one). After each call to chargeFee, add a print statement that prints the stored value of the new balance (appropriately labeled).

  4. Real bank accounts have many more attributes than the three in this simple Account class. For example, a record of each transaction would be associated with each account. That is too complicated for us but we can keep track of the number of transactions. To do this add the following to the Account class:
    1. Add an instance variable named numTransactions of type int. Remember that we make instance variables private.
    2. In the constructor, initialize numTransactions to 0. (NOTE: Java automatically initializes instance variables to 0 but it is generally a good idea to explicitly assign initial values.)
    3. In the deposit and withdraw methods increment numTransactions. Note that this should be done only when the transaction actually occurs, not when there is an error (such as a negative amount or insufficient funds).
    4. In the toString method modify the string returned to include the number of transactions, appropriately labeled.
    5. Classes often have accessor methods for each attribute (see page 183-184 of the text). These methods are often called "get" methods or "getters." All they do is return the value of the attribute so the client program can use it in some way (print it or use it in a calculation for example). The getBalance method is an example of an accessor method. We need to add a getTransactions accessor method to the Account class (note this goes in It will be similar to getBalance except it will return the number of transactions. Think about what the return type for the method needs to be. Be sure to add the header documentation (comments) for the method.
    6. Add a print statement at the end of the ManageAccounts program to print out the number of transactions for each account (use your accessor method). NOTE: Add this even though you are already printing the complete account information - this time you should print just the number of transactions using getTransactions.

  5. Add an accessor method getName() to the Account class. Include the header documentation.

  6. Modify the last print statements (in - the ones that printed the number of transactions) to also print the name of the account owner using the getName method in your print statement.

  7. Run your program to test it.

Writing Classes: A Student Class

Program contains the incomplete Student class declaration from the prelab.
  1. Complete the class declaration. You will need to do the following: Compile to make sure your Student class has no compilation errors.

  2. Program contains a shell of a program that contains a sentinel controlled loop (stops when the name read in is "q") that reads in the name and two test scores for each student a class and, for each student, prints the average grade, the corresponding letter grade, and the highest grade. It will also compute and print the class average (the average of the student test averages). All of this will be done using a Student object and methods from the student class. Fill in statements in to do the following for each student (use the comments in the code to determine where to put these - note that some of what you need to do is similar to what you did in pre-lab). Test your program.

  3. Enhance your Student class so that you figure out the student's initials. Initials are easy to extract from the full name if we assume there is exactly one space separating the parts of the full name. All we need to do is use the first character plus any character that comes after a space. Proceed as follows:

  4. Add code to the Grades program to determine who has the highest average and who has the lowest. Notice that there are already two Student objects (topStudent and bottomStudent) declared for this. One (topStudent) has been instantiated with an empty name. You need to do the following:

  5. Add statements to the end of your Grades program that print the values of your topStudent and bottomStudent variables directly, e.g.:
       System.out.println("The student with the highest average is: " + topStudent);
    This should run, but notice what it does -- nothing very useful! When an object is printed, Java looks for a toString method for that object. This method must have no parameters and must return a string (there is one in the Account class). If such a method exists for this object, it is called automatically -- you don't have to write the call in your program -- and the string it returns is printed. If no such method exists, a unique hexadecimal identifier for the object is printed (e.g., Student@3a56d7).

  6. Add a toString method to your Student class (in that returns a string containing the student's name and test scores in the following format:
                      Joe  Test 1: 85  Test 2: 91
    Note that the toString method does not call System.out.println -- it just returns a string. The Die class, the Account class, and the Coin class in your textbook all have toString methods. The header is the same for all - the only difference is in the string returned.

    You shouldn't have to change the Grades program -- you don't have to call toString explicitly. Now see what happens when you run the Grades program - the output is much nicer!

  7. Be sure to add documentation for the toString method - state what it does and what it returns.

To submit your code: Tar the files in your lab9 directory (be sure to name the file with your names, not lab9.tgz) and cp the tgz file to the directory: