Objectives:
In this part of the assignment you are given a Java program that contains both syntax and logic errors. Your task is to identify and correct each of these errors. You will learn useful tricks for helping to debug your programs.
Start by downloading the source file PetStore.java. This source file contains a Java program that helps the owners of the PetLand pet store figure out how much animal feed to order each week.
Your task in this part of the assignment is to hand in a corrected version of the source code.
Hints:
Usually, it's best to start with the first error Java reports after each compilation! Often the later errors are reported just because Java became confused by the first error.
Also note that Java's error messages are not always entirely correct or helpful!
If you're not sure of the syntax or semantics of a method, try looking it up its class in the Java API. For instance, the Math class has documentation for the round and ceil methods.
The best way to detect logic errors is to come up with a set of tests and check the output from the program. A test consists of a set of inputs, the expected set of outputs and the actual outputs from the program. If the actual output matches the expected output, then the test passes, otherwise the test fails and you must try to find the error in your code.
When developing your tests, not only must you test "normal" or "usual" cases, you must also check "marginal" or "boundary" cases. For example, suppose you have a program that asks for a user's age and determines if the user is a senior citizen. Suppose that senior citizens are classified as people aged 65 or more. When you test the program, you want to test a value less than 65 and a value more than 65 and make sure that the program prints "Not senior citizen" and "Is senior citizen", respectively. These are "regular" cases. It is also critical to test the value 65. When the input is 65, the output should indicate that the user is a senior citizen. If the programmer uses "> 65" instead of ">= 65" in their code, the output will not be correct. It is a common error to write ">" when ">=" should be used - this error will be detected only if we test the value 65. We refer to 65 as a "boundary" value.
Suppose you run a test that fails. How do you find the error? There are various techniques for doing this - you can simply "walk" through the code checking that each line is logically correct. With a small piece of code, this will often work quite well. However, for larger pieces of code you will want to try to narrow your search down to a smaller chunk of code. You can do this by inserting System.out.println(...); statements in your code. Specifically, you can print out the value of key variables and ensure that they are correct. Note that the only value printed by PetStore.java is the final amount and cost of food to buy. It might be helpful to print out some of the intermediate values computed along the way and check that each of them is correct. Try using this technique - you will find that you detect logic errors much more quickly than staring at the code!
Details:
Applets are special Java applications that are displayed in web pages.
The size of the window in which the applet is displayed is specified
in the web page in which the applet is embedded.
Your flag should look like this:
The top and
bottom blue stripes should be 25% of the screen height each, with the
middle yellow stripe taking 50% of the height. The animal face should
take up 70% of the vertical height of the middle yellow stripe.
Your flag must adapt to the size of the window:
The colored stripes should always stretch all the way across the screen. The animal face in the center should always be centered horizontally and vertically within the applet window. It should adapt to the vertical height of the screen, but stay square even as the horizontal width changes. If the user resizes the window to be too narrow, then the sides of the face may be cut off.
Drawing the animal face is the most difficult part of the assignment. You do not need to make a beautiful, realistic image! Your face should at least have ears and eyes that are symmetric. A simple, blocky face like the one above will get full credit.
The Graphics class:
The Graphics class is part of the Java library
and provides you with all the graphics operations that you will need
for this assignment. Some of the methods of this class are presented
in section 3.9 of your text book (3rd edition). You should read through this section
carefully before continuing with the assignment.
You will have to implement only the paint method of the Applet class. Here is a template for the code that you will write, which you should save in a file named PetLandFlag.java.
import java.applet.Applet; import java.awt.*; public class PetLandFlag extends Applet { /** * method that is called automatically when the applet is to be painted * (i.e. when it is first displayed and when the window is resized) */ public void paint( Graphics page ) { super.paint(page); // get current width of the applet window int width = getWidth(); int height = getHeight(); // now draw the PetLand flag }
Running Applets:
Applets run from web pages, so you will to create a simple HTML page in order to view your applet:
<html> <div align="center"> <applet code="PetLandFlag.class" width=400 height=300> </applet> </div> </html>
You can download this file PetLandFlag.html by right-clicking the link and choosing Save As... (or Save Link As...). Save the file in the same directory as your Java code. You can now view your applet using appletviewer:
appletviewer PetLandFlag.html
Note that appletviewer is part of the Java Development Kit (JDK), so if you've got the compiler and interpreter installed, you won't have to do anything else to run it.
Graphics Hints:
We first create the
Polygon: Polygon triangle = new Polygon(); triangle.addPoint( 25, 25 ); triangle.addPoint( 50, 75 ); triangle.addPoint( 75, 25 ); Then we draw the triangle: page.setColor( Color.GREEN ); page.fillPolygon( triangle ); |
Style Hints:
Problem Solving Hints:
Let's review some techniques for solving problems. If you're not careful, when trying to solve a problem like this, you can create more problems than you solve. A powerful approach to problem solving is to divide and conquer. One way to break down the current problem is:
Of course, there are many possible ways to break down a problem. For instance, you could draw the face with one big polygon that includes the ears as part of the face, instead of as separate boxes.
Don't be tempted to code the entire solution in one step!
Code each part of the problem separately and test it before
moving on to the next part. If you have problems with the
first step, for example, you're not going to fix them by moving on to
the next!
Testing:
Devise a series of tests that you will use to check that your program is working
correctly. Each test could have: Apply each of your tests to your program and record
the result. Verify that the actual output matches the expected output.
If any of your tests fails, you probably have some debugging to do! Testing is very
important in creating good programs, even though the tests
aren't something that you hand in so that we grade them directly. We have created tests of our own to
see if your code does the right thing! Testing is also very useful during debugging - we strongly recommend
creating tests for Part 1 as well!
Do you feel like doing more? Here are a few (completely optional!) suggestions:
To submit the electronic copy, you must zip up the following
files: To do this, create a directory
called assign1Handin and copy your two
files to this directory. If you are in the lab, you can zip up
your files by selecting them and then click one of the selected
files with the right mouse button.
Choose 7-zip from the pop-up menu and
then select Add to archive... A
dialog window will pop up. Set
the Archive name
to assign1.zip. Change
the Archive format
from 7z
to Zip and then
click OK. You should see a new file in
your directory named assign1.zip. If you are using Windows Vista and have not installed any
additional file compression programs (like WinZip), you can zip
up your files be selecting them and clicking one of them with
the right mouse button. Choose Send to
and then select Compressed (zipped)
Folder. A zip file will be created in the same directory
containing the selected files. Note that you should change the
name of the zipped file
to assign1.zip. Now that you have zipped up your work, you are ready to
submit the file to our electronic handin box. Open up a web
browser and point it to:
http://www.cs.ubc.ca/ugrad/facilities/windows/handin.shtml You will have to install a security certificate on your
machine (instructions will be provided on-screen) and then log
in using your computer science undergraduate account ID and
password. You will then see a page that looks like: Important: now check that your work has been
submitted correctly by choosing the Check
submissions button. Note that you have to specify the
course and assignment name as you did when you first handed in
your assignment. You will see a list of the files that were
contained in your zip file. If any files are missing, repeat the
process. You can overwrite your submission any time before the due
date by clicking the Overwrite previous box
before you click the Handin
assignment button. If you want to check an assignment due date, select
the List assignments button. Note that you
must specify the Course (cs111).
Working in
Teams:
You may work with at most one other person. That person must
also be enrolled
in CPSC 111 this term. If you are working with a partner, the
two of you must
submit only one assignment. Keep in mind that when you work
in pairs, each
of you must understand the work that you submit. For example,
you should both
individually be able to do the debugging in Part 1 even though
you are submitting
your work as a team.
Please note that weekends and holidays are counted!