Note
Solutions to even-numbered exercises are on the Companion website. Solutions to all exercises are on the Instructor Resource website. The level of difficulty is rated as easy (no star), moderate (*), hard (**), or challenging (***). |
Pedagogical Note
Students may first write a program with a fixed input value and later modify it using an input dialog box or using the console input. |
Debugging Tip
The compiler usually gives a reason for a syntax error. If you don't know how to correct it, compare your program closely with similar examples in the text character by character. |
2.1 | ( Converting Fahrenheit to Celsius ) Write a program that reads a Fahrenheit degree in double from an input dialog box, then converts it to Celsius and displays the result in a message dialog box. The formula for the conversion is as follows : celsius = (5/9) * (fahrenheit - 32) Hint
| ||||
2.2 | ( Computing the volume of a cylinder ) Write a program that reads in the radius and length of a cylinder and computes its volume using the following formulas:
| ||||
2.3 | ( Converting feet into meters ) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot is 0.305 meters. | ||||
2.4 | ( Converting pounds into kilograms ) Write a program that converts pounds into kilograms. 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. | ||||
2.5* | ( Calculating tips ) Write a program that reads the subtotal and the gratuity rate, and 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. | ||||
| |||||
2.6** | ( Summing the digits in an integer ) Write a program that reads an integer between 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
|
2.7* | ( Converting an uppercase letter to lowercase ) Write a program that converts an uppercase letter to a lowercase letter. The character is typed in the source code as a literal value. In Chapter 8, "Strings and Text I/O," you will learn how to enter a character from an input dialog box. Hint
| ||
2.8* | ( Finding the character of an ASCII code ) Write a program that receives an ASCII code (an integer between and 127 ) and displays its character. For example, if the user enters 97, the program displays character a. |
2.9* | ( Calculating the future investment value ) Write a program that reads in investment amount, annual interest rate, and number of years , and displays the future investment value using the following formula: futureInvestmentValue = investmentAmount x (1 + monthlyInterestRate) numberOfYears*12 For example, if you entered amount 1000 , annual interest rate 3.25 %, and number of years 1 , the future investment value is 1032.98 . Hint
| ||
2.10* | ( Monetary units ) Rewrite Listing 2.7, ComputeChange.java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents . For example, the input 1156 represents 11 dollars and 56 cents. |
2.11* | ( Using the console input ) Rewrite Listing 2.7, ComputeChange.java, using the console input and output. |
2.12* | ( Payroll ) Write a program that reads the following information and prints a payroll statement:
Write this program in two versions: (a) Use dialog boxes to obtain input and display output; (b) Use console input and output. A sample run of the console input and output is shown in Figure 2.13. Figure 2.13. The program prints a payroll statement. |
2.13* | ( Calculating interest ) If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula: interest = balance x (annualInterestRate / 1200) Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month in two versions: (a) Use dialog boxes to obtain input and display output; (b) Use console input and output. A sample run of the console input and output is shown in Figure 2.14. Figure 2.14. The program prints interest.
|