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 Account.java contains the Account class from the pre-lab and pages 193-194 from the textbook.

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

  2. Program ManageAccounts.java 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 175-180 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 Account.java). 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 ManageAccounts.java - 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.

  8. Make sure both Account.java and ManageAccounts.java are indented properly (CTRL-x CTRL-p followed by CTRL-ALT-\).

Writing Classes: A Student Class

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

  2. Program Grades.java 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 Grades.java 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).

  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 Student.java) 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.

  • Make an archive file of your work as you did last week: Be sure you are in your lab9 directory then type the following command:
     
     
       zip YourName *
     
     
    
    Be sure to replace YourName with Your last name. This command compresses all of the files in your current directory (the * is a wildcard that denotes all of the files) and places them in a file named YourName.zip (with your name in place of "YourName").

  • When you're done, do an ls and you should see YourName.zip in your directory.
  • Submit your zip file on the course Inquire site.
  • If at some point you need to extract the contents of the zip file use the following command:
     
       unzip YourName.zip