CS 2510 - Assignment 2: On to Java... Class Definitions, Containment, and Unions


Notes on this (and later) assignments:

This problem set is to reinforce basic data definitions Java. We will only use features that are described in How to Design Classes (i.e., like ProfessorJ), but we will use Eclipse and a specialized Testing Library (here is the documentation)

The process of setting up Eclipse will be covered in the Lab. Your submission should consist of a single Java file, submitted via the hand-in plugin, as discussed at the end of the first Lab, and the beginning of the second Lab.

Make sure the solution to each problem is labeled correctly (top to bottom) and that your examples class (say Assignment2Examples) includes at least 2 examples for each of the classes you define. You must provide purpose statements for each class and method that you design.

Important:

You should format your file/classes as discussed in Lab. In otherwords, import the testing.Examples class, and make a public Assignment2 class that extends Examples and has a main method.

          
       // "import" provides the "Examples" class/tester
       import testing.Examples;

       /** Examples and Tests for Assignment #2 */
       public class SolutionAssignment2 extends Examples{
           // "public" Class and the "public static void main(...){ ... }" make it runnable
           public static void main(String[] args){ test(); }

           /* ... Examples Go Here ... */
       }          

       /* ... Other Classes Go Here ... */
        
You can use this as a template for the class in Eclipse, but make sure the class name (Assignment2) is the same as the file name (Assignment2.java).

A note on text-based class-diagrams...

When you a required to create class-diagrams in your assignment(s) please make them look nice, and be sure to draw the arrows when needed. You should use the dash, bar, and plus characters (-, |, +) to build the boxes, greater/lessthan (>, <) or "v"/"^" for containment arrow-heads, and some combination of backslashes, forward slashes, and underscores for inheritance arrows. Something like this:


                               |
                              /_\
                          +----+----+
                          |    |    |
        
But feel free to make it look better if you can find a way.

For example, below is a problem description, a possible class definition, and a correctly formatted class diagram:

          /* PROBLEM
               Design a to represent Foos (A "Foo" is an ancient bird).  Each
               Foo needs to keep track of its age (in years) and each has a Baz.
               A Baz consists of a String that describes the time period in
               which the bird lived, and a boolean telling whether or not the
               time period had humans */

          /* CLASS DIAGRAM
               +---------+        +-----------------+
               |   Foo   |   +--->|       Baz       |
               +---------+   |    +-----------------+
               | int age |   |    | String  timeper |
               | Baz baz |---+    | boolean humans  |
               +---------+        +-----------------+  */
 
          // DEFINITIONS

          /** Represents an ancient bird */
          class Foo{
             int age;
             Baz baz;
             Foo(int age, Baz baz){
                this.age = age;
                this.baz = baz;
             }
          }

          /** Represents a Baz (time with or without humans) */
          class Baz{
             String timeper;
             boolean humans;
             Baz(String timeper, boolean humans){
                this.timeper = timeper;
                this.humans = humans;
             }
          }
        


Now for the Real problems...

How to Design Classes (HtDC) Problems:

We suggest that you complete these problems on paper first, then type the into Eclipse. Working them out before hand will give you practice for the exam, since the exam will be handwritten.

After you finish them, if you feel like you need more practice, try nearby problems in the book/packet.

:

:

:

:



Other Problems:

Problem A1: Classes Definitions

This semester we will eventually use images, windows, and canvases similar to those used in the Dr.Scheme student languages. But first we need to understand how to define the structures.

You will define a class Posn to represent positions, similar to the structure in Scheme. It needs to represent x and y coordinates.

  1. Draw (in your file) a class diagram representing the Posn class.
  2. Translate your diagram into a class definition (don't forget a constructor).
  3. Add Examples of Posn instances to the beginning of your Examples class.

Problem A2: Containment

  1. Imagine you're designing a program for the registrar office that keeps track of students. You need to represent two related kinds of compound information.

    The first represnts a Name, with first and last components.

    The Second represents a Student, with an id number, a name, and a field that tells whether or not the student is male.

    Draw a class diagram for the data representations.

  2. Turn your class diagram into complete class definitions.
  3. Create several example instances and add them to your Assignment2 examples class.

Problem A3: Unions

Along the lines of representing images and canvases, we want to design classes to represent colors.

  1. Draw a class diagram with an interface IColor, and four classes that implement the interface: Red, Green, Blue, and Other.

    The first three each have a single constant field, name, that represents the string name of the color (e.g., "red", "green", "blue").

    The last IColor, Other, has three integer components that represent amount of red, green, and blue of the color. Each integer is assumed to be between 1 and 255.

  2. Turn your class diagram into complete class definitions.
  3. Create several example instances and add them to your Assignment2 examples class.

Problem A4: Self-Referential Unions

Your final task is to represent shapes that we might want to draw on a canvas.

  1. Draw a class diagram to represent an interface IShape, with 5 implementing classes: Circle, Rectangle, Line, Offset, Overlay.

    Each of the first three classes has a color field. In addition, Circle has a radius, Rectangle has a width and height, and Line has fields that represent the difference between the start and end points of the line, i.e., the distances across and down that the line travels.

    The other two shapes are self-referential: they contain fields that references IShape, represented by a back-arrow or a loop in the class diagram. Offset represents a nested IShape that is offset by a given Posn, and Overlay represents the combination of a top and a bottom shape.

    Be sure to add all the IShape arrows, though you do not need to add the arrows for Posn, and IColor.

  2. Turn your class diagram into complete class definitions.
  3. Create several example instances and add them to your Assignment2 examples class.