CPSC 120 Lab 11
The static Modifier, Wrapper Classes and Dialog Boxes

As usual, create a lab11 subdirectory for today's lab, open up Netscape and the Web version of this handout, and open emacs.
  1. File Account.java contains a class representing a bank account similar to the one we used in Lab 9. (Note that the constructor has been modified so that it generates the account number randomly.) Save this file to your directory and use it in the exercises below.
    1. Suppose the bank wants to keep track of how many accounts exist.
      • Declare a private static integer variable numAccounts to hold this value. It will automatically be initialized to 0.
      • Add code to the constructor to increment this variable every time an account is created.
      • Add a static method numAccounts that returns the total number of accounts. (Think about why this method should be static -- its information is not related to any particular account.)
      Compile your revised Account class. Now open TestAccounts1.java, study it to see what it does, and save it to your directory. Use it to test your Account class.

    2. Add a method to the Account class that allows money to be transferred between accounts. This could be done by using two separate methods transferIn(Account otherAcct, double amt) and transferOut(Account otherAcct, double amt), or by using just one method transfer(Account otherAcct, double amt) and defining (and clearly documenting!) it so that it transfers money FROM the current account TO the account that is passed as a parameter. Use this second strategy (just one method). Inside the transfer method you can manipulate the balance directly or use the deposit and withdraw methods you have already written! Either way, first you'll still have to check that the balance in the from account is sufficient, and print a message if it is not.

    3. Now save TestAccounts2.java to your directory. Add code as indicated by the comments to transfer $50 from Madison's account to Spud's account and then to transfer $25 from Spud's account to Madison's account. Print both accounts after each transfer. Don't fill in the rest of the missing code yet. Run it to be sure it works.

    4. Add a static method to the Account class that lets the user transfer money between two accounts without going through either account. You can call the method transfer just like the other one -- you are overloading it, but since one is static and the other is not there is no confusion. Your new method should take two Account objects and an amount and transfer the amount from the first account to the second account. The signature will look like this:
        public static void transfer(Account fromAcct, Account toAcct, double amt)
      
      Compile your revised Account class.

    5. Following the next comment in TestAccounts2.java, fill in code to use the static transfer method to transfer another $10 from Spud's account to Madison's account. Test your program.

    6. Add a method void close() to your Account class that closes an account. It should set the name to null, the balance to 0, and the account number to 0. It should also decrement the total number of accounts.

      Also add a static method Account consolidate(Account acct1, Account acct2) to your Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned. Two important rules of consolidation:

      • Only accounts with the same name can be consolidated. The new account gets the name on the old accounts.
      • Two accounts with the same number cannot be consolidated. (This would be an easy way to double your money!)
      Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old accounts; print a useful message and return null.

    7. Following the comments in TestAccounts2.java, add code to create a second account for Madison with a balance of 500. Print this account and the total number of accounts. Then consolidate Madison's two accounts into account madConsolidated; print all three accounts and the total number of accounts.

    8. Finally, add code to consolidate Spud's account with itself, putting the new account into spudConsolidated. Of course, this consolidation won't work, but it's useful to see what happens when you try it. Print both spudAcct and spudConsolidated after the call.

    Print Account.java and TestAccounts2.java to turn in.

  2. Wrapper classes are described on pages 283-287 of the text. They are Java classes that allow a value of a primitive type to be "wrapped up" into an object, which is sometimes a useful thing to do (we'll see why next semester). They also often provide useful methods for manipulating the associated type. Wrapper classes exist for each of the primitive types: boolean, char, float, double, int, long, short, and byte.

    1. Write a program IntWrapper that uses the constants and methods of the Integer class (p. 840-841) to perform the tasks below. You'll have to look through the descriptions to find what you need. Be sure to clearly label your output. Test your program after each part!
      • Print the maximum and minimum possible Java integer values. Use the constants in the Integer class that hold these values -- don't type in the numbers themselves.
      • Prompt for and read in an integer, then print the binary, octal and hexadecimal representations of that integer.
      • Prompt for and read in a numeric value in decimal, octal, or hexadecimal and print the equivalent decimal value. Read the value as a String, then use the appropriate method to get its decimal value. In Java you can indicate that a value is in octal by preceding it with a 0; for hex, precede it with 0x. So if the user enters 47, your program should print 47 as the decimal equivalent; if the user enters 047, your program should print 39 (the decimal equivalent of 478); and if the user enters 0x47, your program should print 71. Note that there is a single method of the Integer class that can handle any of these inputs.
      • Prompt the user to enter two decimal integers, one per line. Use the readString method of the Keyboard class to read each of them in. (This seems strange, but as we'll see later sometimes you have to do it.) Now convert the strings to ints (use one or more methods of the Integer class to do this), add them together, and print the sum.

    2. Write a program CharWrapper that prompts for and reads in a single character and uses the methods of the Character class (p. 788-790) to perform the tasks below. Be sure to clearly label your output. Test your program after each part!
      • Print the upper case version of the character (or the character itself if it's not a letter).
      • Print the lower case version of the character (or the character itself if it's not a letter).
      • If the character is a digit, print "It's a digit"; otherwise print "It's not a digit."
      • If the character is a letter, print "It's a letter"; otherwise print "It's not a letter."
      • If the character is whitespace, print "It's whitespace"; otherwise print "It's not whitespace." Whitespace is defined as a space, tab, or newline. The Keyboard method readString will skip over newlines looking for a string, but it will read spaces and tabs, so use them to test this part.

    Print IntWrapper.java and CharWrapper.java to turn in.

  3. We have been using the Keyboard class to get input from the user, but there are other possibilities as well. One is to use dialog boxes, which are described on pages 301-303 of the text. A dialog box is a window that pops up and allows the user to interact with it. It might ask for text input, provide choices, or simply present a message. Dialog boxes are created using static methods of the JOptionPane class; two of the most useful methods are listed below:

    Caution: The signature for showMessageDialog is wrong in the text (p. 301), where it says that it returns int, and the signature for showInputDialog is wrong in the appendix (p. 853), where it says that it returns void.

    File Dialogs.java contains a simple program that uses dialog boxes to prompt the user for two test grades and then displays the average of those grades. Study this program; note that the value returned by the showInputDialog method is a String, so the parseInt method of the Integer class must be used to gets its integer value.

    Now save it to your directory and run it. (When you're done you'll have to press Ctrl-c to get back to the prompt.) After doing it the usual way, try entering a value that is not an integer. The exception you see comes from the parseInt method -- it's saying that the value entered is not a legal format for the kind of number required (int).

    Modify the Dialogs class as follows (use dialog boxes for all input and output):

    1. Instead of just entering two test grades, ask the user how many grades there are to enter. Use a for loop to display an input dialog box to enter each grade, and at the end use a message dialog box to display the average.

    2. When the user enters a grade, check to see if it is between 0 and 100. If not, display an input dialog box saying that the number must be between 0 and 100 and get a new value until it is in the appropriate range.

    Print Dialogs.java to turn in.

What to turn in

Turn in hardcopy of Account.java, TestAccounts2.java, IntWrapper.java, CharWrapper.java, and Dialogs.java. Tar your lab11 directory and sent it to your instructor with cpsc120 lab11 in the Subject line.