CS 101 Laboratory #7
I/O and Object-Oriented Programming

Objective: To gain experience working with programs that perform input/output and error handling, and to gain experience with object-oriented programming by building a simple class.
Due date is the beginning of your lab section next week:

Lab prep: Read this lab assignment and review the slides and examples from lectures 18-21. Additional information can be found in J 3.3, 3.4, 6.4, and 8.

Written Exercises: None this week.


Lab Exercises

Start by creating a new hw07 folder within your cs101 folder.
  1. Recall the sample C program we saw in class recently called wordcount.c that counted the number of characters, words, and lines in an input file. Write a Java program called WordCount.java that counts the number of words in a file. Let the name of the file be specified as a command-line argument. Be sure to perform appropriate error handling. An example run on the file test.txt is below:

    this is a 3
    line file with
    eleven words in it.
    
    > java WordCount
    Usage: java WordCount <file name>
    > java WordCount test.text
    Error: can't find file test.text
    > java WordCount test.txt
    Number of words in test.txt: 11
    
    Test your program with different input files. If you want to check your results, here is a sample application that you can save in your hw07 folder: WordCountSample.class.

    Submit a copy of your Java source code for WordCount.java along with some sample output (you can print text from the Interactions pane by right-clicking in it and selecting "Print Interactions").

  2. Let's move on to object-oriented programming. You will be modifying BankAccount.java, which defines a class very similar to the Account class from lecture 19. First, save the BankAccount.java file to your hw07 folder, load it into DrJava, and compile it. In the Interactions pane, experiment with creating BankAccount objects and issuing commands to them. For example, you might interact with the Java interpreter as follows:

    > BankAccount mary = new BankAccount (7, 100);
    > mary
    Account #7
    > mary.deposit(50.25);
    Deposited $50.25.
    > mary.withdraw(200);
    Sorry, you must withdraw a positive amount of money up to $150.25.
    > mary.withdraw(140);
    Withdrew $140.0.
    > mary.printBalance();
    Your balance is $10.25.
    
    Now, modify the code so that a password is a required argument for creating an account, as well as for invoking the withdraw and printBalance methods. A sample transcript of how the modified BankAccount class should behave after making the changes for the previous exercise and this one is shown here:
    > BankAccount jane = new BankAccount(135, "apple", 2000);
    > BankAccount john = new BankAccount(277, "pear", 500);
    > jane.deposit(50);
    Deposited $50.0.
    > jane.deposit(-80);
    You must deposit a positive amount of money.
    > jane.printBalance("aapl");
    Sorry, incorrect password.
    > jane.printBalance("apple");
    Your balance is $2050.0.
    > john.withdraw(200, "pare");
    Sorry, incorrect password.
    > john.withdraw(200, "pear");
    Withdrew $200.0.
    > john.withdraw(500, "pear");
    Sorry, you must withdraw a positive amount of money up to $300.
    > john.withdraw(300, "pear");
    Withdrew $300.0.
    > john.deposit(2000);
    Deposited $2000.0.
    > john.printBalance("pear");
    Your balance is $2000.0.
    
    If you want to check your results, here is a sample class file that you can save in your hw07 folder: BankAccountSample.class.

    Turn in a printout of your source code, as well as a transcript (like the one above) showing that your code works correctly.

  3. A "Buggle" is a creature that moves on an infinite grid in the x-direction or y-direction, and has an orientation east, north, west, or south. (Unlike the Buggle example shown in class, we will not represent the color or the brushUp / brushDown property here.) A buggle can respond to commands to move one or more grid cells forward, or stay in the same location and turn 90 degrees to the left or right from its current orientation. The state of a Buggle can always be described by giving its x-coordinate, y-coordinate, and direction (EAST, NORTH, WEST, or SOUTH). When a new Buggle is constructed, it should be at coordinates (1, 1) facing EAST.

    Develop a class called Buggle that implements these creatures. Your code should provide functionality for the following methods:

      // construct a new Buggle
      public Buggle()
    
      // move forward one grid cell
      public void forward()
    
      // move forward 'k' grid cells
      public void forward(int k)
    
      // rotate 90 degrees to right (EAST -> SOUTH -> WEST -> NORTH -> EAST)
      public void right()
      
      // rotate 90 degrees to left (EAST -> NORTH -> WEST -> SOUTH -> EAST)
      public void left()
    
      // return a string representation of the object
      public String toString()
    

    Write another class to test your Buggle class. For example, suppose the following code is in the main method of a class called BuggleTest:

            // create a Buggle
            Buggle becky = new Buggle();
            // print it (this invokes the toString() method)
            System.out.println("becky: " + becky);
    
            // move becky around a bit and keep printing it
            becky.forward();  System.out.println("becky: " + becky);
            becky.left();     System.out.println("becky: " + becky);
            becky.forward(5); System.out.println("becky: " + becky);
    
            // create another Buggle and print it
            Buggle bobby = new Buggle();
            System.out.println("bobby: " + bobby);
        
            // move bobby without printing
            bobby.right();    
            bobby.forward(5);
            bobby.right();   
            bobby.forward(20);
            bobby.left();
    
            // move becky some more
            becky.left();
            becky.forward(3);
        
            // report final state of both Buggles
            System.out.println("becky: " + becky);
            System.out.println("bobby: " + bobby);
    

    The output of this code would be:

    > java BuggleTest
    becky: x=1, y=1, direction=EAST
    becky: x=2, y=1, direction=EAST
    becky: x=2, y=1, direction=NORTH
    becky: x=2, y=6, direction=NORTH
    bobby: x=1, y=1, direction=EAST
    becky: x=-1, y=6, direction=WEST
    bobby: x=-19, y=-4, direction=SOUTH
    

    Write a BuggleTest class with similar functionality. Turn in a printout of your Java code for both classes: Buggle.java and BuggleTest.java. Also turn in a copy of your sample output.

    If you want to check your results, here are sample class files that you can save in your hw07 folder: BuggleSample.class and BuggleTestSample.class.


  4. Extra Credit (relating to problem 1): Extend your WordCount program from problem 1 to count and report the number of characters, words, lines, and paragraphs in an input file (where paragraphs are separated by blank lines). Submit a copy of your Java source code and sample output. Be sure to save this version in a different file than your solution to problem 1, and submit copies of both programs.

  5. Extra credit (relating to problem 2): Create a class named LinkedAccount. A LinkedAccount object is linked to an existing BankAccount object so that all deposit or withdraw operations affect the same account. Creating a LinkedAccount involves providing three arguments: a BankAccount object to link to, that account's password, and the password for the new LinkedAccount. If the password provided for the existing account is incorrect, you may simply print an error message and exit. You may find it useful to add a boolean verifyPassword method to the BankAccount class that takes a test password as its argument and returns true if it matches the account's password and false otherwise. Here is a sample transcript showing how the LinkedAccout class should behave:
    > BankAccount jane = new BankAccount(135, "apple", 2050);
    > BankAccount john = new BankAccount(277, "pear", 2000);
    > LinkedAccount ron = new LinkedAccount(john, "pear", "grape");
    > john
    Account #277
    > ron
    Account #277
    > ron.deposit(100);
    Deposited $100.
    > ron.printBalance("pear");
    Sorry, incorrect password.
    > ron.printBalance("grape");
    Your balance is $2100.
    > ron.withdraw(300, "grap");
    Sorry, incorrect password.
    > ron.withdraw(300, "grape");
    Withdrew $300.
    > ron.printBalance("grape");
    Your balance is $1800.
    > john.printBalance("pear");
    Your balance is $1800.
    
    Turn in a printout of your source code, as well as a transcript (like the one above) showing that your code works correctly.

  6. About how long did it take you to complete this assignment?

Back to Computer Science 101 Home
Department of Computer Science