CSE 142 Winter 2002

Homework #2

Written Part: Java Syntax, Diagrams, Classes

Written part due: Beginning of lecture, Wed., January 23

Programming Project: Landscape Artist

Warmup due: Electronically, 11:00pm, Tue., January 22

Final project due: Electronically, 11:00pm, Sun., Jan. 27;
written report due in lecture, Monday Jan. 28


Written Part: Java Syntax, Diagrams, Classes

Purpose

The purpose of this part of the assignment is to get more practice with Java and to reinforce some of the terminology that we have used so far.  If you have been following lectures and keeping up with the corresponding reading, this part of the assignment should be fairly easy.  We will be using the terminology throughout the course, so it is important to learn it now.

Warning

It is possible to complete this part of the assignment, get a satisfactory score, and learn next to nothing.  After you complete this part of the assignment, generate your own versions of these questions and answer them.  Better still, find someone else in the course, generate some questions of the same kind as in this assignment and try to answer each other's questions.  Who knows, questions like these might appear on a quiz or exam.  Don't just come up with the answer without really understanding what is really going on.

The Questions

(A nicely formatted version of this section of the assignment is available on line.  You can print that out, fill in the answers, and turn that in if you'd prefer.)

  1. Suppose we start Jeva and then type in the following Java statements in the order given (the ">" is supposed to represent the prompt we see in Jeva):

    > int height = 10;
    > int width = height + 20 / height;
    > int length = width;
    > Oval sun = new Oval (20, 30, 50, height, Color.green, true);
    > Oval moon = sun;

    Draw a picture (like those drawn in lecture and in the Dugan notes) that shows the relationship between the various names above and the objects they refer to.  Use the space below.












  2. a. Some (or all) of the Java statements below would produce an error if typed into Jeva.  For each wrong Java statement, state what kind of error it is (syntax or semantic) and briefly explain (one sentence) what is wrong.  If there is nothing wrong with the statement, just write "OK" next to it.  You may use Jeva to help with your answers, but try to figure it out before using Jeva.  (Thought question that you don't need to turn in: For each of the incorrect Java statements, think about how you could change the statement in a small way so that it is legal Java.)

    b. Now suppose we start Jeva over and then type in the following Jeva statements in the order given.

    > int height = 10;
    > int width = 30;
    > Rectangle bigRedBox = new Rectangle (5, 10, 50, 25, Color.red, true);
    > GWindow gw = new GWindow( );

    For each Java statement below, do the same as you did in part a of this question, i.e., indicate whether the statement is ok, or what sort of error it contains.


  3. Suppose we define a new class as follows.  (This is what the .java file would look like.)

    import uwcse.graphics.*;
    import java.awt.Color;

    public class IceCreamCone {
        Oval scoop;
        Triangle cone;

        public IceCreamCone () {
            this.scoop = new Oval(50, 50, 40, 40, Color.green, true);
            this.cone = new Triangle(50,90,90,90,70,50,Color.orange,true);
        }
    }

a. What is the name of the class?

b. Circle the constructor for this class.

c. List the names of the instance variables, along with their types.

 

Turning in the Written Assignment

Turn in this part of the assignment in lecture on Wednesday, January 23.  Be sure that your name and quiz section attended are written on your paper, and, if you have more than one sheet of paper, be sure to staple them together.

You will be graded on the correctness of your answers, as well as on following directions properly.  For example, if you e-mail your files instead of turning them in in the manner requested above, you won't get full credit.

End of Written Assignment

 

Programming Project: Landscape Artist

Purpose

The purpose of this assignment is to give you experience defining your own classes, instance variables, constructors, and methods. You will create a new Landscape class out of existing shape classes, similarly to how the House class was developed in lecture.  In fact, you will use the House shape as one part of your own Landscape class.  But before we design our landscape, let us first make our house drawing a little more interesting.

Warmup

We will be using BlueJ for the rest of the quarter, so we first need to become familiar with the BlueJ environment.  This warmup exercise will do this and help you learn about the various parts of a class definition.  If you have been following the lectures and readings, this warmup should be easy.

Your task is to take the House class that was developed in lecture and add one more component to the house.  You may choose any component you wish.  Some examples: a door, a window, a chimney.  If we send an addTo message to an object of type House, the resulting drawing should look like a house, but with the extra feature.

Hints and Suggestions

Turning in the warmup exercise

Electronically turn in your modified House.java file and TestScene.java file.  At the top of each of your .java files, put your name and your section in a comment.  Go to the Turn-in form.

End of Warmup

 

Now that we have warmed up and are familiar with BlueJ and classes, it's time to draw a landscape.

Project Description

The goal is to create a landscape, maybe consisting of things you would normally associate with a landscape: a house, a tree, a sun, etc., or maybe something more fanciful (flying houses?)  Your landscape must have a house and at least three other things.   The house must be represented by the House class you turned in for the warmup exercise (with any additional changes you wish to make).  The three or more additional things can be anything you want, even something as simple as an Oval representing the sun, with the following additional requirement:  The scene must include at least one and no more than four other objects of classes that you have defined to represent items in the landscape, and at least one of these must be a compound shape made up of two or more different shapes (e.g., a tree consisting of an oval placed at the top of a tall, skinny rectangle or something similar).

To represent the collection of objects that make up the scene, you must create a Landscape class.  Class Landscape should contain instance variables for each of the items in the scene.  When an instance of Landscape is created, the constructor should create the objects that make up the scene.  A landscape should implement the method addTo so it can be added to a GWindow, and the implementation of addTo should add all of the objects that make up the scene to the window.

You will then need to modify the TestScene class to draw the landscape in a window (by sending the addTo message to the Landscape object).  Your code should consist of: a Landscape class, a House class, one to four other classes that you have defined, and a TestScene class.  Each of these files should contain a comment at the top of the file that includes your name and section.

Your project will be graded (equally) on how well it meets the functional requirements of the assignment (sufficient number of objects in the scene, etc.) and how well the code is written.  To get full credit for quality of the code, you should pick meaningful names for class and object names, include appropriate JavaDoc comments describing classes, constructors, and methods, and organize your code neatly with appropriate indentation.  The JavaDoc comment at the beginning of each file should identify the author (you) and the section you attend.

Hints and Suggestions

Note: the hints and suggestions below are a loose step-by-step guide to completing the assignment.  We think you will find the approach outlined below (that of incremental development) a good one.  However, you may approach the assignment in any way you see fit, as long as your final code satisfies the requirements listed in the Project Description above.

+ Decide what your landscape will contain.  Each of these things will be represented by a Java object.  For example, a sun could be represented by an Oval, or Kane Hall could be represented by a red Rectangle.  You also must include at least one and no more than four other classes that you have defined.  For example, you could define a Tree class, which consists of a Rectangle and a Triangle.  Or, you could define a flower to be a Line and an Oval.  Birds, bushes, or whatever -- you are limited only by your imagination as to what you can create in your picture.  We suggest that you sketch out your scene before you do any programming, identifying the main layout parameters like the various x and y coordinates and widths and heights you'll need to construct the various shapes.

+ Create a new class in BlueJ named Landscape.  Add a comment to the top of your file to explain to your users what your class does.  Include your name and your section in your comment.  Test to see whether you made any typing errors by trying to compile your (blank) Landscape class.  If you get errors, fix them.

+ Add an instance variable in your Landscape class for your house (e.g., "House house;").

+ Add a constructor (with no arguments) that will initialize the instance variables of a newly created Landscape object.  You may use either the House constructor that takes no arguments, or the one that takes 4 arguments allowing the placement and size of the house to be specified; see the source code of the House class for documentation on the 4-argument constructor.  (You can even get JavaDoc-style documentation out of BlueJ: open the House class in BlueJ, and then change the Implementation button to Interface; after a few seconds you'll see the documentation for the House class!)  Compile your Landscape class again to see if there are any syntax or semantic errors.

+ Add a method named addTo to your Landscape class that takes a GWindow argument and returns nothing. As you can guess, sending an addTo message to a Landscape object should cause a drawing to appear on the window associated with the GWindow argument. For this assignment and all future assignments, you should consistently use the addTo interface for adding shapes to windows.

+ Test your code in BlueJ by creating a TestScene object.  You will need to change the constructor for the TestScene class so that it will draw a landscape onto a window, rather than a house.  (Thought question: if you have followed the above steps one at a time, was the picture that was drawn the same as the one that was drawn in the warmup?  Why or why not?)

+ Add additional instance variables for your other component shapes, one at a time.  Each time you add an instance variable, change the addTo method for the Landscape class so that the Java object associated with that component is added to the GWindow.  This way of programming is called incremental development, a useful concept in all engineering endeavors.  The idea is to add a small feature to your program and then test to see if it worked.  If something went wrong, then it is very likely that what you just changed caused the error.  It is generally unwise to try to write all of the code at once and then compile, since if there is an error (and there usually is) it will be much more difficult to determine what is wrong.

+ If you have completed the assignment as described above and you are feeling a little ambitious, you could try to create a landscape in the style of Salvador Dali.  For example, think about how you could reproduce something that looks like his surrealistic work, The Persistence of Memory, using only the shapes in the graphics library.  Allow your creative energies to produce pictures that go beyond the kinds of crayon drawings you made in the 3rd grade.  However, don't get too carried away.  If you aren't learning anything new about Java in these creative exercises, your time might be better spent solidifying your understanding of Java through the readings.

+ Just to be on the safe side -- go back and reread the project description to make sure what you have done satisfies all the requirements of this assignment (a good practice for every homework!).

Turning in the Complete Project

The turn-in page will accept a Landscape.java file, a House.java file, a TestScene.java file, and up to four more .java files (for your own classes).  So don't write a solution that is too fancy: it can't contain additional .class files, and can't call on libraries other than the standard Java libraries and the CSE142 classes.  Of course, on your own you can be as fancy as you like -- write a program to show off to your friends or bring to office hours to demonstrate, for example.  Not for extra credit, just for fun.

You will be graded on two things: the correctness of your program (i.e., how well you followed directions, and how well your program does what we asked), and on the clarity of your program (i.e., how readable it is, and how well it communicates the intent of what you were trying to accomplish to the human reader).  Clarity will benefit from good explanatory comments, good choice of variable names, good use of local names (if needed; perhaps only in the constructor in this assignment), and good use of indentation to group things.  Notice, for example, how the code in House.java is indented.

Electronically turn in your Landscape.java file and your other .java file(s).  Turn-in form.

Written Report

In lecture on Monday, January 28, you should turn in a short (no more than one page) written report about your project.  Include your name, section, homework number and date at the top.  Briefly describe the classes you defined for your project.  Explain what worked and what didn't work in your code.  Describe the most important things you learned from this project, which could be new ideas that you have never seen before, or misconceptions that you had that the project helped you straighten out.

End of Complete Project