Starting from:

$30

COMP2396 - Assignment 2 Solved

You are asked to write a number of Java classes to model basic shapes: Shape, Rectangle, Square and Diamond. Rectangle and Diamond are the subclasses of Shape, and Square is the subclass of Rectangle

 

You are also required to write JavaDoc for all non-private classes and non-private class members. 

 

Implementation
 

The Shape class:

  Methods    Shape()

            Constructor, create an empty shape. A shape is typically a 2D array of pixels (boolean values). 

  String toString()

   Return a drawing of the shape as a String. Each pixel should be represented by the character * and each empty space should be represented by the space character. Newlines are represented by newline character \n.

  int getArea()

                  Return the area, i.e., the number of pixels in the shape.

  Shape intersect(Shape s)

                 Return a new Shape object representing the intersection of this Shape object and s. 

  Shape union(Shape s)

                  Return a new Shape object representing the union of this Shape object and s.

 

The Rectangle class, a subclass of the Shape class:

      Methods

   Rectangle(int width, int height)

Constructor, create a rectangle with the specific width and height. For example, if width = 5 and height = 3, a drawing of this shape will look like this:

*****

***** *****
 

             

The Diamond class, a subclass of the Shape class:

      Methods

   Diamond(int size)

Constructor, create a diamond shape with the specific size. For example, if size = 3, a drawing of this shape will look like this (the empty area is represented by space characters and each newline is represented by the newline character \n.):



 

The Square class, a subclass of the Rectangle class:

      Methods

   Square(int size)

Constructor, create a square with the specific size. For example, if size = 3, a drawing of this shape will look like this:

***

*** ***
 

 

Grading
 

Your classes will be tested with a number of client programs. For example:

// ShapeTester.java public class ShapeTester { public static void main(String [] args) {

    Square s = new Square(3);

    System.out.println(s);

    System.out.println("Area is " + s.getArea());

    Diamond d = new Diamond(3);

    System.out.println(d);

    System.out.println("Area is " + d.getArea());

    Shape union = s.union(d);

    System.out.println(union);

    System.out.println("Area is " + union.getArea());

    Shape intersect = s.intersect(d); 

    System.out.println(intersect);

    System.out.println("Area is " + intersect.getArea()); }

}
 

             

The following output is expected:



 

More products