Lab 5 In-Class: Exploring Data Representation & Writing Classes

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

What is your age in seconds?

The file Age.java contains the skeleton of a Java application that will take as input a person's age in years, months, and days and then compute and print the age in seconds.
  1. Complete the program as follows: Test your program. For example, a person who is 18 years, 3 months, and 21 days old has lived 577,238,400 seconds (under the assumptions of the program). A person who is 21 years, 0 months, and 0 days has lived 662,256,000 seconds.

  2. How many seconds has a person who is 68 years, 6 months, and 12 days old lived? Run your program to find out.

    You should have gotten a strange answer to the last question! Why did your program produce such an answer? Hint: Java uses a 32-bit two's complement representation for type int. Given this,

    Now look at the other ages in seconds you have computed, e.g., 21 years. 68 years is a little over triple that value; is this consistent with your strange answer? (No need to write this answer.)

  3. Fix your program as follows:

    Test the program again and make sure it works. A person who has lived 68 years, 6 months, and 12 days is 2,161,036,800 seconds old.

Writing Classes: The Account Class

Program Account.java contains the Account class from the pre-lab.

  1. Fill in your definitions for methods printSummary, chargeFee (returning the new balance) and changeName. Compile your class.

  2. Program ManageAccounts.java contains the shell program from the prelab that uses the Account class to create and manipulate bank accounts. 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.

  3. Although chargeFee returns the new balance, your ManageAccounts program currently 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.
    4. In the printSummary method add a statement to print the number of transactions.
    5. Classes often have accessor methods for each data value (see page 165 of the text). The getBalance method is an example of an accessor method. 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.
    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).

  5. Add an accessor method getName() to the Account class to return the name on the account.

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

  7. Print Account.java and ManageAccounts.java.

Writing Classes: Graphical Objects

In this exercise you will complete an applet that contains three snowmen holding balloons. Both the snowmen and the balloons will be graphical objects defined in separate classes.
  1. The file Snowman.java contains the class that defines a graphical snowman (the same as the one on pages 103-104 except the code for the snowman has been encapsulated in a class). The file DrawSnowman.java contains a program that draws a sun and some steps. It instantiates a Snowman object and invokes the draw method of the Snowman class to draw the snowman. Save these files to your directory, compile the DrawSnowman.java program (which will also compile Snowman.java) then use the HTML file DrawSM.html and the appletviewer to view the applet.

  2. Modify DrawSnowman.java as follows. Instantiate and draw two additional snowmen, one positioned on the step to the left of the current snowman and one to the right. Compile the program and view it. Make sure the snowmen are in the correct positions.

  3. Listing 4.9 on pages 182 - 184 is the definition of a Circle class. The program on page 181 instantiates 5 Circle objects and draws them (note - they are drawn on a Panel rather than an Applet but the basic idea is the same as what we have been doing). The file Balloon.java contains a class similar to the Circle class (the accessor and mutator methods are not included). The file DrawBalloons.java instantiates and draws three objects of type Balloon. The file DrawB.html is an HTML file you can use for viewing the DrawBalloons applet. Save these files to your directory, compile, and run using the appletviewer. You should note that the "balloons" don't look like balloons (they are just elongated circles with a width that is .9 times the height).

  4. Modify Balloon.java so what is drawn looks like a balloon! To do this add a string (a straight line) hanging down from the balloon. The string should be twice as long as the balloon is high. Recompile DrawBalloons.java and see how the balloons look.

  5. Go back to the DrawSnowman program and for each snowman instantiate and draw a balloon object so it looks like the snowman is holding the balloon (in either hand - you choose).

  6. Print out Balloons.java and DrawSnowman.java to hand in.

Hand In