Exam Rules

1. Write a useful javadoc comment for the following method. Use the javadoc format with @param and @return clauses. Be sure to describe the purpose of the method.

public Rectangle makeRectangle(int x, int y, int width, int height)
{
   int newX = x - width / 2;
   int newY = y - height / 2;
   Rectangle rect = new Rectangle(newX, newY, width, height);
   return rect;
}

2. Consider this Line class that is similar to, but not the same as, the one in Exercise P5.18 of your textbook.

Complete this LineTester class that constructs two lines of your choice that are perpendicular to each other (i.e., form a 90 degree angle), and verifies that the angle is 90 degrees.

3. Modify the grant method of the Genie class so that the genie only grants 3 wishes, and otherwise (politely) refuses.

/**
   A Genie grants wishes.
*/
public class Genie
{
   /**
      Constructs a genie, who will claim to grant wishes.
   */
   public Genie()
   {
      grantedWishes = 0;
   }

   /**
      The genie claims to grant your wish.
      @param wish Your wish
   */
   public void grant(String wish)
   {
      System.out.println("Your wish (" + wish + ") is my command!");
      grantedWishes++;
   }

   private int grantedWishes;
}

4. Find and fix the critical logic error in the code for the bet method in the following class:

/**
   A gambler will make any even probability bet, any time.
*/
public class Gambler
{
   /**
      Constructs a gambler with a set amount of starting funds.
      @param startingFunds the amount of money he starts with.
   */
   public Gambler(double startingFunds)
   {
      funds = startingFunds;
   }

   /**
      Makes the gambler take a bet at even probability to win or lose.
      @param amount the amount of the bet
   */
   public void bet(double amount)
   {
      double random = Math.random(); // A random floating-point value between 0 and 1
      if (random < 0.5)
         funds = amount + funds; // The gambler won
      else
         funds = amount - funds; // The gambler lost
   }

   /**
      Gets the funds remaining for the gambler.
      @return funds remaining
   */
   public double getFunds()
   {
      return funds;
   }

   private double funds;
}

5. In Homework 4C, you were asked to implement a Mail class. In this problem, describe how to enhance the solution so that the letter also shows the date, like this:

May 1, 2013

Dear John:

It appears you have not turned in CS46A homework 2 draft. Please remember that 
you are expected to attempt all assignments in this class.

Sincerely,

Professor Fiendish

a) Which instance field should be added to the Mail class? Give the type and the name.

b) Which methods should be added (if any)? Describe their behavior in English, but DO NOT provide an implementation. If none should be added, state that.

c) Which methods or constructors should be changed? Describe the changes in English, but DO NOT provide an implementation.

6. 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 Robot class and the sequence of calls

Robot carol = new Robot();
carol.turnLeft();
carol.moveForward();

Show the values for x, y, dx, dy, and newDx, as they are set when executing the three statements given above.

7. Draw a musical note. It should look like the following:

Complete this class:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import javax.swing.JComponent;

/**
   A component that draws a musical note.
*/
public class NoteComponent extends JComponent
{
   public void paintComponent(Graphics g)
   {
      // Recover Graphics2D
      Graphics2D g2 = (Graphics2D) g;

      // Construct the note and draw it.
      // Your code goes here. It should create a circle,
      // with a line coming up from the right hand edge of the circle.

   }
}

If you want to test your program, use this NoteViewer class (which you need not change).