| CSCI A201/A597Problem Set Two Second Summer 2000 |
| 1. | Write a program that displays the squares, cubes, and fourth powers of the numbers 1-5. |
| 2. |
Write a program that prompts the user for two integers and then
prints
|
| 3. | Write a program that prompts the user for a measurement in meters and then converts it into miles, feet and inches. |
| 4. |
Write a program that prompts the user for a radius
and then prints
|
| 5. |
Write a program that asks the user for the lengths
of the sides of a rectangle. Then print
|
| 6. | Giving change. Implement a program that directs a cashier how to give change. The program has two inputs: the amount due and the amount received from the customer. Compute the difference, and compute the dollars, quarters, dimes, nickels, and pennies that the customer should receive in return. Hint: First transform the difference into an integer balance, denominated in pennies. Then compute the whole dollar amount. Subtract it from the balance. Compute the number of quarters needed. Repeat for dimes and nickels. Display the remaining pennies. |
| 7. |
Write a program that asks the user to input
|
| 8. |
DOS file names and extensions. Write a program that prompts the user for the drive letter
(C), the path (\Windows\System), the file name (ReadMe), and
the extension (TXT). Then print the complete file name
If you use Unix or a Macintosh, useC:\Windows\System\ReadMe.TXT / or : instead to separate
directories).
|
| 9. |
Write a program that reads a number greater than or equal to 1000 from the user
and prints it out with a comma separating the thousands. Here is a sample
dialog; the user input is in color:
Please enter an integer >= 1000: 23456 23,456 |
| 10. |
Write a program that reads a number greater than or equal to 1000 from the user,
where the user enters a comma in the input. Then print the number without a comma.
Here is a sample
dialog; the user input is in color:
Hint: Read the input as a string. Measure the length of the string. Suppose it contains n characters. Then extract substrings consisting of the first n - 4 characters and the last three characters.Please enter an integer between 1,000 and 999,999: 23,456 23456 |
| 11. |
Printing a grid. Write a program that prints the following grid
to play tic-tac-toe.
Of course, you could simply write seven statements of the form+--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+ | | | | +--+--+--+
You should do it a smarter way, though. Define string variables
to hold two kinds of patterns: a comb-shaped pattern and the bottom
line. Print the comb three times and the bottom line once.
|
| 12. |
Write a program that reads an integer and breaks it into a sequence
of individual digits. For example the input 16384 is displayed as
You may assume that the input has no more than five digits and is not negative. Hint: There are two ways of solving this problem. You can use integer arithmetic and repeatedly divide by 10, or you can convert the number into a string and extract the digits from the string.1 6 3 8 4 |
| 13. |
The following program prints the values of sine and cosine for
0 degrees, 30 degrees, 45 degrees, 60 degrees, and 90 degrees. Rewrite the
program for greater clarity by factoring out common code.
|
| 14. |
Write a program that prints out a message "Hello, my name is Hal!" Then,
on a new line, the program should print the message "What is your name?"
Next the program should read the user's name and print "Hello, user name.
I am glad to meet you." Then, on a new line, the program should print a message
"What would you like me to do?" Then it is the user's turn to type in an input.
Finally the program should ignore the user input and and print the message
"I am sorry, user name. I cannot do that." Here's a typical program run. The user input is printed in color.
Hello, my name is Hal! What is your name? Dave Hello, Dave. I am glad to meet you. What would you like me to do? Clean up my room. I am sorry, Dave. I cannot do that. |
| 15. |
You don't know yet how to program decisions, but it turns out that
there is a way to fake them using substring. Write a program
that asks a user to input
orYou will make it The trick here is to subtract the desired distance from the number of miles the user can drive. Suppose that the number isYou will not make it x.
Suppose further that you find a way of setting a value n
to 1 if x >= 0 and to 0 if x < 0. Then you can
solve your problem:
Hint: Note that x + |x| is 2x if x >= 0,
and 0 if x < 0. Then divide by x, except that you need to worry
about the possibility that x is zero.
|
| 16. |
Write a program that reads two times in military format
(0900, 1730) and prints the number of hours and minutes between the
two times. Here is a sample run. User input is in color.
Extra credit if you can deal with the case that the first time is later than the second time:Please enter the first time: 0900 Please enter the second time: 1700 8 hours 30 minutes Please enter the first time: 1730 Please enter the second time: 0900 15 hours 30 minutes |
| 17. |
Run the following program, and explain the output you get.
Note the trace messages, which are inserted to show the current
contents of the total variable. Then fix up the program, run it with
the trace messages in place to verify that it works correctly, and remove the trace
messages.
|
| 18. |
Writing large letters. A large letter H can be produced like this:
It can be declared as a string constant like this:* * * * ***** * * * * Do the same for the letterspublic static final String LETTER_H = "* *\n* *\n*****\n* *\n* *\n"; E, L, and O.
Then write the message
H E L L O |
| 19. |
Write a program that transforms numbers 1, 2,
3, ..., 12 into the corresponding month names
January, February, March, ...,
December.
Hint: Make a very long string
in which you add spaces such that each month name has the same length. Then use"January February March. . ." substring
to extract the month you want.
|
| 20. |
Change the password program to make it generate more secure passwords.
Use the random numner generator Random in the java.util
package to generate a random number as follows:
Multiply the age by the random number. Then concentrate the initials with the last four digits of the product.int r = new Random().nextInt(1000); |