יום ראשון, 19 בספטמבר 2010

Chapter 3 Exercises


3.1
Think again about the lab-classes projectthat we discussed in Chapter 1 and Chapter 2. Imagine that we create a LabClass objectand three Student objects. We then enroll all three students in that lab. Try to draw a class diagram and an object diagram for that situation.
Identify and explain the differences between them.

Class diagram will only have Class Student and Class LabClass

Object diagram will have a LabClass instance and three Student instances
3.2
At what time(s) can a class diagram change?
How is it changed?

Creating/Deleting new classes
3.3
At what time(s) can an object diagram change?
How is it changed?

Creating/deleting new instances
3.4
Write a definition for a field named tutor that can hold references to objects of typeInstructor.
private Instructor tutor;
3.5
Start BlueJ, open the clock-display example and experiment with it. To use it, create aClockDisplay object, then open an inspector window for this object. With the inspector open call the object's methods. Watch thedisplayString field in the inspector.
Read the project comment (by double-clicking on the text note icon on the main screen) to get more information.

Done
3.6
What happens when the setValue() methodis called with an illegal value?
Is this a good solution?
Can you think of a better solution?

It stays the same. An error messege could work.
3.7
What would happen if you replaced the ">="operator in the test with ">", so that it read..
  if((replacementValue > 0) && (replacementValue < limit))

You wouldn't be able to have the 0 digit, which is pretty essential in a clock.
3.8
What would happen if you replaced the "&&"operator in the test with "||", so that it read..
  if((replacementValue >= 0) || (replacementValue < limit))

Essentially, it could be any number.
3.9
Which of the following expressions return true?
! (4<5)
! false
(2>2) || ((4==4) && (1<0))
(2>2) || (4==4) && (1<0)
(34 !=33) && ! false


the second and fifth
3.10
Write an expression using boolean variablesa and b that evaluates to true when either aand b are both true or both false.
if (a == b)
return true;
else
return false;
3.11
Write an expression using boolean variablesa and b that evaluates to true when only one of a or b is true, and which is false if a and bare both false or both true. (This is also called an exclusive or.)
if (!a==b)
return true;
else
return tfalse;
3.12
Consider the expression (a && b).
Write an equivalent expression (one that evaluates to true at exactly the same values for a and b) without using the && operator.
if (!a || b)
return false;
else
return true;
3.13
Does the getDisplayValue() method work correctly in all circumstances?
What assumptions are made witrhin it?
What happens if you create a number display with limit 800, for instance?

 No, anything above 23 is changed to 0.
3.14
Is there any difference in the result of writing
        return value + "";
Rather than
        return "" + value;
in the getDisplayValue() method?

No.
3.15
Explain the modulo operator.
After the two ints are divided the result is the remainder
3.16
What is the result of the evaluation of the expression (8%3)?
2
3.17
What are all possible results of the expression (n%5), where n is an integer variable?
0, 1, 2, 3, 4
3.18
What are all possible results of the expression (n%m), where n and m are integer variables.
as long as n is greater than m and neither are negative
3.19
Explain in detail how the increment() methodworks.
resets to 0 once the "limit" is reached
3.20
Rewrite the increment() method without themodulo operator, using an if statement.
Which solution is better?

value = value+1
if value = limit
value = 0

i prefer the increment method.
3.21
Using the clock-display project in BlueJ, test theNumberDisplay class by creating a fewNumberDisplay objects and calling theirmethods.
Done
3.22
Create a ClockDisplay object by selecting the following constructor:
        new ClockDisplay()
Call its getTime() method to find out theinitial time the clock has been set to.
Can you work out why it starts at that particular time?

00:00. the constructor
3.23
How many times would you have to call thetick() method on a newly createdClockDisplay object to make its time reach 01:00?
How else could you make it display that time?

60 times. You can use the setTime() method
3.24
Write the signature of a constructor that matches the following object creation instruction:
new Editor ("readme.txt, -1)

public void Editor()
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 Method (Rectangle window)
{
window = newRectangle (40, 24);
}
3.26
Look at the second constructor in ClockDisplay's source code.
Explain what it does and how it does it.

it allows us to set the time.
3.27
Identify the similarities and differences between the two constructors.
Why is there no call to updateDisplay() in the second constructor, for instance?

It is updated after the setTime() method is called on.
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.
p1.getStatus (10);
p1.print ("Tal is cool", true);
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.

Done.
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?

doing the 24 house clock option might be harder but I believe it is easier to determine whether it is AM or PM more easily through this method whilst it's more difficult to do so by storing hour values from 1-12 which might sound easier.
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 thegetNextMailItem() or printnextMailItem()methods).

Done
3.32
Draw an object diagram of the situation you have after creating a mail-server and three mail clients.
Done
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.

Done
3.34
Open the editor for the MailClient class and set a breakpoint at the first line of theprintNextMailItem() method.
breakpoint?
3.35
Step one line forward in the execution of theprintNextMailItem() method by clicking the Stepbutton.
Done
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 next to be executed would be System.out.println("No new mail");

I was wrong, the item wasn't empty and the first part wasn't executed
3.37
Call printNextMailItem() again.
Step through the method again, as before.
What do you observe? Explain why this is.

It didn't execute because the item is empty
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.


3.39
Set a breakpoint in the first line of thesendMessage() method in the MailClientclass. 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.

Done
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.

Done
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 throuh them.
Is the behavior what you expected?
Did this give you new insights? If so what were they?

Done, not what I expected
3.42
Use the debugger to investigate theinsertMoney() method of the better-ticket-machine project from chapter 2.
Conduct tests that cause both branches of theif statement to be executed.

Done
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.

Done
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 aScreen object, and then call its clear()method if and only if its number of pixels is gretaer than 2 million

Screen screen = new Screen(192, 4243);
if (screen.numberOfPixels() > 2000000)
screen.clear (true);