CPE 102

Winter 2008

Program 2

 

Due Date

 

You must turn in a functionally correct program to receive any credit.  The grade you receive will be based on when you turn it in minus any deductions for the quality of your implementation and/or multiple submissions.  There is no deduction for the first submission and a 5% deduction for each additional submission, if any.  Programs will be checked for correctness once a day and you will be notified by email if your submission is not functionally correct.  Programs will be graded after the last due date and the results will be emailed to you within a few days of that date.

 

100% (minus any deductions) by 9:00pm Monday, 1/21/07

90% (minus any deductions) by 9:00pm Tuesday, 1/22/07

80% (minus any deductions) by 9:00pm Wednesday, 1/23/07

70% (minus any deductions) by 9:00pm Thursday, 1/24/07

 

Errata:

 

    None so far

 

Objectives

 

  1. To develop and demonstrate basic object-oriented development skills
    1. Much of the structure of the solution is given – use good judgment to “filling in the blanks”

 

  1. To become familiar with and/or have more practice with:
    1. Java interfaces
    2. The concept of polymorphism
    3. Use of the instanceof operator
    4. Writing  javadoc-style comments
    5. Using the ArrayList collection class

 

Resources

 

  1. How to calculate the area of a general triangle
  2. How to calculate the area of a convex polygon
  3. Java Standard Library
  4. How to write Javadoc comments in your code
  5. How to use the javadoc utility to generate the html files
  6. P2TestDriver.java  (Will be published Monday 1/21)
  7. The JUnit tests for each class P2UnitTests.zip.

 

Ground Rules

 

Many of the classes and interfaces you will be writing in this project have counterparts in the Java Standard Library.  You may not use the Java Standard API versions in your implementation unless explicitly instructed to do so.  The classes and interfaces you may use are identified by a complete package specification.  For example, you will be using the java.awt.Color, java.awt.Point, and java.util.ArrayList classes from the Java Standard Library.  You may also use the Math class from the Java Standard Library (even though it is not explicitly referenced).  Any classes or interfaces in the specifications below that do not identify a package must be entirely written by you.  If you have any questions regarding this restriction please be sure to ask your instructor early in the development process (ideally, before you have written any code!).

 

Orientation

  1. You will be creating a virtual drawing workspace on which you can place various geometric shapes.  You will not actually be drawing the shapes in this program, rather, you will simply be representing geometric shapes as objects and providing the ability to place them in space.  The workspace will make use of an ArrayList from the Java Standard Library and will have methods that allow you to add, remove, and inspect the shapes it contains.   All of the shapes share a common set of methods defined by a Java interface.  The behavior of these common methods varies, as appropriate, by shape.  In addition, each shape has one or more methods unique to it.  The following shapes will be supported: circle, rectangle, triangle, and convex polygon.
  2. JUnit tests will be provided in P2UnitTests.zip, except for RectangleTest.java which is given as a stubbed out version.  Use the JUnit tests for other shapes as references to write the test code for the Rectangle class.

 

Specification

 

  1. You must implement the classes and methods exactly as described.

 

  1. Be sure all instance variables maintain encapsulation (private!).

 

  1. The specific instance variables are not explicitly specified.  You are required to make well-reasoned choices as to the data types and number of instance variables used.  Remember, each instance variable makes every object of the class require more memory.  All instance variables should be essential to defining the state of an object and/or provide a significant computational efficiency and/or allow for a significant reduction in code complexity.  Be ready to justify your choices.

 

  1. Include the required comment block at the beginning of every source file (see Lab 1 for an example).

 

  1. Document all methods in the Shape interface using the javadoc-style of documentation (see the Resources section above for help writing javadoc comments).

 

  1. Write a Java interface called Shape with the following interface:
    1. double getArea() – Calculates and returns the area of the object.
    2. java.awt.Color getColor() – Returns the java.awt.Color of the object.
    3. void setColor(java.awt.Color) – Sets the java.awt.Color of the object.
    4. boolean getFilled() – Returns true if the object is filled with color, otherwise false.
    5. void setFilled(boolean) – Sets the filled state of the object.
    6. void move(java.awt.Point point) – Moves the shape by the x and y amounts specified in the Point.

 

  1. Write a class called Circle that implements the Shape interface.  In addition to implementing the methods specified by the Shape interface you must also implement the following public methods:
    1. Circle(double radius, java.awt.Point position, java.awt.Color color, boolean filled) – Constructor.  This should give you a large clue as to what the instance variables of the class should be!  The java.awt.Point specifies the location of the center of the circle.  The boolean indicates if the shape is filled (with color) or wire-frame.
    2. double getRadius() – Returns the radius of the Circle object.
    3. void setRadius(double radius) – Sets the radius of the Circle object.
    4. java.awt.Point getPosition() – Returns the position of the Circle object.
    5. Override the equals-method (from Object) so that it returns true for two Circle objects that are logically equivalent based on the state of all of their instance variables.
    6. Note: Be sure to use the constant Math.PI (a constant defined in the Math class found in the Java Standard Library) when performing any calculations involving PI.

 

  1. Using a Test-Driven Development (TDD) style as demonstrated in Lab 2, write the test code (stubbed version of RectangleTest is contained in P2UnitTests.zip) and source code for the Rectangle class that implements the Shape interface.  The TDD style is that you implement the test to satisfy the functionality of a method, and then the source code to implement that method.  Ensure it works and move on to the next method.  For the Rectangle class, in addition to writing the methods specified by the Shape interface, you must implement the following public methods:
    1. Rectangle(double width, double height, java.awt.Point position, java.awt.Color color, boolean filled) – Constructor.  This should give you a large clue as to what the instance variables of the class should be!  The java.awt.Point specifies the location of the lower-left corner of the rectangle.  The boolean indicates if the shape is filled (with color) or wire-frame.
    2. double getWidth() –  Returns the width of the Rectangle object.
    3. void setWidth(double width) – Sets the width of the Rectangle object.
    4. double getHeight() – Returns the height of the Rectangle object.
    5. void setHeight(double height) – Sets the height of the Rectangle object.
    6. java.awt.Point getPosition() – Returns the position of the Rectangle object.
    7. Override the equals-method (from Object) so that it returns true for two Rectangle objects that are logically equivalent based on the state of all of their instance variables.

 

  1. Write a class called Triangle that implements the Shape interface.  In addition to writing the methods specified by the Shape interface you must implement the following public methods:
    1. Triangle(java.awt.Point a, java.awt.Point b, java.awt.Point c, java.awt.Color color, boolean filled) – Constructor.  This should give you a large clue as to what the instance variables of the class should be!  The java.awt.Point objects represent the three vertices of the triangle in the specified order, a, b, c.  The boolean indicates if the shape is filled (with color) or wire-frame.
    2. java.awt.Point getVertexA() – Returns a specific vertex of the triangle.
    3. void setVertexA(java.awt.Point point) – Sets a specific vertex of the triangle.
    4. java.awt.Point getVertexB() – Returns a specific vertex of the triangle.
    5. void setVertexB(java.awt.Point point) – Sets a specific vertex of the triangle.
    6. java.awt.Point getVertexC() – Returns a specific vertex of the triangle.
    7. void setVertexC(java.awt.Point point) – Sets a specific vertex of the triangle.
    8. Override the equals-method (from Object) so that it returns true for two Triangle objects that are logically equivalent based on the state of all of their instance variables.

 

  1. Write a class called ConvexPolygon that implements the Shape interface.  In addition to writing the methods specified by the Shape interface you must implement the following public methods:
    1. ConvexPolygon(java.awt.Point[] vertices, java.awt.Color, boolean filled) – Constructor.  This should give you a large clue as to what the instance variables of the class should be!  The array of java.awt.Point objects represents each vertex of the polygon.  Note that the vertices must be specified in counterclockwise order and that the first/last point (same point in a closed polygon) is not specified twice in the array.  You may assume that the array passed to the constructor is in counterclockwise order.  The boolean indicates if the shape is filled (with color) or wire-frame.
    2. java.awt.Point getVertex(int index) – Returns the specified vertex.  Note that index must be between zero, inclusive, and the number of vertices, exclusive.
    3. void setVertex(int index java.awt.Point) – Sets the specified vertex of the polygon.
    4. Override the equals-method (from Object) so that it returns true for two ConvexPolygon objects that are logically equivalent based on the state of all of their instance variables.  To simplify this implementation, consider two polygons equal only when the have the same vertices in the same order.

 

  1. Write a class called WorkSpace.  This class should have one private instance variable of type java.util.ArrayList to hold Shape objects.  In addition, the class should have the following public methods:
    1. WorkSpace() – Default Constructor.
    2. void add(Shape shape) – Adds objects which implement the Shape interface to the end of the WorkSpace’s java.util.ArrayList instance variable.
    3. Shape remove(int index) – Removes the Shape at the specified index and returns a reference to it or null if the index is out-of-bounds.
    4. Shape get(int index) – Return the ith Shape object from WorkSpace.
    5. int size() – Returns the number of Shapes contained by the WorkSpace.
    6. java.util.ArrayList<Circle> getCircles() – Returns a java.util.ArrayList of all of the Circle objects contained in the WorkSpace.
    7. java.util.ArrayList<Rectangle> getRectangles() – Returns a java.util.ArrayList of all of the Rectangle objects contained in the WorkSpace.
    8. java.util.ArrayList<Triangles> getTriangles() – Returns a java.util.ArrayList of all of the Triangle objects contained in the WorkSpace.
    9. java.util.ArrayList<ConvexPolygon> getConvexPolygons() – Returns a java.util.ArrayList of all of the ConvexPolygon objects contained in the WorkSpace.
    10. java.util.ArrayList<Shape> getShapesByColor(java.awt.Color) – Returns a java.util.ArrayList of all Shape objects in the WorkSpace that match the specified java.awt.Color.
    11. double getAreaOfAllShapes() – Returns the sum of the area of all Shape objects in the WorkSpace.

 

Suggestions

  1. Remember and use – where necessary – the instanceof operator in the appropriate places of your solution. 
  2. Implement the Shape interface and document each of the method declarations.  Write the comments in a generic fashion that reads well for any shape you might implement.  The javadoc utility will use the javadoc comments from the interface to generate the html documentation for any class that implements the interface.  This saves you the trouble of documenting the same methods multiple times in Circle, Rectangle, Triangle, ConvexPolygon, or any other class that my implement the Shape interface in the future.
  3. Implement one shape at a time.  Test and debug the shape until you are satisfied with its design, implementation, and functionality then implement the remaining shapes.
  4. Implement the WorkSpace class incrementally.  Notice that many of the methods are very similar and differ only by the Shape type they work with.  Implement the ones that relate to a single Shape class, test this code until you are satisfied it is working correctly – you’ll then be able to cut and paste it for the other related methods and should only need to make minor modifications.
  5. Remember that you may import classes from the Java Standard API used in your implementation and do not need to use the fully qualified package names as done in this specification (done here t reduce potential ambiguities).
  6. Keep track of your hours worked as you go, so you don’t have to guess when you hand in your assignment (see handin section below for information on time.txt).

 

 

Testing With the Provided Tests

 

  1. Your code must be 100% correct to recieve credit.  Correctness will be determined by running the acceptance test P2TestDriver.java to be published Monday 1/21.  
  2. In addition to the acceptance test, sections 7 and 9 will be graded on the quality of JUnit tests they create for this program, section 3 will not.
  3. Use the JUnit tests while developing in a test-driven approach to ensure your code's correctness before the acceptance test is released.  Run them by opening the AllTests.java file and executing it.
  4. Ask your instructor for assistance if you are not able to run these tests on your own.

 

Handing in Your Source Electronically…

 

  1. Create a plain text file called time.txt to log the amount of time spent on the assignment.  The file will contain only three lines with the following information on each line: name, project number, hours spent.

Example time.txt file:

 

    Sally Student

    Program 1

    7.5 hours


  1. Move the necessary file(s) to your vogon account using your favorite FTP client program.
  1. Log on to vogon using your favorite Shell client program.
  1. Change directory (cd-command) to the directory containing the file(s) to hand in.
  2. Use the following handin command being sure to replace the x below with the correct maximum score you can earn based on the due date, i.e., replace the x with 100 if you are handing in by Monday's deadline, 90 for Tuesday, 80 for Wednesday, or 70 for Thursday.  For example, Program1-100 for Monday, Program1-90 for Tuesday, et cetera.

 

12:01pm vogon ~$ handin graderkm Program2-x Shape.java Circle.java Rectangle.java Triangle.java ConvexPolygon.java WorkSpace.java RectangleTest.java time.txt