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."); } } |
Wednesday, 22 September 2010
Chapter Three Continued
Monday, 13 September 2010
Chapter Three Questions 3.1-3.44
3.22 | Create a ClockDisplay object by selecting the following constructor: new ClockDisplay() Call its getTime() method to find out the initial time the clock has been set to. Can you work out why it starts at that particular time? | The clock starts at that particular value because the actual parameters defined in the class source code exceed the limit defined in the NumberDisplay class. Recall that the hours integer can’t reach over 23 and the minutes can’t reach over 59; however, in this case we defined the actual parameters as 24 and 60, which again, exceed the limit, therefore setting them at 0. |
3.23 | How many times would you have to call the tick() method on a newly created ClockDisplay object to make its time reach 01:00? How else could you make it display that time? | You would have to call the tick() method 60 times to get it to 1:00. On the contrary, you can simply set the time to 1:00 by calling the setTime() method. |
3.24 | Write the signature of a constructor that matches the following object creation instruction: new Editor ("readme.txt, -1) | public Editors() - I cannot understand why this is only asking for the signature? As I see it, the signature for a constructor that creates an object of another class is simple, I will ask about this tomorrow in class. |
3.25 | Write Java statements that define a variable named window of type Rectangle, and then create a rectangle object and assign it to that variable. The Rectangle constructor has two int parameters. | public House(Rectangle window) { window = newRectangle(9, 5) } |
3.26 | Look at the second constructor in ClockDisplay's source code. Explain what it does and how it does it. | What the second constructor in ClockDisplay’s source cody does is begin a clock based on parameters that have to be set by the user. It does this by defining two formal parameters of type int, hour and minute, and later defining them to be set by the user. |
3.27 | Identify the similarities and differences between the two constructors. Why is there no call to updateDisplay() in the second constructor, for instance? | The similarities are that both the hours and minutes fields are defined in both cases to above the limit they can hold (24 and 60) in both case. The differences are that the second constructor has formal parameters, and the first constructor has a block, updateDisplay(); whereas the second constructor instead has setTime( hour, minute);. There is no updateDisplay() in the second constructor because the clock doesn’t have to be updated to 00:00 since the user will anyhow be setting new values. |
3.28 | Given a variable Printer p1; which currently holds a printer object, and two methods inside the Printer class with the headers public void print(String filename, boolean doubleSided) public int getStatus(int delay) write two possible calls to each of these methods. | A possible call to the public void print() is: { String filename = “File”; if(“File” = doubleSided) return System.out.println(“File is double-sided”); else return System.out.println(“File is not double-sided”); } A possible call to the public int getStatus(int delay) method is: { delay = glitch; return delay; } |
3.29 | Change the clock from a 24-hour clock to a 12-hour clock. Be careful: this is not as easy as it might at first seem. In a 12-hour clock the hours after midnight and after noon are not shown as 00:30, but as 12:30. Thus the minute display shows values from 0 to 59, while the hour display shows values from 1 to 12. | All that had to be changed was the value 24 in the two constructors above, to a value of 13. |
3.30 | There are at least two ways in which you can make a 12-hour clock. One possibility is to just store hour values from 1 to 12. On the other hand, you can just leave the clock to work internally as a 24-hour clock, but change the display string to show 4:23, or 4:23pm when the internal value is 16:23. Implement both versions. Which option is easier? Why? Which option is better? Why? | The easier option is to store values from 1 to 12 as then you do not have to show the pm or am part of the time; however, the better one is still the one that shows whether the time is am or pm as it gives more information. |
3.31 | Open the mail-system project, which you can find in the book's support material. Create a MailServer object. Create two MailClient objects. When doing this you need to supply the MailServer instance, which you just created, as a parameter. You also need to specify a username for the mail client. Experiment with the MailClient objects. They can be used to send messages from one mail client to another (using the sendMessage() method) and to receive messages (using the getNextMailItem() or printnextMailItem() methods). | Here’s an example of a message I sent to “Billybobjo” (I’m user “Dude”) : |
3.32 | Draw an object diagram of the situation you have after creating a mail-server and three mail clients. | The following is an object diagram of one mail-server and three mail clients. The arrows indicate objects belonging in another object’s field(s): |
3.33 | Set up a scenario for investigation: Create a mail server, then create two mail clients for the users 'Sophie' and 'Juan'. Then use Sophie's sendMessage() method to send a message to Juan. DO NOT YET READ THE MESSAGE. | Here is a picture of the message before I had sent it from Sophie to Juan: |
3.34 | Open the editor for the MailClient class and set a breakpoint at the first line of the printNextMailItem() method. | Here is what the source code looks like after setting a breakpoint: |
3.35 | Step one line forward in the execution of the printNextMailItem() method by clicking the Step button. | After stepping only once: |
3.36 | Predict which line will be marked as the next line to execute after the next step. Then execute another single step and check your prediction. Were you right or wrong? Explain what happened and why. | I believe the line after the boolean expression will be marked after stepping once more, because the boolean is like one entity, one piece of the method. I was partially right, and partially wrong. It DID consider the first if statement as one line, but it didn’t skip until after the ENTIRE boolean statement. The arrow stopped right before the else return statement. This further illustrates what I’m saying: |
3.37 | Call printNextMailItem() again. Step through the method again, as before. What do you observe? Explain why this is. | This time, the method went to the first part of the boolean statement, where the item was equal to null, nothing. This is because we Juan already received the message from Sophie, and there’s no new message. If there was a new message, item would not be null, and it would print out the message instead of printing out “No new mail.” |
3.38 | Set up the same test situation as we did before. That is, send a message from Sophie to Juan. Then invoke the printNextMailItem() method of Juan's mail client again. Step forward as before. This time, when you read the line item.print() use the Step Into command instead of the step command. Make sure you can see the text terminal window as you step forward. What do you observe? Explain what you see. | Nothing prints in the text terminal after using step into at line item.print(). However, a copy of the MailItem class source code pops up, highlighting the first line of it’s print() method. The arrow is on that collumn, which probably means, we stepped further into the code, with a reference to this object, which now means that we have to fully invoke this method in order to finish calling the printNextMailItem() method. |
Chapter Two Test Source
/**
* Adds movies, and rates them.
*
* @Michael Silber
* @1.0 (September 7, 2010)
*/
public class Movie // or MovieRating, except I thought "Movie" makes more sense, as you're adding a movie.
{
private String name; // field
private int time; // in minutes.
private int raters; //declaration
private double rating;
private int tens;
/**
* Constructors for class.
*/
public Movie() // constructor
{
name = ""; //initialization
time = 0;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Constructors for class.
*/
public Movie(String theName, int theTime)
{
name = theName;
time = theTime;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Get the movie name.
*/ // comment
public String getName() //accessor method
{
return name; //return statement
}
/**
*
*/
public void movieDetails()
{
System.out.println(""); //block statement
System.out.println("Movie name:"+" "+name);
System.out.println("Movie duration:"+" "+time);
System.out.println("");
}
/**
* Get the movie duration (in minutes).
*/
public int getLength() //method signature
{
return time; // method body
}
/**
* Change the movie name.
*/
public void changeName(String newName)
{
name //expression
= //assignment operator
newName;
}
/**
* Change the movie lenght.
*/
public void changeLength(int newTime //formal parameter
) //mutator method
{
time = newTime; //assignment statement
}
/**
* Rate the movie.
*/
public void //return type
rate(int newRating)
{
if(newRating<1 || newRating>10) //conditional statement
{
System.out.println("");
System.out.println("Please enter a rating value of 1 through 10");
System.out.println("");
}
else
{
double overallRating; //local variable
overallRating = ((rating*raters)+newRating)/(raters+1);
rating = overallRating;
raters = raters+1;
if(newRating == 10)
{
tens=tens+1;
}
}
}
/**
* Get rating.
*/
public void getRating()
{
System.out.println("");
System.out.println("*************************************************************************");
System.out.println(raters+" "+"person(s) have rated this movie.");
System.out.println("The average rating is" + rating+".");
System.out.println(tens +" "+"person(s) rated this movie a value of 10/10");
System.out.println("*************************************************************************");
System.out.println("");
}
}
* Adds movies, and rates them.
*
* @Michael Silber
* @1.0 (September 7, 2010)
*/
public class Movie // or MovieRating, except I thought "Movie" makes more sense, as you're adding a movie.
{
private String name; // field
private int time; // in minutes.
private int raters; //declaration
private double rating;
private int tens;
/**
* Constructors for class.
*/
public Movie() // constructor
{
name = ""; //initialization
time = 0;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Constructors for class.
*/
public Movie(String theName, int theTime)
{
name = theName;
time = theTime;
raters = 0;
rating = 0.0;
tens = 0;
}
/**
* Get the movie name.
*/ // comment
public String getName() //accessor method
{
return name; //return statement
}
/**
*
*/
public void movieDetails()
{
System.out.println(""); //block statement
System.out.println("Movie name:"+" "+name);
System.out.println("Movie duration:"+" "+time);
System.out.println("");
}
/**
* Get the movie duration (in minutes).
*/
public int getLength() //method signature
{
return time; // method body
}
/**
* Change the movie name.
*/
public void changeName(String newName)
{
name //expression
= //assignment operator
newName;
}
/**
* Change the movie lenght.
*/
public void changeLength(int newTime //formal parameter
) //mutator method
{
time = newTime; //assignment statement
}
/**
* Rate the movie.
*/
public void //return type
rate(int newRating)
{
if(newRating<1 || newRating>10) //conditional statement
{
System.out.println("");
System.out.println("Please enter a rating value of 1 through 10");
System.out.println("");
}
else
{
double overallRating; //local variable
overallRating = ((rating*raters)+newRating)/(raters+1);
rating = overallRating;
raters = raters+1;
if(newRating == 10)
{
tens=tens+1;
}
}
}
/**
* Get rating.
*/
public void getRating()
{
System.out.println("");
System.out.println("*************************************************************************");
System.out.println(raters+" "+"person(s) have rated this movie.");
System.out.println("The average rating is" + rating+".");
System.out.println(tens +" "+"person(s) rated this movie a value of 10/10");
System.out.println("*************************************************************************");
System.out.println("");
}
}
Meaningful Sentences for Four Vocabulary Words:
1. Often fields in a class are left with formal parameters which have to be given actual parameters, or specific values, by the creator of an object upon its creation. An example of this is the "name" field in the MovieRating class, which either receives a unique actual name from its creator, or uses the default settings "", upon creation, both of which are actual parameters.
2. Another term for a field is an instance variable, both of which are defined at the beginning of the class source code, and present spaces in which values can be stored. The earlier example of "name" in the MovieRating class, or others, such as "time," "raters," and "rating," are all fields.
3. The scope of a method is defined at the signature and is either, from what we've learned so far, "public" or "private."Public means the scope of the method is accessible to objects created by that class, and private, on the contrary, means that the scope is limited to that method and/or declaration. The scope of my public String getName() method in the MovieRating class is defined at the beginning as "public."
4. The lifetime of a variable is often only from the invocation of the method, to its completion. Such variables are referred to as local variables. In the MovieRating class, one such example of a variable with a lifetime that exists until the end of the method is my double overallRating local variable, which exists in the public void rate() method.
Subscribe to:
Posts (Atom)