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

  1. (3 points) Insert the following elements into a binary tree, and draw the result: 72, 35, 61, 10, 4, 98, 32, 55
  2. (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
      }
    }
    1. (2 points) The A() constructor has a bug in it. Without compiling it, explain what the bug might be.
    2. (2 points) Suggest a way that this bug can be fixed, and why your solution might work.
  3. (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:

  1. Point, i.e., a zero-sided shape;
  2. Line, i.e., a one-sided shape;
  3. Circle;
  4. 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.

  1. (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. Make sure to specify constructors with appropriate parameters for each of the initial shapes.
  2. (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. (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!
  4. (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.
  5. (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.
  6. (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.
Here are some hints you may find helpful. (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.