CS10, Fall 2009, lab04

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

Primary TA for this lab: Esra Kucukoguz

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!
(Especially the "Goals" and "Overview" sections!)

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

Goals


Collaboration

On this project, you may work in pairs, but if you do, you still each need to turn in a separate submission, and you should indicate in a comment in your code who it was that you worked with.

A reminder that copying code electronically from one person to another is NOT permitted for this assignment—don't email code, or send it back and forth through a chat program, or anything like that. You may look at each others code and discuss each others code, but you may not type for each other, or copy code directly. (You may wonder how we can tell----it is usually typos, and small formatting glitches that are the tell-tale signs of direct code copying)

Another way to say this is—if you work together, you may "share with your mouth and your eyes, but not with your fingers."


Overview

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.

In the preliminaries, you'll just copy some code from the course web site into your account.

You'll also see that the

  1. In the first part, you'll look over the code you've copied to understand what you have. You'll see that you have:

    In the detailed instructions for Part 1, we'll guide you through how important it is to learn how to read code, not just write code. We'll point out several things you should notice as you read through these files.

  2. In the second part, you'll first go through the ComplexTest class, and look at the test methods, to understand how they work. Many of these are similar to the ones you say in lab02—and a few are new. Then, there are a few places where you need to add some test methods. The instructions below will walk you through doing that.

    There are several reasons for asking you to add more JUnit tests.

  3. In the third part, you'll first work through the Complex class until all of its test methods pass, and then through the QuadraticEqn class until all of its test methods pass. It is important to do it in that order, since the QuadraticEqn class depends on the Complex class.

  4. In the fourth part, you'll first look at the QuadraticSolver, which solves quadratic equations based on user input. There is no code you have to add here—this code is here as an example of how a "real program" can be supported by the work you do on basic classses and unit tests (that is, the work you did on QuadraticEqn, Complex, and its two unit test classes.) Then, you'll write your own program from scratch—using the QuadraticSolver as a model.

    Your own program will be a class called ComplexCalc, and will allow users to enter two complex numbers, and then show both the sum and product.

    Your program should allow the user to continue as many times as they want, or quit—just like the QuadraticSolver.

Sample Sessions

Here's a sample session from QuadraticSolver

Please enter values for a quadratic equation below.


Please enter a value for a:1
Please enter a value for b:2
Please enter a value for c:-15
Two real roots: 2.0 and -6.0
Do you want to calculate another equation? Y/N
Y
Please enter values for a quadratic equation below.


Please enter a value for a:1
Please enter a value for b:2
Please enter a value for c:15
Complex roots: -1.0 + 3.7416573867739413i and -1.0 + -3.7416573867739413i
Do you want to calculate another equation? Y/N
Y
Please enter values for a quadratic equation below.


Please enter a value for a:1
Please enter a value for b:0
Please enter a value for c:4
Complex roots: -0.0 + 2.0i and -0.0 + -2.0i
Do you want to calculate another equation? Y/N
Y
Please enter values for a quadratic equation below.


Please enter a value for a:0
Please enter a value for b:0
Please enter a value for c:0
Can't calcuate any roots: a is 0, and b is 0
Do you want to calculate another equation? Y/N
Y
Please enter values for a quadratic equation below.


Please enter a value for a:0
Please enter a value for b:1
Please enter a value for c:2
One degenerate root: -2.0
Do you want to calculate another equation? Y/N
N
Goodbye!

      
Here's a sample session from the ComplexCalc program that you'll write in Step 4 of this lab:

Please enter values for two complex number c1 and c2, each of the form a + bi



Entering value for c1

Please enter a value for a:a
Sorry, an error occurred: java.lang.NumberFormatException: For input string: "a"
Please try again!

Please enter a value for a:1 Please enter a value for b:2
Entering value for c2
Please enter a value for a:3 Please enter a value for b:4
c1 + c2 = 4.0 + 6.0i c1 * c2 = -5.0 + 10.0i
Do you want to perform more calculations? Y/N Y Please enter values for two complex number c1 and c2, each of the form a + bi Entering value for c1
Please enter a value for a:0 Please enter a value for b:2
Entering value for c2
Please enter a value for a:0 Please enter a value for b:-2
c1 + c2 = 0.0 + 0.0i c1 * c2 = 4.0 + 0.0i
Do you want to perform more calculations? Y/N N Goodbye!


Here's one more session from QuadraticSolver that illustrates the error handling that QuadraticSolver has for entering numbers. Your ComplexCalc program should have the same feature. You may reuse code from QuadraticSolver to implement this.

Please enter values for a quadratic equation below.


Please enter a value for a:2
Please enter a value for b:no I don't feel like it
Sorry, an error occurred: java.lang.NumberFormatException: For input string: "no I don't feel like it"
Please try again!

Please enter a value for b:really, I'd prefer not to
Sorry, an error occurred: java.lang.NumberFormatException: For input string: "really, I'd prefer not to"
Please try again!

Please enter a value for b:three
Sorry, an error occurred: java.lang.NumberFormatException: For input string: "three"
Please try again!

Please enter a value for b:3
Please enter a value for c:
Sorry, an error occurred: java.lang.NumberFormatException: empty String
Please try again!

Please enter a value for c:4
Complex roots: -3.0 + 4.795831523312719i and -3.0 + -4.795831523312719i
Do you want to calculate another equation? Y/N
n
Goodbye!

Detailed Instructions

Step 0: Preliminaries

Step 0.1 Copy the starting point files (lab04Proj) from my account

Open a command line prompt in your ~/cs10/lab04 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/lab04/lab04Project .

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

Other ways to access the code:

 

Step 0.2 (Optional, but recommended): Open the starting point files in BlueJ

Open BlueJ, and use the "Open Project" menu item to navigate to the lab04Project inside your ~/cs10/lab04 directory. This will be similar to what you did last week.

Note:

Now the real work begins!

Step 1: Familiarizing yourself with the code.

If you opened up the lab04Project correctly, you'll see In your window, you'll see five Java classes

Please spend some time looking through each of these files—reading them the way you would read a book, from top to bottom. Look at the Javadoc comments for each method, and then read through the method to see how the method is implemented—and indeed, whether the method is implemented yet, or whether it is one you are going to have to implement yourself.

Reading through all of the code rather than just jumping in—will really help you get things done faster in the long run. Plus you will learn more, and be better prepared for the next midterm exam!

Also, learning how to read code is an important skill for programmers—you have to be able to read code, not just write code, because in real-world software development, you are often modifying other people's code. So, here's an opportunity to develop your skill in that area.

Here's the suggested reading order, and what to notice:

 

Once you've noticed all those things, you are ready to move on to part 2.

Step 2: Adding some additional JUnit tests to ComplexTester and QuadraticTester

Note: Look ahead to the grading criteria for this step—so you know how your work will be evaluated. Do this

Make a habit of this at every step in every lab.

Step 2.1: Complete some unfinished unit tests in ComplexTester.java

In ComplexTester.java. search for the @@@ comments. You'll see two places where you are required to add additional unit tests—one more for the add method, and one more for the mutiply method.

If you've read through the file (as you were invited to) back in Step 1, you've already noticed these, and no doubt already have some idea what needs to be done to create these additional unit tests. You can follow the model of the unit tests that are already there, but choose different values.

Note that one of the whole ideas of test-driven development is to write tests before you write the methods. This forces you to do a hand calculation of an answer (e.g. with pencil and paper, and perhaps a calculator.) This makes you think about the steps involved in computing the answer. This helps you later, when you are writing the code, because you already know all the steps involved.

In a later step, you'll be implementing the add and multiply methods of the Complex class—notice whether writing a test case helps you as you consider how to implement those methods.

Remember, if you modify the code for a given file, to add yourself as an author in the javadoc comment at the top of the file.

From now on, I won't tell you this explicitly—you'll just be expected to know that you are supposed to do it.

Step 2.2: Complete some unfinished unit tests in QuadraticEqnTester.java

In QuadraticEqnTester.java. search for the @@@ comments. You'll see two places where you are required to add additional unit tests—one more for the evaluate method that operates on doubles, and one more for the evaluate method that operates on Complex objects. You can follow the existing unit tests for these functions as a guide.

In a later step, you'll be implementing the evaluate methods—again, notice whether writing a test case first helps you when you get to that step.

Step 3: Implementing the stub methods in Complex and QuadraticEqn, fixing bugs

Step 3.1: Fix the Complex class

Now, look first at Complex, and search for the @@@ comments. You'll see two places where you are required to finish a stub method.

Finish the methods, so that all unit tests for Complex pass (some of the ones for QuadraticEqn may still be failing.)

Note that you can right click on the ComplexTest class and choose "Test All" to run only the unit tests in ComplexTest—that way, you can get a nice green bar, even if you haven't fixed QuadraticEqn yet.

Step 3.2: Fix the QuadraticEqn class

Now, look first at QuadraticEqn, and search for the @@@ comments. You'll see three places where you are required to finish a stub method: the two evaluate functions, and the complexRoot2 method.

Finish the methods, so that all unit tests for the entire project pass.

Step 3.3: Play around with the QuadraticSolver

Before you go on to Step 4, play around with the Quadratic Solver a bit. Run the main method of the QuadraticSolver, and try entering various values for a, b, and c. Getting familiar with this will

 

Step 4: Implementing your own ComplexCalc class

Now, it is time for you to write a new class from scratch.

Reminder: look at the grading criteria for this step—before, during and after writing your code.


Turn in Step

This week, use turnin to submit your entire lab04 directory to lab04@cs10

Don't be surprised if that ends up being a LOT of files, especially if you are using BlueJ, and don't worry about warnings for binary files.

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


Grading

Points for this lab are distributed as follows. Partial credit may be awarded for each step at the discretion of the TA/Instructor.

Total points: ?

Due Date: You should do your best to complete as much of this lab as possible within the assigned lab time 05/07 or 05/08. If you don't, you may continue to work on it over the weekend, and early next week. We'll accept this assignment up without penalty up until noon Thursday on 05/14.

Submissions turned in after that time are subject to receiving zero credit—they will be graded only they arrive before the TA finishes work on grading the entire "batch" of assignments submitted.



After you turnin: An extra credit opportunity—maybe

Can you find any bugs? I'm not aware of any—that is, it is my belief (my hope, actually) that if you follow all the instructions above—you've implemented the methods indicated, and all the existing unit tests pass, along with your new ones—then, in that case the QuadraticSolver won't return any wrong answers, regardless of what you input.

However, if you can find a bug in the underlying Complex or QuadraticEqn classes, then there is an extra credit opportunity:

Similarly, if you find a bug—a genuine bug (see below)—in the user interface code in QuadraticSolver, then explain the bug in a comment flagged with // BUG-FIX, fix it, and submit your fix.

If you do this, put the special comment // BUG-FIX in your code in the unit test that exposes the bug, and next to the lines of code that fix the bug.

The amount of extra credit given will range from 0 pts to 100 pts, depending on the severity of the bug, and your skill in presenting your solution.

Note again: there may not be any opportunity here. My code might be free of bugs.

Also note that formatting glitches, ease of use issues, or differing mathematical interpretations of what consitutes a "degenerate root" don't count here as bugs. I'm talking about a genuine wrong answer—that's the threshold here.

If you find a bug after you do the turnin step, you may do a second or third turnin. If you do this, please email the TA for this assignment to let him/her know you did that.

 


End of lab04