3.1 Think again about the lab-classes project that we discussed in Chapter 1 and Chapter 2. Imagine that we create a LabClass object and 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.
The difference is that there is 1 LabClass object and 3 student objects in the object diagram.
3.2 At what time(s) can a class diagram change?
How is it changed?
When you change the source code.
3.3 At what time(s) can an object diagram change?
How is it changed?
It can change when the program is working.
3.4 Write a definition for a field named tutor that can hold references to objects of type Instructor. Private Instructor tutor
3.5 Start BlueJ, open the clock-display example and experiment with it. To use it, create a ClockDisplay object, then open an inspector window for this object. With the inspector open call the object's methods. Watch the displayString field in the inspector.
Read the project comment (by double-clicking on the text note icon on the main screen) to get more information.
Okay, Completed.
3.6 What happens when the setValue() method is called with an illegal value?
Is this a good solution?
Can you think of a better solution?
Set the value to zero.
3.7 What would happen if you replaced the ">=" operator in the test with ">", so that it read..
if((replacementValue > 0) && (replacementValue < limit))
That would mean that you are not including zero as a number, and only numbers greater than zero. It would go from 59 to 1 and that is not what you want.
3.8 What would happen if you replaced the "&&" operator in the test with "||", so that it read..
if((replacementValue >= 0) || (replacementValue < limit))
You would have to set a value below the limit.
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
false
true
false
false
true
3.10 Write an expression using boolean variables a and b that evaluates to true when either a and b are both true or both false. (a&&b)
3.11 Write an expression using boolean variables a and b that evaluates to true when only one of a or b is true, and which is false if a and b are both false or both true. (This is also called an exclusive or.) (a&&b)
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.
a==b
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?
Because it goes from 59 to 0 it thinks that the value of the next number (after 59) will be a two digit number.
3.14 Is there any difference in the result of writing
return value + "";
Rather than
return "" + value;
in the getDisplayValue() method?
No.
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? (-4) - (4)
3.18 What are all possible results of the expression (n%m), where n and m are integer variables. All whole numbers
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?
It starts at that particular time because we did not set any specific time for it to start as. (random)
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?
60
Method setTime
3.24 Write the signature of a constructor that matches the following object creation instruction:
new Editor ("readme.txt, -1)
public void Editor (String filename, int number)
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. Rectangle window;
window = new Rectangle(2,5);
3.26 Look at the second constructor in ClockDisplay's source code.
Explain what it does and how it does it.
It initializes the time to the values passed through the method. It uses the method setTime() to set the time to the initial value.
3.27 Identify the similarities and differences between the two constructors.
Why is there no call to updateDisplay() in the second constructor, for instance?
Both constructors create two new NumberDisplays. The first one calls updateDisplay and the second calles setTime. In the second constructor, there is no call to updateDCisplay because this will be done in the method setTime.
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.
Okay, Completed.
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?
updateDisplay(); }
public Clock Display (int hour, int minute) {
hours = new NumberDisplay (12);
minutes new NumberDisplay (60);
setTime (hour, minute); }
private void updateDisplay() {
int hour = hours.getValue ();
if (hour ==0)
hour = 12;
displayString = hour + “:” + minutesgetDisplayValue();
}
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).
Okay, Completed.
3.32 Draw an object diagram of the situation you have after creating a mail-server and three mail clients. Okay, Completed.
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.
Okay, Completed.
3.34 Open the editor for the MailClient class and set a breakpoint at the first line of the printNextMailItem() method. Okay, Completed.
3.35 Step one line forward in the execution of the printNextMailItem() method by clicking the Step button. Okay, Completed.
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 predict: the line following the one selected now System.out.println("No new mail");
I was wrong: it highlighted item.print().
That's because the item wasn't empty and the first part of the if statement wasn't executed, but the second (else) was.
3.37 Call printNextMailItem() again.
Step through the method again, as before.
What do you observe? Explain why this is.
It executes the first part of the if statement because the item is empty - I didn't send a message.
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 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.
Okay, Completed
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.
Okay, Completed.
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?
Okay, Completed. No, this behavior is not what I expected.
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.
Okay, Completed.
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.
Okay, Completed.
3.44 Given the following class..
public class Screen
{
public Screen (int xRes, int yRes)
{}
public int numberOfPixels()
{}3.1 Think again about the lab-classes project that we discussed in Chapter 1 and Chapter 2. Imagine that we create a LabClass object and 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.
The difference is that there is 1 LabClass object and 3 student objects in the object diagram.
3.2 At what time(s) can a class diagram change?
How is it changed?
When you change the source code.
3.3 At what time(s) can an object diagram change?
How is it changed?
It can change when the program is working.
3.4 Write a definition for a field named tutor that can hold references to objects of type Instructor. Private Instructor tutor
3.5 Start BlueJ, open the clock-display example and experiment with it. To use it, create a ClockDisplay object, then open an inspector window for this object. With the inspector open call the object's methods. Watch the displayString field in the inspector.
Read the project comment (by double-clicking on the text note icon on the main screen) to get more information.
Okay, Completed.
3.6 What happens when the setValue() method is called with an illegal value?
Is this a good solution?
Can you think of a better solution?
Set the value to zero.
3.7 What would happen if you replaced the ">=" operator in the test with ">", so that it read..
if((replacementValue > 0) && (replacementValue < limit))
That would mean that you are not including zero as a number, and only numbers greater than zero. It would go from 59 to 1 and that is not what you want.
3.8 What would happen if you replaced the "&&" operator in the test with "||", so that it read..
if((replacementValue >= 0) || (replacementValue < limit))
You would have to set a value below the limit.
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
false
true
false
false
true
3.10 Write an expression using boolean variables a and b that evaluates to true when either a and b are both true or both false. (a&&b)
3.11 Write an expression using boolean variables a and b that evaluates to true when only one of a or b is true, and which is false if a and b are both false or both true. (This is also called an exclusive or.) (a&&b)
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.
a==b
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?
Because it goes from 59 to 0 it thinks that the value of the next number (after 59) will be a two digit number.
3.14 Is there any difference in the result of writing
return value + "";
Rather than
return "" + value;
in the getDisplayValue() method?
No.
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? (-4) - (4)
3.18 What are all possible results of the expression (n%m), where n and m are integer variables. All whole numbers
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?
It starts at that particular time because we did not set any specific time for it to start as. (random)
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?
60
Method setTime
3.24 Write the signature of a constructor that matches the following object creation instruction:
new Editor ("readme.txt, -1)
public void Editor (String filename, int number)
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. Rectangle window;
window = new Rectangle(2,5);
3.26 Look at the second constructor in ClockDisplay's source code.
Explain what it does and how it does it.
It initializes the time to the values passed through the method. It uses the method setTime() to set the time to the initial value.
3.27 Identify the similarities and differences between the two constructors.
Why is there no call to updateDisplay() in the second constructor, for instance?
Both constructors create two new NumberDisplays. The first one calls updateDisplay and the second calles setTime. In the second constructor, there is no call to updateDCisplay because this will be done in the method setTime.
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.
Okay, Completed.
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?
updateDisplay(); }
public Clock Display (int hour, int minute) {
hours = new NumberDisplay (12);
minutes new NumberDisplay (60);
setTime (hour, minute); }
private void updateDisplay() {
int hour = hours.getValue ();
if (hour ==0)
hour = 12;
displayString = hour + “:” + minutesgetDisplayValue();
}
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).
Okay, Completed.
3.32 Draw an object diagram of the situation you have after creating a mail-server and three mail clients. Okay, Completed.
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.
Okay, Completed.
3.34 Open the editor for the MailClient class and set a breakpoint at the first line of the printNextMailItem() method. Okay, Completed.
3.35 Step one line forward in the execution of the printNextMailItem() method by clicking the Step button. Okay, Completed.
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 predict: the line following the one selected now System.out.println("No new mail");
I was wrong: it highlighted item.print().
That's because the item wasn't empty and the first part of the if statement wasn't executed, but the second (else) was.
3.37 Call printNextMailItem() again.
Step through the method again, as before.
What do you observe? Explain why this is.
It executes the first part of the if statement because the item is empty - I didn't send a message.
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 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.
Okay, Completed
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.
Okay, Completed.
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?
Okay, Completed. No, this behavior is not what I expected.
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.
Okay, Completed.
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.
Okay, Completed.
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
Screen screen = new Screen(1024, 768);
if(screen.numberOfPixels() > 2000000)
{
screen.clear(true);
}
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
Screen screen = new Screen(1024, 768);
if(screen.numberOfPixels() > 2000000)
{
screen.clear(true);
}
No comments:
Post a Comment