Java Reference
In-Depth Information
2.4
( Convert pounds into kilograms ) Write a program that converts pounds into kilo-
grams. The program prompts the user to enter a number in pounds, converts it
to kilograms, and displays the result. One pound is 0.454 kilograms. Here is a
sample run:
Enter a number in pounds: 55.5
55.5 pounds is 25.197 kilograms
*2.5
( Financial application: calculate tips ) Write a program that reads the subtotal
and the gratuity rate, then computes the gratuity and total. For example, if the
user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5
as gratuity and $11.5 as total. Here is a sample run:
Enter the subtotal and a gratuity rate: 10 15
The gratuity is $1.5 and total is $11.5
**2.6
( Sum the digits in an integer ) Write a program that reads an integer between 0 and
1000 and adds all the digits in the integer. For example, if an integer is 932 , the
sum of all its digits is 14 .
Hint : Use the % operator to extract digits, and use the / operator to remove the
extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93 .
Here is a sample run:
Enter a number between 0 and 1000: 999
The sum of the digits is 27
*2.7
( Find the number of years ) Write a program that prompts the user to enter the
minutes (e.g., 1 billion), and displays the number of years and days for the min-
utes. For simplicity, assume a year has 365 days. Here is a sample run:
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days
*2.8
( Current time ) Listing 2.7, ShowCurrentTime.java, gives a program that displays
the current time in GMT. Revise the program so that it prompts the user to enter
the time zone offset to GMT and displays the time in the specified time zone. Here
is a sample run:
Enter the time zone offset to GMT: −5
The current time is 4:50:34
 
Search WWH ::




Custom Search