Homework Assignment 2

  1. The type of graphics commands provided by a graphics package can influence the way a class hierarchy is designed. For example, some graphics systems, including X, provide a single command for drawing arcs, ovals, and circles. More particularly, X's command for drawing arcs, ovals, and circles can be roughly written as follows:
    	DrawArc(left, top, width, height, angle1, angle2)
    angle1 and angle2 denote the beginning and ending angle of the arc. An oval can be created by setting angle1 to 0 and angle2 to 360. A circle can be similarly created, except that in addition the width must equal the height.

    The format of this command could lead you to define a class hierarchy in which an oval is a subclass of an arc, and a circle is a subclass of an oval:

    The arc class might define the following methods:

    1. void SetAngle1(double angle);
    2. void SetAngle2(double angle);
    3. void Draw(some parameters);
    4. void setLeft(int left);
    5. void setTop(int top);
    6. void setWidth(int width);
    7. void setHeight(int height);

    The oval subclass does not want to provide either the SetAngle1 or SetAngle2 methods because it is a closed arc. The circle subclass additionally wants to eliminate the setWidth and setHeight methods and add a setDiameter method. Answer the following questions:

    1. What is the problem with the class hierarchy?
    2. Suggest another way to implement the oval and circle subclasses that allows you to "inherit" the implementation of the arc subclass without incurring the disadvantages of actual inheritance. Sketch out a sample class declaration for a circle that includes the following elements:
      1. declarations for the instance variable(s)
      2. declarations for the public methods (it is okay to use my "some parameters" notation for the parameters to the Draw method)
      3. implementations for the setLeft and setDiameter methods.

  2. You are given the following declarations in Java (you should assume that each class would be placed in a different file):
    package LinkedList;
    class ListNode {
        protected int value;
        String name;
    package LinkedList;
    public class List {
        ListNode header;
        protected ListNode sentinelNode;
        public List() {
            header = new ListNode();
    1)      header.value = 10;
    2)      header.name = "brad";
    	sentinelNode = new ListNode();
    package LinkedQueue;
    class Queue extends LinkedList.List {
        public Queue() {
    3)      header.value = 20;
    4)	sentinelNode.value = 30;
    Answer the following yes/no questions about the above code and for each answer explain why you answered as you did:

    1. Is it legal to access the value variable in statement 1?
    2. Is it legal to access the name variable in statement 2?
    3. Is it legal to access the header variable in statement 3?
    4. Is it legal to access the sentinelNode variable in statement 4?
    5. Is it legal to access the value variable in statements 3&4? Hint the answer and reason is the same in both cases.

  3. Answer this question only after writing the program in the next question.

    1. In the next problem, could the TreeNode class be written as an interface? Why or why not?
    2. In the next problem, could the OperatorNode class be written as an interface? Why or why not?

  4. Using Java write an expression evaluator that takes an expression with one operator and two numbers in prefix notation, evaluates it, and prints an answer. Your program will continue to read expressions and evaluate them until the user hits Ctrl-D. Here is a sample session:

    >>> + 3 6
    >>> / 2 3
    >>> * 8 3
    >>> Ctrl-D

    Your program should read in one expression at a time, build an expression tree for each expression, and then evaluate the expression tree and print out the answer.

    For example, given the expression:

    * 2 3
    your program should construct the expression tree:
                     /     \
    		2       3
    Each of your nodes should be an object that is an instance of some class. I know that you can solve this problem much more easily by simply looking at the first character in the input string and then performing the appropriate operation on the next two numbers. You will receive a 0 if you try to do it this way. The point of this problem is to get you started doing object-oriented design on a simple problem that you can easily solve. By giving you an easy problem, I am allowing you to focus on how to do the object-oriented design, rather than how to solve the problem.

    A good inheritance hierarchy for this program would be:

    		|               |
    	   OperatorNode    NumberNode
            |         |          |           |
         PlusNode MinusNode MultiplyNode DivideNode
    The actual expression tree from above would then look like:
    		|                    |
    	    NumberNode           NumberNode
    Part of this assignment involves figuring out what methods to define and where to place them, and figuring out which classes should be abstract and which should be concrete.

    Place all of your classes in a package named formula. The class containing the main method should be named ExpressionTree.

    Your program may assume that the input is always correct and has the correct number of operands (my executable catches errors but yours does not need to).


    1. If you have a question about what an expression should evaluate to, type the following:
      java -jar /home/bvz/cs365/hw/hw2/ExpressionTree.jar

    2. Use either a console object to read lines or else a Scanner to read lines using the Scanner's nextLine method. The console class's readLine method and the Scanner's nextLine method both return a string that can be passed to the constructor for another Scanner object. You can use the second Scanner object to read the numbers and operator symbols.

    3. Use the printf/format methods for either System.out or the console class to print out your formatted results.

    Extra Credit (20 points)

    Extend your interpreter so that it can handle recursive prefix expressions. For example:

    >>> * 3 / 20 5
    >>> / 2 + -3 6
    >>> + + / 24 * 2 4 - 3 5 6