CSE 341 - Homework 6 - Java Generics and Smalltalk Warmup

Due: Nov 8, 10pm. You can use up to 3 late days for this assignment. (The reason for this is so that we can post sample solutions within a reasonable time --- HW 7 will build on the answer to the Smalltalk question for this homework.)

15 points total

Turnin: You should turn in four files: ArrayExample.java, GoodArrayExample.java, ArrayListExample.java, and Stacks.st


The first two questions are about Java. They use a hierarchy of classes of simple geometric objects. Here are the definitions:

Test.java is a little test class that you can run to make sure the other classes are working. For convenience, here is a zip file containing all the definitions, as well as the skeleton code for the Java questions: geometric.zip.

Running Java: You can run Java on whatever machine you want. If you don't have some other machine you prefer, you can use attu (javac Test.java to compile the program, java Test to run it).

  1. (2 points) This question is about Java arrays and the covariant typing rule. Look at the skeleton for the "main" method in this class: ArrayExample.java. Add some additional statements to the main method so that the code compiles correctly, and when it is run, it raises a java.lang.ArrayStoreException when adding the cone to geoshapes. (Adding the circle should be OK.) Here is the exception you should get:
    Exception in thread "main" java.lang.ArrayStoreException: Cone
            at ArrayExample.main(ArrayExample.java:10)
    
    Hint: look at the ArrayTest example in the lecture notes.

    Next, look at the code in GoodArrayExample.java. (This is exactly the same except for the class name.) Add statements to this code as needed, so that the code compiles correctly, and runs without error.

    If you want, you can add some statements to these "main" methods to get them to print something so that you know they are doing something, but you don't need to.

  2. (7 points) This question is about Java generics. Look at the skeleton code in ArrayListExample.java. Add additional methods total_area, total_perimeter, describe_all, and add_empties. Leave the main method untouched. You should get this output when you run the code:
    Example with a list of shapes with a circle, a cone, and some empty shapes
    Circle[radius=1.0]
    Cone[radius=2.0,height=3.0]
    Circle[radius=0.0]
    Cone[radius=0.0,height=0.0]
    Rectangle[width=0.0,height=0.0]
    Sphere[radius=0.0]
    Total number of shapes: 6
    
    Example with a list of rectangles
    Rectangle[width=2.0,height=3.0]
    Rectangle[width=5.0,height=5.0]
    Total number of shapes: 2
    total area of rectangles: 31.0
    total perimeter of rectangles: 30.0
    
    Example with a list of 2d shapes with a circle and a rectangle
    Rectangle[width=10.0,height=10.0]
    Circle[radius=2.0]
    Total number of shapes: 2
    total area of flat shapes: 112.56637061435917
    

  3. (6 points) This is a Smalltalk warmup question. Define a class Stack, as described in the lecture notes. To make it easy to submit the answer to this question, define a new class category named Stacks in a browser, and define your class Stack in that category.

    Test your class by opening a workspace, making a couple of instances of Stack, and sending each of them some push: and pop messages. Open an inspector on each stack so you can see its state.

    Once you have the stack working, add three more methods:

  4. Now put some test cases in a more permanent place than a workspace, by defining a method test for Stack that runs some suitable tests, printing the results to the transcript. Your test method should create and exercise at least two separate, independent stacks.

    Hint: open a transcript using the "open ..." menu item, then picking "transcript". Here is some code that exercises a stack. (Be sure and open the transcript before running this, so that the results show up. You need the final Transcript flush to get the characters to show -- otherwise they may just be buffered up.)

        | s |
    s := Stack new setsize: 10.
    s push: 'squid'.
    s printOn: Transcript.  Transcript cr.
    s pushAll: #('fee' 'fie' 'fo' 'fum').
    s printOn: Transcript.  Transcript cr.
    Transcript flush.
    

    To create the file for the electronic submission for your answer to Question 3, select the class category Stacks in a browser and then pick 'fileOut' from the "yellow button" menu (right hand button on Windows, or option-click on a Mac).