CS365 Homework 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:

     arc
      |
     oval
      |
      circle
    	
    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.

  2. Write regular expressions to describe the following set of tokens:

    1. Calendar dates of the form mm/dd/yyyy. Months and days may be either 1 or 2 digits. Years must be four digits. Do not worry about out-of-range dates such as 24/88/3005.

    2. Html header tags that start with h1, h2, or h3, then have a header with letters, numbers, blank spaces, and dashes, and end with a /h1, /h2, or /h3. For example <h2>Super Bowl-41</h2> is a valid header but <h2>Super Bowl-41</h3> is not because of the non-matching header tags and <h2>Super Bowl: 41</h2> is not because of the :.

    3. Scott #2.1d

  3. Augment your answer to the expression tree problem in homework set 1 by:

    1. allowing the values of expression trees to be assigned to variables.

    2. allowing expressions to reference variables

    For example, I should now be able to type:

    >>> = a / 2 + -3 6 >>> 0.67 >>> = b * 6 a >>> 4
    1. You may again assume that all input is correct and that variables are defined before they are used.

    2. You should use an appropriate data structure to store variable names and their values (you only have to store their values, you do not need to save the expression trees once they have been evaluated) and you should use Java's pre-defined set of data structures if possible.

    3. You may use my solution from homework 1 if you wish.

  4. Write a JLex specification that recognizes the following set of tokens. The tokens are presented with their regular expression first and then their name:
    1. CAPITAL -> [A-Z]
    2. LETTER -> CAPITAL | [a-z]
    3. NAME -> {CAPITAL}{LETTER}*
    4. ; | . | , : DELIMITER -- return the token name and lexeme value via the Symbol class
    5. NAME([ -]NAME)* : LASTNAME -- return the token name and lexeme value via the Symbol class
    6. [ \t\n\r\f] : WHITESPACE -- discard the white space
    7. . : print an error message with a line number