CS1004 Homework #5
Due by Tuesday, April 20, at
11:00am
There are two parts to this homework assignment: a theory
portion, worth 10 points, and a programming portion,
worth 15 points. Please be sure to review the submission instructions in
advance, and make sure to include a README.
This version is revised as of 4/12. Minor clarification
on computePerimeter, the use of doubles, the size of the array,
area calculation, and a tip on getting the value of Pi.
Written questions
- (3 points) Insert the following elements into a binary tree,
and draw the result: 72, 35, 61, 10, 4, 98, 32, 55
- (4 points) You're given the following snippet of code.
class A {
public static void main(String[] args) {
new A();
}
public A() {
B first = new B(10);
B second = first;
second.setValue(20);
System.out.println("first is equal to 10: " + first);
System.out.println("second is equal to 20: " + second);
}
}
class B {
int x;
public B(int value) {
x = value;
}
public void setValue(int value) {
x = value;
}
public String toString() {
return "" + x; // Trick to temporarily convert int to string
}
}
- (2 points) The A() constructor has a bug in it. Without
compiling it, explain what the bug might be.
- (2 points) Suggest a way that this bug can be fixed, and
why your solution might work.
- (3 points) You're given the following elements sitting in a
stack: 45, 57, 12, 9, 32, 10, 5 -- such that 45 is at the "top"
of the stack (i.e., the first element to be popped). Now, for
every element in this stack, we pop that element and push it
into a second stack. Once the first stack is empty, we now pop
each element in the second stack and print them out, one by one.
In what order are the numbers printed, and why?
Programming assignment: shape calculator
In this assignment, you're going to build a simple program that
stores user-defined shapes and is capable of computing simple
properties for them: namely, the perimeter and area. We will
consider four types:
- Point, i.e., a zero-sided shape;
- Line, i.e., a one-sided shape;
- Circle;
- Polygons, i.e., 3, 4, and 5-sided shapes.
We will model this program after the previous assignments (i.e.,
a user interface to add and get information about what's been
added). For simplicity's sake, we will assume regular polygons.
Also, we'll use double values for edge/radius lengths.
-
(4 points) Design the data structures for this assignment.
You will implement five classes: Point, Line, Circle, Polygon,
and Shapes. I recommend you don't recycle lab classes for
this assignment, as we use them in a simpler fashion here.
- The Point class will not store any information.
- The Line class will store the edge's length.
- The Circle class will store the radius.
- The Polygon class will store the number of sides and the
edges' length. Since we assume regular polygons, you only
need to store the length for one edge.
- The Shapes class will contain main, an
array to store actual shape instances (use a size of at
least 50), and a counter to keep track of the number of
shapes stored. Since we don't know how to create an array
that can store different types, declare this array of type
Object. Since every Java object implements
Object, this array can store objects (shapes, for
that matter) of any type.
Make sure to specify constructors with appropriate parameters
for each of the initial shapes.
-
(3 points) Write methods in the Shapes class to add shapes.
addPoint doesn't take any parameters, addLine
takes only the length, addCircle takes the radius,
and addPolygon takes the number of sides and the
edge length. In all of these cases, construct the object
and place it in the shapes array. Have your methods return
a boolean, which is false only if the array is full.
-
(3 points) Write methods called computePerimeter in
both Shapes and in the subsidiary shape classes to compute the
circumference/perimeter of a shape. In Shapes, the method
should have one parameter: an index into the array; it should
find the appropriate element and call (and return) the results
of its computePerimeter method. In the shape class's
method, it should compute the perimeter (or circumference).
Of course, the method in Point should return a zero.
Note that in order for this to work in the Shapes class,
you'll need to upcast elements in the Object array; otherwise,
Java will refuse to recognize the method call on the object.
In order to know what to upcast to, use the
instanceof operator (if you don't know it, it will be
covered in the next lab) to determine what type of shape it
is. Then, upcast that shape back into its original datatype
and call the appropriate method in the individual shape class
(which, incidentally, needs no parameters). Note that you
should not have the actual calculation happen in the Shapes
class -- it should just find the constitutient shape object
and call its calculation method!
-
(2 points) Using the same technique as computePerimeter,
create a method called computeArea that does the same
thing, but computes the area instead. Of course, Points and
Lines have zero area.
-
(1 point) Write a toString method for Point, Line,
Polygon and Circle. Have it return the type of object,
its properties, its perimeter and area as a (single) String.
-
(2 points) Write a user interface to support this application.
Use a methodology similar to the previous homeworks. The
substantial difference is that instead of doing operations
by name, we're doing them by index directly.
- ap: Add a Point. This takes no parameters.
- al: Add a Line, after asking the user for a length.
- ac: Add a Circle, after asking the user for a radius.
- az: Add a Polygon, after asking for the number of
edges and length of one edge.
- cp: Compute perimeter, after asking for the index of
the element.
- ca: Compute area, after asking for the index of the
element.
- l: List all shapes. Print out the index, followed by
the toString representation of that array element.
- q: Quit.
Here are some hints you may find helpful.
- This
link contains information on planar geometry, including the
mechanisms to compute the area of various shapes. For polygons,
you're not expected to use generic planar algorithms --
just include ways of computing the area of 3, 4, or 5-sided
polygons.
- For the value of Pi, you can program your own or use
the constant Math.PI.
(3 points extra credit) Add a sort method to sort the
elements in the array by area, from least to greatest. Add a
UI option for this as well.