Wednesday, 22 September 2010

Chapter Three Continued


3.39
Set a breakpoint in the first line of the sendMessage() method in the MailClient class. Then invoke this method.
Use the Step Into function to step into the constructor of the mail item. In the debugger display for the mail item object, you can see the instance variables and local variables that have the same names, as discussed in section 3.12.2.
Step further to see the instance variables get initialized.

 Here is a picture of the local and instance variables getting initialized as I stepped into the method further:

3.40
Use a combination of code reading, execution of methods, breakpoints, and single stepping to familiarize yourself with the MailItem and MailClient classes.
Explain how the MailClient and MailItem classes interact.
Draw object diagrams as part of your explanations.

 The MailClient class interacts with the MailItem class by relying on it as a reference. The MailClient object in fact uses the MailItem class in order to move it’s messages, or results of its method, to other object. Here is an object diagram of their interaction:

3.41
Use the debugger to investigate the clock-display project.
Set breakpoints in the ClockDisplay constructor and in each of the methods, and then single-step through them.
Is the behavior what you expected?
Did this give you new insights? If so what were they?

Yes, the behavior which I expected was for the ClockDisplay class to open up the NumberDisplay class for each of its methods in order to receive the actual parameters for the objects it was referencing. This was exactly what happened.
3.42
Use the debugger to investigate the insertMoney() method of the better-ticket-machine project from chapter 2.
Conduct tests that cause both branches of the if statement to be executed.

 This is a picture of a half printed ticket, while single-stepping into the printTicket() method:

3.43
Add a subject line for an e-mail to mail items in the mail-system project.
Make sure printing messages also prints the subject line. Modify the mail client accordingly.

 Here is a picture of a printed message with the newly created subject field represented. It simply has to be entered such as any other String field in the MailItem class, such as “to,” “from,” and “message”:

3.44
Given the following class..

public class Screen
{
 public Screen (int xRes, int yRes)
 {}
 public int numberOfPixels()
 {}
 public void clear(boolean invert)
 {}
}
Write some lines of Java code that creates a Screen object, and then call its clear() method if and only if its number of pixels is gretaer than 2 million

 The following is the source code of the class which I created, and which functions with all of the above methods:

/**
 * Class Screen
 *
 * @author (Michael)
 * @version (2.147)
 */
public class Screen
{
    // instance variables - replace the example below with your own
    private int xRes;
    private int yRes;

    /**
     * Constructor for objects of class Screen
     */
    public Screen(int xres, int yres)
    {
        xRes = xres;
        yRes = yres;
    }

    /**
     * Constructor Default for Screen.
     */
    public Screen()
    {
        xRes = 0;
        yRes = 0;
    }
   
    /**
     * Return the number of pixels
     */
    public int numberOfPixels()
    {
        return xRes*yRes;
    }
   
    /**
     * Clear.
     */
    public void clear(boolean invert)
    {
        int pixels = xRes*yRes;
        if(invert)
        {
            if(pixels >= 2000)
                 pixels = 0;
        }
        else System.out.println("There aren't enough pixels to clear the Screen, please enter more pixels and retry.");
    }
}



No comments:

Post a Comment