4.12. Review Questions

 
[Page 122 ( continued )]

Programming Exercises

Pedagogical Note

A problem often can be solved in many different ways. Students are encouraged to explore various solutions.


Sections 4.2 “4.7

4.1* ( Repeating additions ) Listing 4.1, SubtractionTutorLoop.java, generates ten random subtraction questions. Revise the program to generate ten random addition questions for two integers between 1 and 15. Display the number of correct answers and test time.
4.2* ( Counting positive and negative numbers and computing the average of numbers ) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values, not counting zeros. Your program ends with the input 0. Display the average as a floating-point number. (For example, if you entered 1 , 2 , and , the average should be 1.5 .)

[Page 123]
4.3 ( Conversion from kilograms to pounds ) Write a program that displays the following table (note that 1 kilogram is 2.2 pounds):
 Kilograms Pounds 1 2.2 3 6.6 ... 197 433.4 199 437.8 

4.4 ( Conversion from miles to kilometers ) Write a program that displays the following table (note that 1 mile is 1.609 kilometers):
 Miles Kilometers 1 1.609 2 3.218 ... 9 14.481 10 16.09 

4.5 ( Conversion from kilograms to pounds ) Write a program that displays the following two tables side-by-side (note that 1 kilogram is 2.2 pounds):
 Kilograms Pounds Pounds Kilograms 1 2.2 20 9.09 3 6.6 25 11.36 ... 197 433.4 510 231.82 199 437.8 515 234.09 

4.6 ( Conversion from miles to kilometers ) Write a program that displays the following two tables side-by-side (note that 1 mile is 1.609 kilometers):
 Miles Kilometers Kilometers Miles 1 1.609 20 12.430 2 3.218 25 15.538 ... 9 14.481 60 37.290 10 16.09 65 40.398 

4.7** ( Computing future tuition ) Suppose that the tuition for a university is $10,000 this year and increases 5% every year. Write a program that uses a loop to compute the tuition in ten years. Write another program that computes the total cost of four years' worth of tuition starting ten years from now.
4.8 ( Finding the highest score ) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score.
4.9* ( Finding the two highest scores ) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score and the student with the second-highest score.
4.10 ( Finding numbers divisible by 5 and 6 ) Write a program that displays all the numbers from 100 to 1000 , ten per line, that are divisible by 5 and 6 .
4.11 ( Finding numbers divisible by 5 or 6 , but not both ) Write a program that displays all the numbers from 100 to 200 , ten per line, that are divisible by 5 or 6 , but not both.
4.12 ( Finding the smallest n such that n 2 > 12000 ) Use a while loop to find the smallest integer n such that n is greater than 12,000 .

[Page 124]
4.13 ( Finding the largest n such that n 3 < 12000 ) Use a while loop to find the largest integer n such that n is less than 12,000 .
4.14* ( Displaying the ACSII character table ) Write a program that prints the characters in the ASCII character table from ' ! ' to ' ~ '. Print ten characters per line.

Section 4.8 Case Studies

4.15* ( Computing the greatest common divisor ) Another solution for Listing 4.6 to find the greatest common divisor of two integers n1 and n2 is as follows : First find d to be the minimum of n1 and n2 , then check whether d , d-1 , d-2 , 2 , or 1 is a divisor for both n1 and n2 in this order. The first such common divisor is the greatest common divisor for n1 and n2 .
4.16** ( Finding the factors of an integer ) Write a program that reads an integer and displays all its smallest factors. For example, if the input integer is 120 , the output should be as follows: 2 , 2 , 2 , 3 , 5 .
4.17* ( Finding the sales amount ) Rewrite Listing 4.7, FindSalesAmount.java, as follows:
  • Use a for loop instead of a do-while loop.

  • Let the user enter COMMISSION_SOUGHT instead of fixing it as a constant.

4.18* ( Printing four patterns using loops ) Use nested loops that print the following patterns in four separate programs:
 Pattern I Pattern II Pattern III Pattern IV 1 1 2 3 4 5 6 1 1 2 3 4 5 6 1 2 1 2 3 4 5 2 1 1 2 3 4 5 1 2 3 1 2 3 4 3 2 1 1 2 3 4 1 2 3 4 1 2 3 4 3 2 1 1 2 3 1 2 3 4 5 1 2 5 4 3 2 1 1 2 1 2 3 4 5 6 1 6 5 4 3 2 1 1 

4.19** ( Printing numbers in a pyramid pattern ) Write a nested for loop that prints the following output:
 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 16 8 4 2 1 1 2 4 8 16 32 64 32 16 8 4 2 1 1 2 4 8 16 32 64 128 64 32 16 8 4 2 1 

Hint

Here is the pseudocode solution:

   for   the row from 0 to 7 { Pad leading blanks in a row using a loop like this:   for   the column from 1 to 7-row System.out.print(" "); Print left half of the row for numbers 1, 2, 4, up to 2    row    using a look like this:   for   the column from 0 to row System.out.print(" " + (int)Math.pow(2, column)); Print the right half of the row for numbers 2    row-1    , 2    row-2    , ..., 1 using a loop like this: 

[Page 125]
   for   (int column = row - 1; column >= 0; col--) System.out.print(" " + (int)Math.pow(2, column)); Start a new line System.out.println(); } 


You need to figure out how many spaces to print before the number. This is dependent on the number. If a number is a single digit, print four spaces. If a number has two digits, print three spaces. If a number has three digits, print two spaces.

The Math.pow() method was introduced in §2.12.1, "Example: Computing Loan Payments." Can you write this program without using it?

4.20* ( Printing prime numbers between 2 and 1000 ) Modify Listing 4.11 to print all the prime numbers between 2 and 1000 , inclusively. Display eight prime numbers per line.

Comprehensive

4.21** ( Comparing loans with various interest rates ) Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8. Suppose you enter the loan amount 10,000 for five years; display a table as follows:
 Loan Amount: 10000 Number of Years: 5 Interest Rate Monthly Payment Total Payment 5% 188.71 11322.74 5.125% 189.28 11357.13 5.25% 189.85 11391.59 ... 7.85% 202.16 12129.97 8.0% 202.76 12165.83 

4.22** ( Displaying the loan amortization schedule ) The monthly payment for a given loan pays the principal and the interest. The monthly interest is computed by multiplying the monthly interest rate and the balance (the remaining principal). The principal paid for the month is therefore the monthly payment minus the monthly interest. Write a program that lets the user enter the loan amount, number of years, and interest rate, and displays the amortization schedule for the loan. Suppose you enter the loan amount 10,000 for one year with an interest rate of 7%; display a table as follows:
 Loan Amount: 10000 Number of Years: 1 Annual Interest Rate: 7% Monthly Payment: 865.26 Total Payment: 10383.21 Payment# Interest Principal Balance 1 58.33 806.93 9193.07 2 53.62 811.64 8381.43 ... 11 10.0 855.26 860.27 12 5.01 860.25 0.01 

Note

The balance after the last payment may not be zero. If so, the last payment should be the normal monthly payment plus the final balance.



[Page 126]

Hint

Write a loop to print the table. Since monthly payment is the same for each month, it should be computed before the loop. The balance is initially the loan amount. For each iteration in the loop, compute the interest and principal, and update the balance. The loop may look like this:

   for   (i =   1   ; i <= numberOfYears *   12   ; i++) { interest = monthlyInterestRate * balance; principal = monthlyPayment 2 interest; balance = balance 2 principal; System.out.println(i +   "\t\t"   + interest + +   "\t\t"   + principal +   "\t\t"   + balance); } 


4.23* ( Demonstrating cancellation errors ) A cancellation error occurs when you are manipulating a very large number with a very small number. The large number may cancel out the smaller number. For example, the result of 100000000.0 + 0.000000001 is equal to 100000000.0 . To avoid cancellation errors and obtain more accurate results, carefully select the order of computation. For example, in computing the following series, you will obtain more accurate results by computing from right to left rather than from left to right:


Write a program that compares the results of the summation of the preceding series, computing from left to right and from right to left with n = 50000 .

4.24* ( Summing a series ) Write a program to sum the following series:


4.25** ( Computing p) You can approximate by using the following series:


Write a program that displays the value for i = 10000 , 20000 , . . ., and 100000 .

4.26** ( Computing e ) You can approximate e by using the following series:


Write a program that displays the e value for i = 10000 , 20000 , , and 100000 .

Hint


Initialize e and item to be 1 and keep adding a new item to e . The new item is the previous item divided by i for i = 2,3,4, ....

4.27** ( Displaying leap years ) Write a program that displays all the leap years, ten per line, in the twenty-first century (from 2001 to 2100 ).
4.28** ( Displaying the first days of each month ) Write a program that prompts the user to enter the year and first day of the year, and displays the first day of each month in the year on the console. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the following output:
[Page 127]
 January 1, 2005 is Saturday ... December 1, 2005 is Thursday 

4.29** ( Displaying calendars ) Write a program that prompts the user to enter the year and first day of the year, and displays the calendar table for the year on the console. For example, if the user entered the year 2005, and 6 for Saturday, January 1, 2005, your program should display the calendar for each month in the year, as follows:
January 2005
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
...            

December 2005
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

 


[Page 128]
 


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