Assignment 3

Due Wednesday November 4th, 2009 11:55PM

  1. (15 points)

    Assume you are modeling the state for a character in a side-scrolling video game - let's call him Jeff. Throughout the game, the following things can happen to Jeff:

    • When the game starts, Jeff is just Plain Old Jeff.
    • If Jeff collects a powerup, he becomes Big Jeff.
    • If Jeff collects another powerup, he becomes Happy Jeff.
    • If at any point Jeff falls into a chasm, he becomes Dead Jeff. He also screams in a comic fashion.
    • If Jeff is bitten by a monster, what happens depends on what he has collected:
      • If Jeff is currently Big Jeff or Happy Jeff, the monster bite transforms him into Plain Old Jeff and he grunts.
      • If Jeff is already Plain Old Jeff, the monster bite transforms him into Dead Jeff.

    Draw a UML state diagram for Jeff. Make sure you include the following:

    • Transition edges between states which makes use of the "event [guard] / action" syntax where appropriate.
    • Start and end nodes.
    • Treat Jeff's screaming and grunting as actions he performs in response to state transition.

  2. (15 points)

    Create a UML sequence diagram for the following code.

    public class SillyProgram
    {
    	public static void main(String[] arg)
    	{
    		Nutty[] nutties = new Nutty[]{new Nutty(),new Nutty()};
    
    		goCrazy(nutties);
    		goCrazy(nutties);
    	}
    	
    	private static void goCrazy(Nutty[] nutties)
    	{
    		SomeClass a = new SomeClass(nutties[0]);
    		SomeClass b = new SomeClass(nutties[1]);
    		a.foo(b, 2);
    	}
    }
    
    class SomeClass
    {
    	private Nutty nutty;
    	public SomeClass(Nutty nutty)
    	{
    		this.nutty = nutty;
    	}
    	
    	public void foo(SomeClass other, int x)
    	{
    		if (x<=0)
    		{
    			nutty.chocolate();
    		} else
    		{
    			other.foo(this, x-1);
    		}
    	}
    }
    			
  3. (15 points)

    Consider the following code:

    public class SquarePanel extends JPanel implements KeyListener
    {
        private int number = 1;
    
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            for (int i=0;i<number;i++)
            {
            	g.fillRect(i*10,0,8,8);
            }
        }
    
        public void keyPressed(KeyEvent e) { }
        public void keyReleased(KeyEvent e) { }
        public void keyTyped(KeyEvent e)
        {
            switch (e.getKeyChar())
            {
                case '+': number++; break;
                case '-': number--; break;
            }
        }
    }
    			
    1. (5 points)

      For each of the methods and fields in the above class, identify whether it is model logic, controller logic, or view logic.

    2. (10 points)

      The above code is in egregious violation of MVC design. Describe at least two refactoring operations you would perform in order to make this code conform to MVC. (You do not have to name the refactorings you are using; just describe them.)

  4. (15 points)

    For each of the following code fragments, state whether or not that fragment is in violation of the Liskov Substitution Principle. Justify your answer.

    1. (5 points)

      Is the Better class in violation of LSP? Why or why not?

      public class NumberSequence {
      	private int lastNumber = 0;
      	/**
      	 * Returns the next number in the sequence.
      	 * @return The next number in the sequence.  The numbers from subsequent calls are always in increasing order.
      	 */
      	public int next() {
      		lastNumber++;
      		return lastNumber;
      	}
      }
      public class Better extends NumberSequence {
      	public int next() {
      		return super.next() % 45;
      	}
      }
      					
    2. (5 points)

      Is the EvenBetter class in violation of LSP? Why or why not?

      public class EvenBetter extends NumberSequence {
      	public int next() {
      		super.next(); // throw away a value
      		return super.next();
      	}
      }
      					
    3. (5 points)

      Is the Pen class in violation of LSP? Why or why not?

      public class Pencil implements WritingImplement {
      	public void sharpen() {
      		...
      	}
      	public void write(String message) throws SharpenException {
      		...
      	}
      }
      public class Pen extends Pencil {
      	public void write(String message) throws NeedsInkException {
      		try {
      			super.write(message);
      		} catch (SharpenException se) {
      			throw new NeedsInkException(se);
      		}
      	}
      }
      					
  5. (10 points)

    With the Liskov Substitution Principle in mind, consider two classes: Square and Rectangle. Assume that they have methods and fields representative of the corresponding geometric constructs, such as coordinates defining their respective locations and methods for calculating area.

    Should Square be a subclass of Rectangle or should a Rectangle be a subclass of Square? Think carefully about situations in which one might be used as the other. Justify your answer. If you must impose some restrictions on some of the functionality of the classes, explain why.

  6. (30 points)
    1. Consider the following code.

      MyPriceObject o = ...;
      double basePrice = o.getBasePrice();
      double taxRate = o.getTaxContext().getTaxRate();
      double actualPrice = basePrice * (1 + taxRate);
      ...
      					
      1. (10 points)

        If that snippet appeared more than once in your program, how would you fix it?

      2. (5 points)

        What is this refactoring called?

    2. Consider the following code.

      Product product = ...;
      double price;
      switch (product.getType())
      {
      	case PRODUCT_TYPE_FOOD:
      		price = product.getPrice();
      		break;
      	case PRODUCT_TYPE_CLOTHING:
      		price = product.getPriceWithTax();
      		break;
      	case PRODUCT_TYPE_PENGUIN:
      		price = product.getPriceWithTax() + product.getImportFees();
      		break;
      	default:
      		price = Double.NaN;
      }
      ...
      					
      1. (10 points)

        If that snippet appeared in your program, how would you fix it?

      2. (5 points)

        What is this refactoring called?

Submission Instructions

Submit your answers via WebCT. Handwritten UML diagrams are acceptable as long as they are legible. If we cannot read your diagram with a reasonable amount of effort, we will not assign credit for it. You may wish to use a UML diagramming application. Your homework must be submitted electronically; we will not accept paper copies.