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. | There is LabClass and Student in the class diagram. There are 3 student objects and one LabClass object in the object diagram. But I had some misunderstanding. |
3.2 | At what time(s) can a class diagram change? How is it changed? | It's changes when we add a new class. |
3.3 | At what time(s) can an object diagram change? How is it changed? | It's changes when you create a new object reference. |
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 a ClockDisplayobject, then open an inspector window for thisobject. With the inspector open call the object'smethods. 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. | Done |
3.6 | What happens when the setValue() method iscalled with an illegal value? Is this a good solution? Can you think of a better solution? | The time is set back to default that is 0. |
3.7 | What would happen if you replaced the ">="operator in the test with ">", so that it read.. if((replacementValue > 0) && (replacementValue < limit)) | You couldn't use 0 because it is not included in the coding. But it can be an exception when you have a 0:0 in a clock that can signify either mid day or mid night. |
3.8 | What would happen if you replaced the "&&"operator in the test with "||", so that it read.. if((replacementValue >= 0) || (replacementValue < limit)) | It would be one or the other. For the time to be set, you would have to either enter a value bigger or equal to zero OR 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 | 1.false 2. true 3. false 4. False 5. true |
3.10 | Write an expression using boolean variables aand b that evaluates to true when either a andb are both true or both false. | (a && b) I I (!a && !b) The exclamation mark is for Not equal to. |
3.11 | Write an expression using boolean variables aand b that evaluates to true when only one of aor b is true, and which is false if a and b are both false or both true. (This is also called anexclusive or.) | (a && !b) I I (!a && b) |
3.12 | Consider the expression (a && b). Write an equivalent expression (one that evaluates to true at exactly the same values fora 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? | It returns 00 - it assumes the value will be 2 spaces long. |
3.14 | Is there any difference in the result of writing return value + ""; Rather than return "" + value; in the getDisplayValue() method? | No it works the same way. |
3.15 | Explain the modulo operator. | 21/2 would be 10 and the remainder would be 1. That's because 10 times 2 is 20. 11 times 2 is 22. When you divide 21 by 2, you get 10,33333 etc. But we cannot have decimals. So normally, you would get 10 as the result. But the modulo operator returns 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? | 1 to 4. |
3.18 | What are all possible results of the expression (n%m), where n and m are integer variables. | Only whole numbers |
3.19 | Explain in detail how the increment() method works. | First, the methods adds 1. Then it uses the modulo operator and if the number is bigger than 59, it sets the value to be 0. If it's within the limit, it increments it. |
3.20 | Rewrite the increment() method without themodulo operator, using an if statement. Which solution is better? | if (value > limit) value ++; else value = 0; If statement is more convenient for me. |
3.21 | Using the clock-display project in BlueJ, test theNumberDisplay class by creating a fewNumberDisplay objects and calling their methods. | Done. |
3.22 | Create a ClockDisplay object by selecting the following constructor: new ClockDisplay() Call its getTime() method to find out the initialtime the clock has been set to. Can you work out why it starts at that particular time? | Because we did not set it to any specific time. |
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 because there are 60 seconds in a minute. Or, 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's the same as the first constructor except for the last statement. |
3.27 | Identify the similarities and differences between the two constructors. Why is there no call to updateDisplay() in the second constructor, for instance? | Error |
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.print("file.txt", false) p1.pring("document.txt", true) p1.getStatus(10) p1.getStatus(20) |
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 it. |
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? | I think both are equally useful. |
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. | Done |
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. | The line following the one selected now System.out.println("No new mail"); I was wrong - it highlighted item.print(). It is because the item wasn't empty and only the second part of the statement was executed. |
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 only that item is empty at the moment. |
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. | It displays them line by line. |
No comments:
Post a Comment