3. Selection Statements

 
[Page 63 ( continued )]

Programming Exercises

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.


Sections 2.2 “2.8

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

In Java, 5 / 9 is 0, so you need to write 5.0 / 9 in the program to obtain the correct result.


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:
area = radius * radius * p
volume = area * length

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.

[Page 64]
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

Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93


Section 2.9 Character Data Type and Operations

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

In the ASCII table (see Appendix B), uppercase letters appear before lowercase letters . The offset between any uppercase letter and its corresponding lowercase letter is the same. So you can find a lowercase letter from its corresponding uppercase letter, as follows:

   int   offset = (   int   )   'a'   - (   int   )   'A'   ;   char   lowercase = (   char   )((   int   )uppercase + offset); 


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.

Sections 2.10 “2.12

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

Use the Math.pow(a, b) method to compute a raised to the power of b .


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.

Section 2.13 Getting Input from the Console

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:
  • Employee's name (e.g., Smith)

  • Number of hours worked in a week (e.g., 10)

  • Hourly pay rate (e.g., 6.75)


    [Page 65]
  • Federal tax withholding rate (e.g., 20%)

  • State tax withholding rate (e.g., 9%)

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.


 


[Page 66]
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net