CS10, Fall 2009, lab02

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

Primary TA for this lab: Yiming Li

Important note

Please read all of the instructions before staring this lab—even the section that says "Goals". Here, let me put it in hideously ugly colors, and a different font, in order to try to get your attention.

Read ALL of the instructions before starting! (Even the "Goals" section!)

It will really help if you do. A lot of questions that the TAs and I get are answered here.

Goals

(1) Reinforce basic concepts: instance variables, constructors, getters/setters

I've seen that many of you are still not entirely comfortable with a few basic concepts of object-oriented programming in Java—even those of you that have had a Java course before. These basic concepts are:

Because these concepts are so fundamental, this week's lab is going to give you lots of practice on writing these.

(2) Introduce the toString method

In addition, I want to introduce one other fundamental method you need to learn how to implement for a class, namely the toString() method. This is a method that helps you format how an object prints if we do something like this (assume here that the Student constructor shown below takes name and permNumber as its parameters):

Student s = new Student("Phill",1234567);
System.out.println("s=" + s);  

If we don't implement a toString() method, what we'll see that what we get is something sort of ugly. The output might look like this:

s=Student@e13e07

When, what we really want is something like this:

s=(name: Phill, perm: 1234567)

We'll see that we can get this result instead, by simply implementing a method like this:

 public String toString()
 {
     return "(name: " + this.name + ", perm: " + this.permNumber + ")"; 
 }

In fact, just by implementing a toString() method, we can customize how objects appear when they show up in the output of System.out.println. This is a very powerful technique, and can be of great help to us later in debugging large programs.

(3) Give practice with JUnit style unit testing

In lecture, I've been showing you how to write unit tests using JUnit. In this lab, you'll get lots of practice with using JUnit in BlueJ. This will set up us for practicing later with writing your own JUnit tests.

Overall Structure of the Lab

Read this before starting—this gives you the big picture, so that you understand WHY you are doing what you are doing.

The detailed instructions come later on this web page.

After an initial "step 0" to get BlueJ configured, this lab comes in two parts:

  1. In the first part, I walk you step-by-step through a class for a Student with instance variable (name and permNumber), a couple of constructors, getters and setters, and a toString method. In this part, I tell you exactly what to do. It is important though that you don't just proceed in robot-like fashion—you need to understand what you are doing, because at later stages, you'll be asked to do the same thing we've done here, but without the extra hand-holding.

    We proceed in four stages:
    1. I'll walk you through understanding the purpose of the class, and what instance variables will be required.
    2. I'll walk you through writing stubs for all the methods, and understand the purpose of each one
    3. I'll walk you through writing JUnit tests for each of the methods, compile, and watch the methods fail
    4. I'll walk you through how to implement the methods properly, compile, and watch the tests pass.

    This is the exactly the same appraoch we'll take in the three additional parts of this lab that follow—except you won't get as much "hand holding". So you should refer back to the instructions for part one if you are stuck at any point.

  2. In the second part, I'll give you a description of the class for a ComplexNumber (with instance variables real and imag, for the real and imaginary part of the number), along with JUnit tests for the class. However, the class itself will be empty. You'll have to fill in the code for the instance variables, the constructors, the getters and setters and the toPrint method.

    Use the same four steps (a,b,c,d) from part 1. But this time:
    1. I'll walk you through understanding the purpose of the class.
    2. I'll tell you what methods you need, and you need to write the stubs for them yourself (you won't be given any code—just some suggestions as to what to write—you'll need to come up with the details yourself.)
    3. I'll give you the JUnit tests for each of the methods—you can compile these, and run the tests and watch the methods fail
    4. Then—on your own—you'll implement the methods properly, compile, and watch the tests pass.

    So in part 1, I gave you both the class and the tests. In part 2, I gave you the tests, and you had to write the class. I think you can guess what is coming next...

In a future lab, we'll repeat these steps, for additional practice but with a few twists:

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

Step 0: Preliminaries (setting up BlueJ)

To start on this week's lab, you need to be sure that you can successfully startup BlueJ, and you need to configure BlueJ to show the testing tools (i.e. the Run Tests button.)

There is nothing to turn in from Step 0—this is all just to be sure that BlueJ is working properly before you proceed.

Step 0a: Start up BlueJ

Starting up bluej works differently in Cooper lab vs. CSIL. In both cases, you start with a Terminal window.

Make sure you can start BlueJ. When it comes up, it may look something like this.

Blue J on X Windows, initial start

Wou should be able to choose "Project/New Project" to create a new project called BlueJPractice in your lab02 directory. The images below show what that might look like:

Creating BlueJPractice project on BlueJ under X11

BlueJPractice project in BlueJ on X11

You should now be able to create a class called "HelloWorld" by clicking on the "New Class" button, and filling in HelloWorld. That will end up looking like this:

Creating HelloWorld class in BlueJ

HelloWorld Class as it initially shows up in BlueJ

You can then double click on HelloWorld to start editing it. Initially it will be full of a sort of "template" of a class, like this:

Initial contents of a HelloWorld class in BlueJ (the template code)

For our purposes today, just get rid of all of what is there, and replace it with your typical HelloWorld class, like this:

Hello World code as it appears when you are editing in BlueJ

To then try compiling and executing this, click the Compile button. When it compiles with no errors, to run it, right click—control click on Mac—the HelloWorld icon on the main project screen, and select the main method, like this:

right click on main method of HelloWorld class in BlueJ

This should allow you to run the method, and see Hello World pop up, as shown below:

Main method dialog for Hello World in BlueJ

(Just click OK here...)

Then you should see this window come up:

Hello World console output in BlueJ

Now, save your project, and we're done with that short introduction to BlueJ.

There are a few more things to do in Step 0 (the preliminaries) before the real work begins.

Step 0.2 Copy the starting point files (lab02Proj) from my account

Open a command line prompt in your ~/cs10/lab02 directory, and use the following command. Notice the space and the period at the end of the line—those are very important!

cp -r /cs/faculty/pconrad/public_html/cs10/09S/labs/lab02/lab02Project .

This command will copy a directory containing several Java files, plus all the extra files for a BlueJ project into your directory.

Step 0.3 Open the starting point files in BlueJ

Open BlueJ, and use the "Open Project" menu item to navigate to the lab02Project inside your ~/cs10/lab02 directory. You should be able to open this project, and see something like this:

Opening up lab02Project in BlueJlab02Project BlueJ start point screen shot

Step 0.4 Enabling the Testing Tools in BlueJ

The last thing you'll need to do before we get started is enable the testing tools. To do that, select Preferences from the Tools menu, as shown here.

Preferences Menu in BlueJ on X Windows

Under Preferences, find the tab that says: Miscellaneous, and the item that says "Show Unit Testing Tools". You want to click that box, as shown in the picture below:

The Show Testing Tools item in the BlueJ preferences

Click OK, and then your main BlueJ screen should look like this. Note the Run Tests button at the left hand side of the screen (that wasn't there before.)

BlueJ on X windows with Run Tests enabled

 

Now the real work begins!

Step 1: Understaning how to work with a basic class and test class in Java

In your window, you'll see two basic Java classes:

You'll also see two test classes based on JUnit, StudentTest, and ComplexTest.

In Step 1, we'll only be working with Student, and StudentTest. (Complex and ComplexTest are for step 2.)

Open up the Student class.

Here's what you need to do:

Step 1.1: Correctly implement the constructor

A constructor's job is just to initialize the instance variables in the object, either with some reasonable default value, or with the values passed in as parameters.

In this case, what we need inside the constructor is two lines of code:

 this.name = theName;
 this.permNumber = thePermNumber;

In any case of of constructor, the general rule is: look at each instance variable, and make sure it has a reasonable initial value.

If the constructor has parameters, that's usually where the values are coming from!

Step 1.2 Correctly implement the getter functions

A getter functions job (e.g. getName(), getPermNumber()) is to return the value of an instance variable that is private, and make it available to class users. These functions have a return type of String, int, double, etc.—one that matches the instance variable we are getting.

Typically, all we need is a line of code such as:

return this.name;

or

return this.permNumber;

Find the getter functions for this class, and put these lines of code inside the correct ones.

If you do this correctly, you should now be able to run the tests and get at least one to pass—the one that tests the constructors and the getters. Click the "Run Tests" button in the main project window, and you should see a red bar, but at least one green check, like this:

red bar, but one green x

Step 1.3 Correctly implement the setter functions

Setter functions such as setName, setPermNumber are designed to give a private instance variable a new value. We typically pass in the new value as a parameter. These functions are usually void.

Find the setter function for name. Note that the parameter value is newName. So, we need to set the instance variable this.name to be equal to newName:

this.name = newName;

We do this inside the body of the setName method.

Do something similar for the setPermNumber method, and now some more tests should pass!

Note that we don't have to put this. in front of this.name—we could just write:

name = newName;

Including this. is optional, but it makes the code more clear. Instance variables are a part of this object—the one we are invoking the method on.

Step 1.4 Correctly implement the toString method.

The toString method provides a way to nicely format an object.

Here is an example of how we might nicely format a student object. You can fill this code in for the toString method:

  return "(name: " + this.name + ", perm: " + this.permNumber + ")"; 

 

Once you've done all of this, the JUnit tests in StudentTest should all pass! When you see the green bar, you are ready to move on to Step 2.

Step 2: Practice with implementing a class yourself

Now, take a look at the Complex class. This one has a bunch of JUnit tests written for you, but the class itself is empty, except for some comment to guide you.

You need to create instance variables of type double called "real" and "imag", to stand for the real and imaginary parts of a complex number.

You then need to fill in the constructor (two of them in this case), as well as getters, setters, and a toString method.

Your job is to fill in the code in the Complex class until all the JUnit tests for ComplexTest compile, and they all pass. You should NOT change the ComplexTest class—only the Complex class (by filling in the missing code.)

You can use the Student class as an example, but keep in mind that this isn't an exact comparison—Students have "name" (a string) and perm numbers (integers). Complex numbers have a real part (double) and an imaginary part (double.)


Turn in Step

This week, you'll be turning your entire lab02 directory. Here's how:

You are finished—there is no paper component to hand in with this lab.


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

Additional guidelines:

Due Date: You should do your best to complete as much of this lab as possible within the assigned lab time 04/09 or 04/10—however it is very possible that you will not finish. You may continue to work on the assignment over the course of the week by visiting the CSIL lab, or by downloading your files to your own computer where you have BlueJ installed. At the end, you'll transfer the files back to CSIL to execute the turnin step. We'll discuss ways of transfering these files in lecture.

You are encourage to complete the lab as soon as possible—before the midterm exam, if possible, because it will help you prepare for the midterm exam on 04/24.

However, I'll accept this assignment up without penalty up until noon Thursday on 04/30.

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


End of lab02 (v2.0)