CS 12 Homework Assignment 7


CS 12 Homepage
This assignment is worth 30 points and is due on Monday, August 25, 2003, at the start of the lecture. Please hand in printed solutions.

Note: In the problems below, don't worry about writing complicated main() functions. Write the simplest main() needed to get the job done. The point here is not to get complete programs, but to exercise your skill in creating, declaring, and implementing classes. In this context, main() is merely a tool.

[2 points] 1. Explain, in your own words, what the Principle of Encapsulation is all about.

[3 points] 2. Draw an UML diagram for a class Book. It's up to you to come up with its member variables and member functions. I want at least two member variables and at least two member functions. Does it make sense for this class to have mutator functions? Explain your answer.

[5 points] 3. Suppose that you're going to write a graphical user interface program to control your VCR. Clearly, you will need a class to represent buttons that can be toggled on or off, such as the "Play" button or the "Pause" button. Write the declaration and implementation for a class ToggleButton described by the UML diagram below:

ToggleButton.jpg

The press() function should flip the button's on/off state, then print the button's label and whether the button is now on or off. Answer the following questions:

[1 point] 4. Write a main() function that (a) creates a button object labelled "Play" and (b) "presses" it. Append to your homework solutions a printout of the result.

[5 points] 5. Now imagine that you're writing a program to keep track of your monthly expenses. You will need a class Money to describe dollar amounts, and a simplified version is described by the UML diagram below. Write the declaration and implementation of this class. What implementations would make sense for the constructors with (a) one argument and (b) no arguments? Explain your answers. The member function printValue() should print the amount represented by an object of this class in the familiar format. For example, a Money(2, 14) object would be printed as '$2.14' (without the quotes, of course). Note that if you create an object such as, say, Money(3, 257), you have two choices: either convert the 257 cents into the appropriate number of dollars and cents in the constructor, or do the conversion when printing the value. Either one is fine, but try to give a reason why you picked one choice instead of the other.

Money.jpg

[5 points] 6. Write the declaration and implementation of a class Time described by the UML diagram below. What implementations would make sense for the constructors with (a) one argument, (b) two arguments, and (c) no arguments? The member function int secondsFrom(Time t) takes another Time object and should return the number of seconds between that object and the object on which the function is called. For example, if Time before = Time(2, 7, 10) and Time after = Time(5, 17, 32), then after.secondsFrom(before) would return the value 11422. The member function print() should print the object it's called on in the usual form. For example, the object Time(23, 41, 8) would be printed as '23h 41m 8s' (without the quotes, of course). Once again, you have a choice of when to perform the necessary conversions when you're given more than 60 seconds or more than 60 minutes. As before, you can do that conversion at construction time or inside the other member functions. Which option makes more sense?

Time.jpg

[1 point] 7. Write a main() function that creates two Time objects, Time(23, 41, 8) and Time(9, 53, 37), and prints the number of hours, minutes, and seconds between them (that is, the time interval between those two objects). Append to your homework solutions a printout of the results. Hint: find the number of seconds between the two objects, create a new Time object with that many seconds (but no hours and no minutes) and then call print() on that new object.

[3 points] 8. Write the declaration and implementation for a (overly-simplified!) URLConnection class described by the UML diagram below. The constructor should only open a connection if no more than the maximum number of connections have already been opened.

URLConnection.jpg

[1 point] 9. Write a main() function that creates 5 objects of type URLConnection and prints whether or not they're open. The output should look something like this:
Connection 1 to www.google.com is open.
Connection 2 to www.ucr.edu is open.
...
Connection 5 to www.cs.ucr.edu/cs12/ was not opened.
Make sure to run your main() and append to your homework solutions a printout of the results. You may choose any URL strings you like, since we're not actually connecting to anything here.

[3 points] 10. Write the declaration and implementation for each class described by the class diagram below, according to their UML diagrams.

Coins.jpg

[1 point] 11. Using the classes from problem 10, write a main() function that creates specific coin objects (that is, a number of pennies, nickels, dimes, and quarters) and then prints the total amount of change they represent. Thus, if in your main() you created the objects Dime d1 = Dime(), Dime d2 = Dime(), Quarter q = Quarter(), and Penny p = Penny(), then your main() would print something like "The total amount of change you have is 0 dollars and 46 cents."

© 2003 UC Riverside Department of Computer Science & Engineering. All rights reserved.