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
- To develop and demonstrate
basic object-oriented development skills
- Much of the structure of the
solution is given – use good judgment to “filling in the blanks”
- To become familiar with and/or
have more practice with:
- Java interfaces
- The concept of polymorphism
- Use of the instanceof
operator
- Writing javadoc-style
comments
- Using the ArrayList
collection class
Resources
- How to calculate the area of a general triangle
- How to calculate the area of a convex polygon
- Java Standard Library
- How
to write Javadoc comments in your code
- How
to use the javadoc utility to generate the html files
- P2TestDriver.java
(Will be published Monday 1/21)
- 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
- 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.
- 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
- You must
implement the classes and methods exactly as described.
- Be sure all
instance variables maintain encapsulation (private!).
- 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.
- Include the required comment block at
the beginning of every source file (see Lab 1 for an
example).
- Document all
methods in the Shape interface using the
javadoc-style of documentation (see the Resources section
above for help writing javadoc comments).
- Write a Java interface called Shape
with the following interface:
- double getArea() – Calculates
and returns the area of the object.
- java.awt.Color getColor() –
Returns the java.awt.Color of the object.
- void setColor(java.awt.Color)
– Sets the java.awt.Color of the object.
- boolean getFilled() – Returns
true if the object is filled with color, otherwise false.
- void setFilled(boolean) – Sets
the filled state of the object.
- void move(java.awt.Point
point) – Moves the shape by the x and y amounts specified in the Point.
- 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:
- 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.
- double getRadius() – Returns
the radius of the Circle object.
- void setRadius(double radius)
– Sets the radius of the Circle object.
- java.awt.Point getPosition() –
Returns the position of the Circle object.
- 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.
- 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.
- 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:
- 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.
- double getWidth() –
Returns the width of the Rectangle object.
- void setWidth(double width) –
Sets the width of the Rectangle object.
- double getHeight() – Returns
the height of the Rectangle object.
- void setHeight(double height)
– Sets the height of the Rectangle object.
- java.awt.Point getPosition() –
Returns the position of the Rectangle object.
- 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.
- 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:
- 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.
- java.awt.Point getVertexA() –
Returns a specific vertex of the triangle.
- void setVertexA(java.awt.Point
point) – Sets a specific vertex of the triangle.
- java.awt.Point getVertexB() –
Returns a specific vertex of the triangle.
- void setVertexB(java.awt.Point
point) – Sets a specific vertex of the triangle.
- java.awt.Point getVertexC() –
Returns a specific vertex of the triangle.
- void setVertexC(java.awt.Point
point) – Sets a specific vertex of the triangle.
- 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.
- 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:
- 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.
- java.awt.Point getVertex(int
index) – Returns the specified vertex. Note that index must be
between zero, inclusive, and the number of vertices, exclusive.
- void setVertex(int index
java.awt.Point) – Sets the specified vertex of the polygon.
- 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.
- 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:
- WorkSpace() – Default
Constructor.
- void add(Shape shape) – Adds objects
which implement the Shape interface to the end of the WorkSpace’s
java.util.ArrayList instance variable.
- 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.
- Shape get(int index) – Return
the ith Shape object from WorkSpace.
- int size() – Returns the
number of Shapes contained by the WorkSpace.
- java.util.ArrayList<Circle>
getCircles() – Returns a java.util.ArrayList of all of the Circle objects
contained in the WorkSpace.
- java.util.ArrayList<Rectangle>
getRectangles() – Returns a java.util.ArrayList of all of the Rectangle
objects contained in the WorkSpace.
- java.util.ArrayList<Triangles>
getTriangles() – Returns a java.util.ArrayList of all of the Triangle
objects contained in the WorkSpace.
- java.util.ArrayList<ConvexPolygon>
getConvexPolygons() – Returns a java.util.ArrayList of all of the
ConvexPolygon objects contained in the WorkSpace.
- 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.
- double getAreaOfAllShapes() –
Returns the sum of the area of all Shape objects in the WorkSpace.
Suggestions
- Remember and use – where necessary – the instanceof
operator in the appropriate places of your solution.
- 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.
- 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.
- 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.
- 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).
- 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
- 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.
- 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.
- 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.
- Ask
your instructor for assistance if you are not able to run these tests on
your own.
Handing
in Your Source Electronically…
- 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
- Move the
necessary file(s) to your vogon account using your favorite FTP client
program.
- Log on to vogon using your favorite Shell client program.
- Change
directory (cd-command)
to the directory containing the file(s) to hand in.
- 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