Java Reference
In-Depth Information
example, if the user entered month 2 and year 2012 , the program should display
that February 2012 had 29 days. If the user entered month 3 and year 2015 , the
program should display that March 2015 had 31 days.
3.12
( Check a number ) Write a program that prompts the user to enter an integer and
checks whether the number is divisible by both 5 and 6, or neither of them, or just
one of them. Here are some sample runs for inputs 10 , 30 , and 23 .
10 is divisible by 5 or 6, but not both
30 is divisible by both 5 and 6
23 is not divisible by either 5 or 6
*3.13
( Financial application: compute taxes ) Listing 3.6, ComputeTax.java, gives the
source code to compute taxes for single filers. Complete Listing 3.6 to give the
complete source code.
3.14
( Game: heads or tails ) Write a program that lets the user guess whether the flip of
a coin results in heads or tails. The program randomly generates an integer 0 or 1 ,
which represents head or tail. The program prompts the user to enter a guess and
reports whether the guess is correct or incorrect.
**3.15
( Game: lottery ) Revise Listing 3.9, Lottery.java, to generate a lottery of a three-
digit number. The program prompts the user to enter a three-digit number and
determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is
$10,000.
2. If all the digits in the user input match all the digits in the lottery number, the
award is $3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is
$1,000.
3.16
( Random character ) Write a program that displays a random uppercase letter
using the Math.random() method.
*3.17
( Game: scissor, rock, paper ) Write a program that plays the popular scissor-rock-
paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can
wrap a rock.) The program randomly generates a number 0 , 1 , or 2 representing
scissor, rock, and paper. The program prompts the user to enter a number 0 , 1 , or
2 and displays a message indicating whether the user or the computer wins, loses,
or draws. Here are sample runs:
scissor (0), rock (1), paper (2):
The computer is scissor. You are rock. You won
1
scissor (0), rock (1), paper (2):
The computer is paper. You are paper too. It is a draw
2
*3.18
( Use the input dialog box ) Rewrite Listing 3.8, LeapYear.java, using the input
dialog box.
**3.19
( Compute the perimeter of a triangle ) Write a program that reads three edges for a
triangle and computes the perimeter if the input is valid. Otherwise, display that
the input is invalid. The input is valid if the sum of every pair of two edges is
greater than the remaining edge.
 
Search WWH ::




Custom Search