CS46A Homework E

  1. This class has several compile-time and run-time errors. Correct as many as you find and add
    // Corrected compile-time error

    or

    // Corrected run-time error

    behind each correction that you made.

    Submit the corrected Bug1.java.

  2. Add a method
    public int getTurns()

    to this class that returns the total amount by which this bug (the solution for homework 4A) has turned. You should return an angle between 0 and 359 degrees. For example,

    Bug2 bugsy = new Bug2();
    bugsy.turn(300);
    bugsy.turn(50);
    int turns = bugsy.getTurns(); // sets turns to 350
    bugsy.turn(40);
    turns = bugsy.getTurns(); // sets turns to 30 

    You may assume that turn has been called only with integers between 0 and 359.

    Hints:

    Submit Bug2.java and Bug2Tester.java, testing the above scenario.

  3. In Lab 4, you traced changes in the instance variables and local variables, by writing values into boxes and crossing them out. In this exercise, you will do the same tracing for a different problem. However, don't cross out old values. Simply write new values below to the old ones, and put an X when a local variable is removed. A typical run for Lab 4A would be
    totalScore | quizCount | score | newTotalScore | newQuizCount
    -----------+-----------+-------+---------------+-------------
         0     |           |       |               |
               |     0     |       |               |
               |           |   8   |               |
               |           |       |      8        |       
               |           |       |               |       1
         8     |           |       |               |
               |     1     |       |               |
               |           |       |      X        |       X
               |           |   7   |               |
               |           |       |     15        |
               |           |       |               |       2
         15    |           |       |               |
               |     2     |       |               |
               |           |       |      X        |       X
        

    Consider this class and the sequence of calls

    Bug3 carol = new Bug3();
    carol.moveBackward(2);
    carol.turnLeft();

    Show the values for x, y, dx, dy, n, and newDx, as they are set when executing the three statements given above. Including the removal of local variables, there are 12 changes.

    Submit a file problem3.txt in which you fill in this form:

    x |y |dx |dy |n |newDx
    --+--+---+---+--+----
      |  |   |   |  |
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
      |  |   |   |  |    
        
  4. Implement a class HorizontalFlag that draws flags such as the following. A viewer and component class have been provided for your convenience. As in How To 3.2 , the height of the flag should be 2/3 of the width. As in Homework 5B, pass colors to the constructor for the top, middle, and bottom colors. Fill three rectangles, one each of the given color, and then draw a large rectangle around all three.

    Submit HorizontalFlag.java.