Instructor: Phill Conrad
TAs: Esra Kucukoguz, Yiming Li, Murali Yeleswarapu
Primary TA for this lab: Yiming Li
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.
It will really help if you do. A lot of questions that the TAs and I get are answered here.
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.
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.
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.
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:
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.
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:
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.
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.
Starting up bluej works differently in Cooper lab vs. CSIL. In both cases, you start with a Terminal window.
~pconrad/cs10/cooper/bluej/bluej~pconrad/cs10/csil/bluej/bluejMake sure you can start BlueJ. When it comes up, it may look something like this.



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:


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:

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

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:

This should allow you to run the method, and see Hello World pop up, as shown below:
(Just click OK here...)
Then you should see this window come up:

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.
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.
Note: if you are working from home, you can also use a secure file transfer program (such as scp, sftp, fetch, etc.) to copy this directory onto your PC or Mac. The idea is the same.
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:


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.

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:

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.)

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:
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!
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:

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.
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.
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.)
This week, you'll be turning your entire lab02 directory. Here's how:
cd .. to move into the parent directory, i.e. the ~/cs10 directory. ls command to verify that the lab02 directory shows up in your listingturnin lab02@cs10 lab02You 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)