CS10, Fall 2009, Lab one (lab01)

Instructor: Phill Conrad
TAs: Esra Kucukoguz, Yiming Li, Murali Yeleswarapu

Primary TA for this lab: Esra Kucukoguz

A word about your TAs:

Starting this week, our plan is for Esra and Yiming to alternate weeks.

Murali will provide additional help as needed, and help with grading.

Goals

This week, we want to work with a Java class that comes from your textbook, from Chapter 3, and add some code to it.

Collaboration

You may work with one other student on this project, or you may work alone. If you work with another student, please list his/her name in the comments. Each of you must still turn in your work independently, however, for an individual grade.

How to Proceed

  1. Complete all of the steps from lab00 first. This is essential—you need to understand how the Cooper lab works, how to access a CSIL prompt when you need one, and how to use the turnin command, not to mention details such as editing files, and using the javac and java commands.
  2. Open a web browser, navigate to the links below in two windows, or two tabs. These links give you access to two Java classes that are the starting point for this week's lab:

    Take a look at these two classes. In a later step, you'll be asked to make some modifications to each of them, so it is worth getting to know them a bit.

    The first class, BankAccount provides two constructors for a bank account: one that creates an account object with a zero balance, and another that creates an account object with whatever balance is passed in as a parameter. There are then three methods for depositing money, withdrawing money, and checking the balance.

    The second class, BankAccountTester, consists only of a main method that we can run to test the class. In lecture we've been looking at using the JUnit features built into BlueJ to do testing—and we'll get to that in lab next week. For now, we're doing something a bit more simple. BankAccountTester just has simple main method that uses the capabilities of the BankAccount class and then prints the result, along with what we expect the result to be.

    Here's a link to the Javadoc for this class, which explains the methods it provides:
    http://www.cs.ucsb.edu/~pconrad/cs10/09S/labs/lab01/BankAccountProject/doc

    When you've spent a few moments looking over this code, continue on with the next steps.

  3. Create a directory (that's Unix-speak for "folder") called ~/cs10/lab01
    Here's how:
  4. Now, save the two Java programs you brought up in the web browser, inside your ~/cs10/lab01 directory. Save them with their original names: either BankAccount.java, or BankAccountTester.java. You can do this by one of several techniques:
  5. Next, we are going to compile these programs in their initial unmodified versions. The easiest way to do this is with the command:

    javac *.java

    This command runs the Java compiler. It indicates that we want to compile all of the files ending in .java in the current directory. The * symbol is a "wildcard" that matches any filename. If this command works, you'll just get another Unix prompt. To see if it worked, use the ls command. You should see files called BankAccount.class and BankAccountTester.class in the listing, along with BankAccount.java and BankAccountTester.java.

    If it didn't work, you'll get an error message. The most likely error message is something like what you see below. This message indicates that you don't have any files in your current directory that have names ending in .java. In this case, you are probably not in the right directory. Try the pwd and ls commands to see where you are, and what you have, and if necessary use the cd command to navigate to where you should be.

    -bash-3.2$ javac *.java
    javac: file not found: *.java
    Usage: javac <options> <source files>
    use -help for a list of possible options
    -bash-3.2$

    If you get some other error message, and you can't figure out what is wrong, you might ask your TA for assistance.
  6. Moving on, if you have the BankAccount.class and BankAccountTester.class in your directory, you are ready to try running the test program. To do that we type:

    java BankAccountTester

    Note that we don't put .java or .class after BankAccountTester when using the java command. The java command invokes the Java Virtual Machine (JVM) and says that we want to load the BankAccountTester class, look for its main(String [] args) method, and start running the code in that method.

    If it works, you should see something like this:

    -bash-3.2$ java BankAccountTester
    1500.0
    Expected: 1500
    -bash-3.2$


    The two lines of output in this program come from the two lines below, and we can see that the output from calling the getBalance() method does indeed return 1500, as we expected.

    System.out.println(harrysChecking.getBalance());
    System.out.println("Expected: 1500");

    Note, just an an exercise, what happens if you type java BankAccount at the command line.

    You'll get an error message. Questions to consider: Now we are ready to make some modifications.
  7. As a first step, change the Javadoc comments in each file to reflect the fact that you are making changes.

    In the BankAccount.java file, use a comment like this one (replace Sally Coder with your name):

         
        /**
           A bank account has a balance that can be changed by 
           deposits and withdrawals.
    
           @author Sally Coder
           @version cs10, lab01, Spring 2009 
        */ 
          

    If you work with another programmer, indicate that this way:

         
        /**
           A bank account has a balance that can be changed by 
           deposits and withdrawals.
    
           @author Sally Coder, working with Marge Inovera
           @version cs10, lab01, Spring 2009 
        */ 
          

    Use a similar format to change the comments in the BankAccountTester.java file. We'll use this format throughout the course, so I won't explain it in this much detail every time—I may just refer back to this lab.

  8. Now, we are ready for the modifications. (The following is taken from exercise P3.2 in your textbook):

    Add a method:

       public void addInterest( double rate)

    to the BankAccount class that adds interest at the given rate. For example, after the statements:

      BankAccount momsSavings = new BankAccount( 1000);
      momsSavings. addInterest( 10); // 10% interest

    the balance in momsSavings should be $1,100. Also supply a BankAccountTester class that prints the actual and expected balance.

    I'd encourage you to proceed in this fashion:

  9. Turn in step: This week, you'll be turning in two programs, not just one. So, to accomplish that, the easiest thing to do is this:
  10. You are finished—there is no paper component to hand in with this lab.

Grading: This lab is worth 100 pts, distributed as follows. Partial credit may be awarded for each step at the discretion of the TA/Instructor.

Due Date: You should do your best to complete this within the assigned lab time 04/09 or 04/10. If that is not possible, then you should complete the assignment and execute the turnin command no later than 4pm 04/17. This gives you one full week to seek help during office hours from both the instructor and the TAs, plus one additional lab period to ask the TAs for additional help.

Submissions turned in after that time are subject to receiving zero credit.


End of lab01