CPSC 111 Assignment 1: Pets

Details | Part 1 | Part 2 | Deliverables

Details

Out: Wed 20 Jan 2010
Due: Wed 3 Feb 2010, 5:00pm, by electronic handin
Value: 4% of final grade

Objectives:


Part 1: Debugging PetStore Code

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:


Part 2: Writing PetLandFlag Code

In this part of the assignment you will write a Java applet that displays the flag of the PetLand pet store.

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:

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:

  1. draw just a yellow rectangle
  2. add a blue strip across the top of the flag
  3. add a second blue strip across the bottom
  4. add the animal face to the centre of the flag
    1. draw the face as a square black box
      • test see whether the face adapts correctly to resizing
    2. draw the ears as two more small black boxes
    3. draw the eyes as two small green boxes

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!

Challenge

Do you feel like doing more? Here are a few (completely optional!) suggestions:


Deliverables:

Please be sure to include the following comment statement at the top of each source file that you submit in this assignment:

/*
  Authors: 
  UNIX login IDs: 
  Student numbers: 
  Date:   
  
  By submitting this file, we acknowledge that the persons whose names
  appear above are the only authors of this code except as acknowledged in
  the code below.
*/
You must submit an electronic copy of your assignment. You do not need to submit hardcopy (paper). In this assignment you must submit two files: (i) the source file that you corrected in Part 1; (ii) the source file PetLandFlag.java for the program that you wrote in Part 2. It is not necessary to submit any other work.

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.

Late Penalty:

The following late penalties will be applied if your work is not submitted on time:

Please note that weekends and holidays are counted!

A Word of Advice:

The demand for our labs may be extremely high on the due date of an assignment. To avoid last minute headaches, start early and plan to submit your assignment at least one day before it is due!


Back to 111 home
Last modified: Mon Jan 18 20:15:11 PST 2010