Monday, September 27, 2010

Chapter 3 test questions

3. The class ConvertToCents is self explanatory. For example, if we create an object and put in $45 and then call int convert() it gives us 45, 00. Basically it converts to value to cents.

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

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);
changeInCents=numQuarters.getChange();
System.out.println("" +numQuarters.getQuarters() + " quarter(s)" );
numDimes = new NumberOfDimes(changeInCents);
changeInCents=numDimes.getChange();
System.out.println ("" +numDimes.getDimes() + "Dimes (s)");
numNickels = new NumberOfNickels (changeInCents);
changeInCents=numNickels.getChange();
System.out.println ("" +numNickels.getNickels() + "Nickels (s)");
System.out.println ("" +changeInCents + "cent(s)");
}//getChange


}//class Transaction


5. Done
6.


Saturday, September 25, 2010

Chapter 3 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.

Done!

3.2

At what time(s) can a class diagram change?

How is it changed?

A class diagram can change when a new class is added or removed.

3.3

At what time(s) can an object diagram change?

How is it changed?

An object diagram can change when a new object reference is added or removed.

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 it!

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 value is displayed as 0.

3.7

What would happen if you replaced the ">="operator in the test with ">", so that it read..

if((replacementValue > 0) && (replacementValue <>

If we replace “>=” with “>”, 0 can’t be included and the clock will be without 0’s.

3.8

What would happen if you replaced the "&&"operator in the test with "||", so that it read..

if((replacementValue >= 0) || (replacementValue <>

You would either have to set the value greater than or equal to 0 OR less than 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 aand b that evaluates to true when either a andb are both true or both false.

(a && b) || (!a && !b)

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) && (!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 within it?

What happens if you create a number display with limit 800, for instance?

It returns 00

It assumes that the value will be two spaces.

It takes in values from 00 till the number you enter while creating the object.

3.14

Is there any difference in the result of writing

return value + "";

Rather than

return "" + value;

in the getDisplayValue() method?

No! It acts the same way.

3.15

Explain the modulo operator.

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?

3.18

What are all possible results of the expression (n%m), where n and m are integer variables.

All whole numbers.

3.19

Explain in detail how the increment() method works.

The method adds 1. Then the modulo rule is applied and if the number is greater than or equal to the value we assigned in the beginning, the value is set to 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;

The if statement is better because it is easier to understand.

3.21

Using the clock-display project in BlueJ, test theNumberDisplay class by creating a fewNumberDisplay objects and calling their methods.

Done it!

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?

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?

We would have to call thetick() method 60 times on a newly created ClockDisplay object to make its time reach 01:00.

You can also make it display that time by calling void method setTime (int hour, int minute) and setting the hour to be 1, and minute to be 0.



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?

Probably because we didn't assign it any other value.
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. Or you could use the 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. It calls the method setTime so that we can decide what time we want to start with.
3.27
Identify the similarities and differences between the two constructors.
Why is there no call to updateDisplay() in the second constructor, for instance?

Because in the second one, we decide when we create the object what time/values we want to start with. updateDisplay() gets the values of the two objects because we don't specify what we want to start with.
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.
Posted by Nives at 12:45 PM 0 comments