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. |
You can create UML class diagrams with Creately online diagramming and collaboration software. There are tools and templates available to draw class diagrams with 100s of class diagram examples to be used freely. Also you can learn how to draw class diagrams in-depth from this Class Diagram Tutorial .
ReplyDelete