Java Reference
In-Depth Information
quarters, dimes, nickels, and pennies it takes to make that amount. As many high-
er-valued coins as possible should be used. For example, for the amount $4.20,
the program should print on one line:
16 quarters, 2 dimes, 0 nickels, and 0 pennies
E18. Write a function that is given the number of seconds from midnight and
returns a String that contains the number of hours, minutes, and seconds from
midnight that it represents, suitably annotated. For example, called with argu-
ment 3675 , it yields "1 hour 1 minute 15 seconds" .
E19. Write a procedure that is given the number of seconds from midnight and
prints the number of hours, minutes, and seconds from midnight that it repre-
sents, suitably annotated. For example, called with argument 3675 , this should
be printed: 1 hour 1 minute 15 seconds .
E20. Write a function that is given a time in terms of hours, minutes, and sec-
onds and returns the time in seconds only (as an integer). This is, in a sense, the
inverse of the previous exercise. For example, for 1 hour, 1 minute and 15 sec-
onds, return the integer 3676 .
E21. Write a function that is given two times in military format and prints the
hours and minutes between the two times. You may assume that the second
parameter is the bigger of the two. For example, for the arguments (0352, 1900)
—that is 3:52AM and 7:00P— the result is: 15 hours 8 minutes . To make
things easier for you, try doing it by calling the function and procedure of the
previous two exercises.
E22. Write a function with an int parameter whose value is in the range 0..15
and that returns a String of length 4 that depicts its binary equivalent. For
example, for the number 7 , the answer is the String "0111" . Here is a hint. The
rightmost bit is 7%2 , and the first three bits are the binary representation of 7/2 .
E23. Write a procedure with an int parameter whose value is in the range 0..15
and that prints its binary equivalent. For example, for the number 7 , this should
be printed: 0111 . Here is a hint. The rightmost bit is 7%2 , and the first three bits
are the binary representation of 7/2 .
E24. In Sec. 0.1, near Fig. 0.2, we outlined the relationship between the decimal,
hexadecimal, octal, and binary numbers systems. Write a procedure that prints
the first 7 natural numbers ( 0, 1, 2, ..., 8 ) in all four systems. The first line
should contain 0 in all four systems; the second; 1 in all four systems; and so on.
For descriptions of static methods to help you do this exercise, turn to lesson
page 5-1 of the CD and click on the footnote for static methods of class Integer
(near activity 3).
E25. Do the same as for the previous exercise, but print the decimal values 21,
22, 23, 24, 25 in each of the four number systems.
 
Search WWH ::




Custom Search