4. Loops

 
[Page 93 ( continued )]

Programming Exercises

Section 3.2 boolean Data Type and Operations

Pedagogical Note

For each exercise, students should carefully analyze the problem requirements and the design strategies for solving the problem before coding.


Pedagogical Note

Instructors may ask students to document analysis and design for selected exercises. Students should use their own words to analyze the problem, including the input, output, and what needs to be computed and describe how to solve the problem using pseudocode. This has two benefits: (1) it mandates students to think before typing code; (2) it fosters writing skills.


Debugging Tip

Before you ask for help, read and explain the program to yourself, and trace it using several representative inputs by hand or using an IDE debugger. You learn how to program by debugging your own mistakes.


Note

Do not use selection statements for Exercises 3.1 “3.6.


3.1* ( Validating triangles ) Write a program that reads three edges for a triangle and determines whether the input is valid. The input is valid if the sum of any two edges is greater than the third edge. For example, if your input for three edges is 1 , 2 , 1 , the output should be:
   Can edges 1, 2, and 1 form a triangle? false   

If your input for three edges is 2, 2, 1, the output should be:

   Can edges 2, 2, and 1 form a triangle? true   

3.2 ( Checking whether a number is even ) Write a program that reads an integer and checks whether it is even. For example, if your input is 25 , the output should be:
   Is 25 an even number? false   

If your input is 2000 , the output should be:

   Is 2000 an even number? true   


[Page 94]
3.3* ( Using the &&, and ^ operators ) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6 , whether it is divisible by 5 or 6 , and whether it is divisible by 5 or 6 , but not both. For example, if your input is 10 , the output should be
   Is 10 divisible by 5 and 6? false     Is 10 divisible by 5 or 6? true     Is 10 divisible by 5 or 6, but not both? true   

3.4** ( Learning addition ) Write a program that generates two integers under 100 and prompts the user to enter the addition of these two integers. The program then reports true if the answer is correct, false otherwise . The program is similar to Listing 3.3.
3.5** ( Addition for three numbers ) The program in Listing 3.3 generates two integers and prompts the user to enter the addition of these two integers. Revise the program to generate three single-digit integers and prompt the user to enter the addition of these three integers.
3.6* ( Using the console input ) Rewrite Listing 3.2, LeapYear.java, using the console input.

Section 3.3 Selection Statements

3.7 ( Monetary units ) Modify Listing 2.4 to display the non-zero denominations only, using singular words for single units like 1 dollar and 1 penny, and plural words for more than one unit like 2 dollars and 3 pennies. (Use 23.67 to test your program.)
3.8* ( Sorting three integers ) Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables num1 , num2 , and num3 , respectively. The program sorts the numbers so that num1 less-than or equal to num2 less-than or equal to num3 .
3.9 ( Computing 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 any two edges is greater than the third edge (also see Exercise 3.1).
3.10 ( Computing taxes ) Listing 3.4 gives the source code to compute taxes for single filers. Complete Listing 3.4 to give the complete source code.
3.11* ( Finding the number of days in a month ) Write a program that prompts the user to enter the month and year, and displays the number of days in the month. For example, if the user entered month 2 and year 2000, the program should display that February 2000 has 29 days. If the user entered month 3 and year 2005, the program should display that March 2005 has 31 days.
3.12 ( Checking 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 , neither , or just one of them. Here are some sample outputs 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 ( An addition learning tool ) Listing 3.5, SubtractionTutor.java, randomly generates a subtraction question. Revise the program to randomly generate an addition question with two integers less than 100 .
 


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