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.
It will help if you read the sections marked "G" in the table of contents in the chapters we've covered so far.
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.
In the second part, you'll first decide what you want to draw, and register that on Gauchospace (find the discussion board marked "Register your drawing here". Each student should draw a different picture. You'll then draw it on some 480x640 graph paper, using rectangles, lines, and ellipses (circles are a special case of an ellipse.) You can find graph paper to print at these links:
You'll then write code to generate your picture, following the model given. The instructions below, the comments in the code, and the guidance from lecture will help you through this.
The third part is a continuation of some of what we started in lab02 (and doesn't involve any graphics). This is practice with JUnit, but from the other side—coming up with test cases. You'll be given a class for CACity objects (California City), with name and population as the instance variables. The class is already written, though there may be one or two bugs. Your job is to write JUnit tests similar to the ones you were given in lab02 that test the functionality of the class. If your tests are well written, they should catch a couple of bugs. You should then fix the bugs.
In this part, if you have difficulty knowing how to write test cases, look back at the code from lab02, in particular at these test classes
On this project, you must do your own drawing, which much be substantially different from all the others in the class.
However, given that each student's drawing must be different, on this assignment you are free to get help from other students on steps 1 and 2 if you need help in understanding how to write the code. Be sure, though, that you are learning from each other, and not just having someone else "tell you what to type", or "telling someone else what to type".
For step 3, 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.
Also note 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."
Open a command line prompt in your ~/cs10/lab03 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/lab03/lab03Project .
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, fugu, etc.) to copy this directory onto your PC or Mac. The idea is the same. There are some helpful suggestions offered by your fellow students on the Gauchospace discussion boards about how to do this as well.
Open BlueJ, and use the "Open Project" menu item to navigate to the lab03Project inside your ~/cs10/lab03 directory. This will be similar to what you did last week.
Note:
If you opened up the lab03Project correctly, you'll see In your window, you'll see five Java classes. In this part of the lab, we are only concerned with three of those:
To get started, first:
Now, here's what you need to do:
Open up the PhillsPictureComponent.java program, and start reading through the comments. This is the class where the drawing happens.
You'll see the box and the lines for the house (as demonstrated in lecture).
You'll also see some code that draws circles for the snowman—only the bottom two have been drawn so far.
Figure out how to draw the third one. It will require you to create a new instance of the Circle class, passing the right values into the constructor. It may help to go through how the other values were calculated first.
In addition, you'll need to call g2.draw(), passing in the new instance of Circle that you created as the parameter.
Once you've completed this step, go up to the Javadoc comment at the top of the file, and enter your name as one of the authors (where it says something like YOUR NAME HERE). Also do this in the line that prints a name inside the window (the second call to g2.drawString()).
Test your code again after completing those two steps. If all is well, you are almost ready for step 2.
But, before moving on to step 2...
Take a moment to look through all the comments and code inside the file PhillsPictureViewer.java as well. You will need to create a similar file in step 2, so it is helpful to look through this file first, and try to understand how it works. Ask your TA if anything is not clear.
Also, look at Circle.java. You won't need to modify Circle.java—I've provided this for you both as a convenient way to draw circles, and as an illustration of inheritance (which we'll look at in Chapter 11.) Here, we are saying that a Circle "is a" special kind of Ellipse2D.Double—one where the constructor allows us to specify the center and the radius as (x,y,r) rather than specifying (upperLeftX,upperLeftY,width,height).
This file is very simple, but very powerful—it illustrates how, if we don't like the way some kind of class works in Java, we can often use inheritance to put a "wrapper" around it, and make it work the way we want it to work, rather than having to start from scratch. (This works in other languages that support inheritance too, such as C++ and Python.)
Ok, the real fun part.
Then you are ready to start coding.
Call your first class something like "FredsBeachSceneViewer" or "DianasPictureViewer" or whatever name you come up with—just make it in end in Viewer.
If you are working in BlueJ, choose "New Class", and name your class. Your class will come up with some starter code. Select all (^A) and then hit delete to erase all the starter code. Open the class PhillsPictureViewer.java in another window, and copy/paste all that code into your new class.
Then:
When you try to compile the first time, you'll get an error if you changed the lines that say PhillsPictureComponent to be FredsBeachSceneComponent, or DianasPictureComponent—no worries. That just means you need to implement that class also.
To do that, create a second class, this time calling it FredsBeachSceneComponent, or DianasPictureComponent (substituting your own name, and if you like, a description of what you are drawing), and copy/paste the code from PhillsPictureComponent.
At this point, I'd suggest trying to get everything to compile and run before making further modifications. At first, you'll see my house and my snowman—as finished by you. Once that's working, start going through your new Component class—actually, its a derived class of JComponent if we want to be technical about it—starting at the top, and replacing everything that should be replaced.
When your drawing looks awesome, its time to do a bit more with JUnit, in step 3.
Section 8.10 in your textbook describes JUnit, which we've been using since week 2 of the course. In lab02, I gave you the JUnit tests, and you implemented the classes. This time, we are turning it around—I've written a class, but it has some bugs. You are going to write JUnit tests that expose those bugs, and then fix the bugs.
The class you are going to work with is in the project already, along with a test class—though the test class methods are currently empty:
Your job here is to fill in the test methods.
For each test case, you should first construct a CACity object, using the name of some city, and its population. Here are a few samples—you can find other California cities' populations by looking up the city in Wikipedia (that's the source of this data, which may not be the most authoritative or accurate, but it is good enough for our purposes here:)
Then use a statement of the form:
assertEquals(expectedResult,calculatedResult);
I've put expectedResult and calculatedResult in italics, because you are not going to literally type that part in. Instead, what you fill in for calculatedResult is typically a method call, something like:
city.getName() city.getPopulation() or city.toString()What method call you use depends on what you are trying to test. Let the name of the test method be your guide.
What you put in for expectedResult is some value that you think you should get when you call the method you filled in for calculatedResult.
I'm hopeful that this is enough help that you can get through this fairly easily. If not, please ask for help.
A final note:
It is important that you understand how this "coming up with test cases" thing works, because on the next exam, you can be sure I'll be asking you about it. You'll be asked to come up with test cases for a class on the exam, just like what we are doing here in Step 3.
So if you are able to "get through the lab" by getting help from the TA or a friend, but you really don't understand—don't keep quiet. Come to office hours and ask questions—if, that is, it is important to you to be able to pass the next midterm exam.
While most of you did understand what was going on in lab02, it is clear that some of you did not understand what was going on in lab02—I can tell from your exam answers on midterm 1.
This week, you'll be turning your entire lab03 directory. Don't be surprised if that ends up being a LOT of files, especially if you are using BlueJ.
Here's how:
cd .. to move into the parent directory, i.e. the ~/cs10 directory. ls command to verify that the lab03 directory shows up in your listingturnin lab03@cs10 lab03You are finished—there is no paper component to hand in with this lab.
NOTE: This is the last week I'll be giving detailed instructions for the turnin step—in future weeks, I'll just say something like "use turnin to submit your lab04 directory", and you'll be expected to be able to get through the detailed steps on your own. You can always refer back to lab02 or lab03 if you need a reminder of the details.
Grading: This lab is worth Total points: ?, 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 as much of this lab as possible within the assigned lab time 04/30 or 05/01. 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/07.
Submissions turned in after that time are subject to receiving zero credit.
End of lab03