[an error occurred while processing this directive]

Assignment 04: Inheritance

  1. Due Date
  2. Download Files
  3. Description
  4. Hints and Tips
  5. Submission
  6. Test Compile

Due date

This assignment is due Monday, November 13 at 4:00pm.

Important Note: Late assignments and assignments submitted via email will not be accepted!

Download Files

Download the A04.zip file and extract the files to the cs133 folder on your Home directory. You will use these files to complete this assignment. These files contain methods which we expect to be implemented without changes being made to the method signatures. We will use your classes in our own test programs based on the method signatures given in the provided files. If you make any changes to the signatures, our test will fail and your mark will suffer. You are allowed to add helper methods.


Description

In this assignment you will create a hierarchy of classes for drawing simple shapes and pictures on a Board object. The base class for the hierarchy is the Picture class, which keeps track of the width and height for pictures defined in subclasses, but represents no image itself.

public class Picture 
{
   private int height, width;


   public Picture() 
   { this.height = 0;
     this.width = 0; 
   }

   public Picture(int newHeight, int newWidth)
   { this.height = newHeight;
     this.width = newWidth;
   } 

   public int getHeight() 
   {
     return this.height;
   }

   public int getWidth() 
   {
     return this.width;
   }

   public void drawMe(Board board, int row, int column) 
   {
   }
}

Every picture has a height and width, which are returned by the accessor methods getHeight and getWidth. The drawMe method draws the picture on the Board object, with the upper-lefthand corner of the picture located at the indicated row and column. In the base class, the drawMe method does nothing. These methods will be overridden in the derived classes you will be writing.


The Rectangle class extends the Picture class and draws a rectangle of pegs on a board. (Note that this is not the same Rectangle class that you may have seen as an example in class.) A Rectangle object should contain instance variables to store its colour and dimensions. Complete the Rectangle class and test it using the main method provided. When run, it should display the following:

public class Rectangle extends Picture 
{
   // Add instance variables here.

   public Rectangle(Color color, int height, int width) 
   {
     // Your code here.
   }

   public void drawMe(Board board, int row, int col) 
   {
     // Your code here.
   }

   public static void main(String[] args) 
   {
     Picture test = new Rectangle(Board.BLUE, 5, 6);
     Board board = new Board(9, 12);
     test.drawMe(board, 2, 3);
   }
}

Your code should check if the rectangle will fit on the board at the location specified. If it won't fit, the portion that fits should be displayed on the board. Follow this rule for all classes derived from Picture that you write for this assignment.


The Square class is a derived class of the Rectangle class. A square is a special case of a rectangle, with the height and width equal. Complete the given code to finish the Square class. Override methods of the Rectangle class as appropriate.

public class Square extends Rectangle 
{
   public Square(Color color, int size) 
   {
   }
}

The Peg class is a derived class of the Square class. A peg is a special case of a square, with the height and width equal to one. Complete the given code to finish the Peg class. Override methods of the Square class as appropriate.

public class Peg extends Square 
{
   public Peg(Color color) 
   {
   }
}

The Collage class is a derived class of the Picture class that allows us to combine pictures together to create larger pictures.

public class Collage extends Picture 
{
   public Collage() 
   {
   }

   public void add(Picture picture, int row, int column) 
   {
   }

   public static void main(String[] args) 
   {
     Collage test = new Collage();
     Picture a = new Square(Board.GREEN, 2);
     Picture b = new Square(Board.YELLOW, 3);
     Picture c = new Peg(Board.RED);
     test.add(a, 0, 0);
     test.add(b, 4, 2);
     test.add(c, 2, 4);

     Board board = new Board(10, 10);
     test.drawMe(board, 2, 2);
   }
}

The Collage class should use an array to store the Picture objects. The array should double as necessary to accommodate any number of Picture objects. The add method adds a picture to the collage. The row and column parameters of the add method indicate the position to draw the picture relative to the upper-lefthand corner of the collage which you will also have to store in instance variables in the Collage class, associated with the correct picture element. When a collage is drawn, the pictures it contains should be drawn in the order they were added, and may end up drawn on top of each other. Note that a Collage can contain other Collages, since it is also a Picture object. Complete the given code to finish the Collage class. Override methods of the Picture class as appropriate.

You may assume that the Board on which you will draw your picture will always be large enough to hold the image.

If you've written your code correctly, the main method supplied with the class should draw the following picture:


The Triangle and Circle classes are derived classes of Picture that draw isosceles triangles and (approximate) circles.

The constructor for the Circle class takes a color and diameter as parameters.

public class Circle extends Picture 
{
   public Circle(Color color, int diameter) 
   {
   }

   public static void main(String[] args)
   {    
     Picture orangeOne = new Circle(Board.ORANGE, 1);
     Picture yellowOne = new Circle(Board.YELLOW, 4);
     Picture whiteOne = new Circle(Board.WHITE, 7);
     Picture greenOne = new Circle(Board.GREEN, 2);
    
     Board b = new Board(12, 12);
     orangeOne.drawMe(b, 4, 1);
     yellowOne.drawMe(b, 7, 1);
     whiteOne.drawMe(b, 1, 4);
     greenOne.drawMe(b, 9, 7);
   } // end main
}

The constructor for the Triangle class takes a color, height and width. The triangle is drawn point upwards, with the two equal sides at the right and left, and the base of the triangle at the bottom as in the following illustration.

public class Triangle extends Picture 
{
  public Triangle(Color color, int height, int width) 
  {
  }

  public static void main (String [] args) 
  {
    Board b = new Board(20, 20);
    Picture test1 = new Triangle(Board.ORANGE, 4, 7);
    Picture test2 = new Triangle(Board.GREEN, 4, 8);
    Picture test3 = new Triangle(Board.RED, 4, 9);
    Picture test4 = new Triangle(Board.BLUE, 6, 7); 
    test1.drawMe(b, 3, 2);
    test2.drawMe(b, 13, 2);
    test3.drawMe(b, 8, 4);
    test4.drawMe(b, 7, 13);
  }
}

Complete the given code to finish the Circle and Triangle classes. Be sure to add any necessary methods to override those in the Picture class. When drawing a circle or triangle, the upper-lefthand corner passed to the drawMe method represents the corner of a rectangle that would encompass the shape to be drawn. For example, the highlighted square in the triangle picture below has position (1,1), and this is the upper-lefthand corner that should be passed to the drawMe method to create this picture (only the pegs should actually appear). Similarly, the position (2,1) was passed to the drawMe method for the circle below.

Drawing circles and triangles is relatively challenging. We suggest that you initially implement the drawMe methods of these classes as stub functions and get the rest of the assignment working first before attempting to implement these drawMe methods.

Make sure that triangles with the following dimensions appear on the board as in the picture below: (4,7) , (4,8) , (4,9) ,(6,7) .

Make sure that circles with the following diameters appear on the board as in the picture below: 1 , 2 , 4 , 7.


If you've written everything correctly, the main method of the MyFirstPicture class should draw the picture below. Notice how the display method uses the getHeight and getWidth methods to calculate the required size for the board.


Bonus Question

This question is a bonus of up to 10%

Send by e-mail to the tutors (cs133@student.cs.uwaterloo.ca) two or more interesting pictures drawn with the assignment. The pictures sent by the students will be made available on the assignment page and can be used as possible test cases. You can find the submitted bonus pictures here


Hints and Tips


Submission

Electronic Submission

This assignment will be submitted electronically. Go here to see information about how to submit files electronically.

For this assignment, you may write additional classes not listed here if you wish. Create and submit a zip file called A04submission.zip that contains everything needed to run your program. Read these instructions if you don't know how to make a zip file. Submit your file using the handin code 04.

Files to Submit

You may submit the file A04submission.zip as many times as you like prior to the submission deadline; a later submission replaces any previous submission. Submitting early ensures that you are able to submit, and that you will receive some credit if you inadvertently fail to submit your final version of A04submission.zip by the submission deadline.

Late submissions will not be accepted, and no submissions will be accepted via e-mail. It is your responsibility to ensure, prior to the submission deadline, that the submission process works for you, and it is your responsibility to make your final submission sufficiently in advance of the submission deadline as to ensure that there is an adequate margin to allow for unexpected delays.

We will be compiling and running your submission. Be sure the your submission compiles.

After you have gone through the steps for electronic submission (by running an X11 window and executing the submit command), if you want to check to see what files have successfully been electronically submitted, click the button below and enter your Quest password.



Compilation Request

Press the button to request that the files for the current assignment be compiled and run. This facility is here to allow you to ensure that you have:

  1. submitted all required pieces of code
  2. no compilation errors
  3. no package names

You may not request testing for a particular assignment once its late deadline has passed. The results will be emailed to the specified userid upon the next scheduled test run (every 10 minutes).

[an error occurred while processing this directive]