Monday 27 September 2010

Chapter 3 Test- Jonathan

public void getChange()
{
    int changeInCents = converter.convert();
     numDollars=new NumberOfDollars(changeInCents);
     System.out.println("" +numDollars.getDollars() + " dollar(s)" );

    changeInCents = numDollars.getChange();
     numQuarters = new NumberOfQuarters(changeInCents);
     System.out.println("" +numQuarters.getQuarters() + " quarter(s)");
  
     changeInCents = numQuarters.getChange();
  
     numDimes = new NumberOfDimes(changeInCents);
     System.out.println("" +numDimes.getDimes() + " dimes(s)");
  
     changeInCents = numDimes.getChange();
  
     numNickels = new NumberOfNickels(changeInCents);
     System.out.println("" +numNickels.getNickels() + " nickels(s)");
  
     changeInCents = numNickels.getChange();
  
     System.out.println("" + changeInCents + " cent(s)");
}//getChange






5. Place a check to make sure that the cash tendered by the customer is more than the cost of the goods.


DONE



6. In what way does this project exemplify abstraction and modularization.

In complex software and programming it is easier to divide everything into subcomponents. Instead of using one giant, long and complex class for the money transaction, we divided it into parts. Each part in charge of something else. The abstraction and modularization are used to "divide and conquer" and make this programming a lot more simple. the abstraction and modulazation method resembles a conveyor belt, or the system used to build cars. 

7. Give one example, from this project, of each of the following.

a. an object reference.

b. the use of a primitive type other than int.

primitive type : DOUBLE
Transaction (double totalPrice, double amountGiven)


c. an object creation.

public class Transaction
{
    private double totalPrice;
    private double amountGiven;
    private ConvertToCents converter;
    private NumberOfDollars numDollars;
    private NumberOfQuarters numQuarters;
    private NumberOfDimes numDimes;
    private NumberOfNickels numNickels;

Transaction (double totalPrice, double amountGiven)
{
    this.totalPrice=totalPrice;
    this.amountGiven=amountGiven;
    converter =new ConvertToCents(amountGiven-totalPrice);
}//constructor







d. overloading.

e. an internal method call.

  changeInCents = numDollars.getChange(); it invokes the get change method it is internal since it is in the same class as the method

f. an external method call.

8. Draw an object diagram for one moment in the execution of method getChange() on a Transaction instance that was initialized with..
total price = 5.29
amount of cash = 10.00
You should use the BlueJ 
debugger to halt the execution at line..    changeInCents = numDollars.getChange();

Barnes and Kolling Chapter Three Questions

 Barnes and Kolling Chapter Three Questions


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.

Class diagram will only have Classes student
and lab 
3.2
At what time(s) can a class diagram change?
How is it changed?

when objects are created or deleted
3.3
At what time(s) can an object diagram change?
How is it changed?

when instances are created or deleted
3.4
Write a definition for a field named tutor that can hold references to objects of typeInstructor.
Private instuctor
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?

when illegal value is set, time does not change.
Better solution in an error message requesting a different value
3.7
What would happen if you replaced the ">="operator in the test with ">", so that it read..
  if((replacementValue > 0) && (replacementValue < limit))

value 0 would not be an option . the replacement value could not be zero
3.8
What would happen if you replaced the "&&"operator in the test with "||", so that it read..
  if((replacementValue >= 0) || (replacementValue < limit))

in both cases the value would equal the replacement value
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.
if (a== true&& b==true || a==false && b==false)
return true;
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.)
if
(a==true && b==false || a==false && b==true)
else return false;
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.

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, any value 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.
it calculates the remainder of an integer division
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.
0,1,2.....m-1
3.19
Explain in detail how the increment() method works.
it increments a value by a parameter
example:
value = (value + 1) % limit
so it essentially 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?

if
value > limit
return value + 1
else return value
??
modulo operator is better
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?

initial time 00:00
it is the beginning of the 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

call set the time value mutator method
3.24
Write the signature of a constructor that matches the following object creation instruction:
new Editor ("readme.txt, -1)

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 class rectangle
{
private Rectangle length;
private Rectangle width;
3.26
Look at the second constructor in ClockDisplay's source code.
Explain what it does and how it does it.

it allows you to set the hours and the minutes with two int parameters.
 hours= new NumberDisplay
minutes=new NumberDisplay
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.
public void changeFileName(String newName)
{
name= newName
}

Public void getDoubleSided()
{
if Doublesided==true
return true
else return false}

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


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?


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


3.32
Draw an object diagram of the situation you have after creating a mail-server and three mail clients.

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.


3.34
Open the editor for the MailClient class and set a breakpoint at the first line of theprintNextMailItem() method.

3.35
Step one line forward in the execution of theprintNextMailItem() method by clicking the Stepbutton.

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.


3.37
Call printNextMailItem() again.
Step through the method again, as before.
What do you observe? Explain why this is.


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.


3.40
Use a combination of code reading, execution of methods, breakpoints, and single stepping to familiarize yourself with the MailItem andMailClient classes.
Explain how the MailClient and MailItem classes interact.
Draw object diagrams as part of your explanations.


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?


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.


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.


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