CPSC 124 Introduction to Programming Spring 2008

Lab 8: Classes and Objects

Introduction

In this lab, you'll learn how to write basic object-oriented programs.

Setup

Create a lab08 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s08/cs124/labs/lab08/ to your lab08 directory.

Classes and Objects

This lab covers Sections 5.1 through 5.4 of the book. If you don't have your book with you, the book can be found online at http://math.hws.edu/javanotes5/.

Exercises

Here are the exercises for this week's lab, due next Thursday.

  1. Within a file called Counter.java write a class called Counter that represents a counter that counts 0, 1, 2, 3, 4, .... The Counter class should have one private instance (i.e., non-static) variable representing the value of the counter. It also has two instance (i.e., non-static) methods: increment() adds one to the counter value, and getValue() returns the current counter value.

    Within a second file called CoinToss.java, You will then write another class called CoinToss that simulates 100 coin tosses and keeps track of the number of heads and tails using two counter objects: headCount and tailCount. Your class CoinToss will look like the following, although you will have to fill in the blanks (i.e., _____________________):

    class CoinToss {
      public static void main(String[] args) {
        Counter headCount, tailCount;
        headCount = new Counter();
        tailCount = new Counter();
        for (int flip = 0; flip < 100; flip++) {
          if (Math.random() < 0.5)
            _____________________;
          else
            _____________________;
        }
        System.out.println("There were " + _____________________ + " heads.");
        System.out.println("There were " + _____________________ + " tails.");
      }
    }
    
    When you have completed both classes, compile each file and run the program using the following:
    bash$ javac Counter.java
    bash$ javac CoinToss.java
    bash$ java CoinToss
    
    Note: you need to compile both files (actually, in some scenarios you only need to compile one file, but just to be safe you should compile both) and run only the second file, since the second file contains the main method.
  2. Within the file Die.java create a class called Die that represents a single, 6-sided die (like the die used in games such as Craps). In particular, your Die class should contain the following:

    After you finish writing Die.java, open the provided file Craps.java, which contains the program you wrote for lab 4. Modify the class Craps to use Die objects to simulate rolling the dice rather than ints. You will need to change the die1 and die2 variables to type Die rather than int and manipulate them as Die objects (i.e., use the methods that you wrote in the Die class). There should be no calls to Math.random() when you have finished modifying the Craps class.

    When you finish modifying Craps.java, compile both Die.java and Craps.java, and run Craps. Make sure the program works like it's supposed to (the probability should be around 49.3% chance of winning).

  3. Within a file called BankAccount.java create a class called BankAccount to represent a bank account. In particular, the class should have the following elements:

    After you finish writing BankAccount.java create a class called Banking.java, which contains a single main method. The main method should create two BankAccount objects, deposit and withdraw amounts from them, and print the balance after each transaction. Make sure that you call each method in the BankAccount class at least once.

    When you finish writing Banking.java, compile both BankAccount.java and Banking.java and run Banking. Make sure that your program works correctly.

  4. In this exercise, you will write your own String class called NewString. You may not use the original, built-in String class in Java to implement NewString, but instead, will implement NewString using an array of characters. This array will represent the state of each NewString object. Your NewString class will also contain constructors and methods (similar to the String class) for manipulating the string.

    Below is a list of the variables and methods you will need to define in your NewString class. In particular, your NewString class should contain the following:

    If you're having trouble understanding how any of the methods above should work, test them out with conventional Strings. Your NewString methods should work similarly.

    Write your NewString class in the file NewString.java. In addition, write a class NewStringTest in a file called NewStringTest.java, which contains a main() method for testing your NewString class. Write some code that creates some NewString objects and calls the various methods. Your code should call each method you wrote in NewString at least once.

Handin

Verify that your lab08 folder contains all of the files you created or modified for this lab (including at least Counter.java, CoinToss.java, Die.java, Craps.java, BankAccount.java, Banking.java, NewString.java, and NewStringTest.java), then copy your entire lab08 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).