Java Reference
In-Depth Information
5.16 (Bar Chart Printing Program) One interesting application of computers is to display
graphs and bar charts. Write an application that reads five numbers between 1 and 30. For each
number that's read, your program should display the same number of adjacent asterisks. For exam-
ple, if your program reads the number 7, it should display ******* . Display the bars of asterisks after
you read all five numbers.
5.17 (Calculating Sales) An online retailer sells five products whose retail prices are as follows:
Product 1, $2.98; product 2, $4.50; product 3, $9.98; product 4, $4.49 and product 5, $6.87.
Write an application that reads a series of pairs of numbers as follows:
a) product number
b) quantity sold
Your program should use a switch statement to determine the retail price for each product. It
should calculate and display the total retail value of all products sold. Use a sentinel-controlled
loop to determine when the program should stop looping and display the final results.
5.18 (Modified Compound-Interest Program) Modify the application in Fig. 5.6 to use only in-
tegers to calculate the compound interest. [ Hint: Treat all monetary amounts as integral numbers
of pennies. Then break the result into its dollars and cents portions by using the division and re-
mainder operations, respectively. Insert a period between the dollars and the cents portions.]
5.19
Assume that i = 1 , j = 2 , k = 3 and m = 2 . What does each of the following statements print?
a) System.out.println(i == 1 );
b) System.out.println(j == 3 );
c) System.out.println((i >= 1 ) && (j < 4 ));
d) System.out.println((m <= 99 ) & (k < m));
e) System.out.println((j >= i) || (k == m));
f) System.out.println((k + m < j) | ( 3 - j >= k));
g) System.out.println(!(k > m));
5.20
π
π
(Calculating the Value of
) Calculate the value of
from the infinite series
4
---
4
---
4
---
4
---
4
11
-
-
-
π
=
4
+
+
------
+
π
Print a table that shows the value of
approximated by computing the first 200,000 terms of this
series. How many terms do you have to use before you first get a value that begins with 3.14159?
5.21 (Pythagorean Triples) A right triangle can have sides whose lengths are all integers. The set
of three integer values for the lengths of the sides of a right triangle is called a Pythagorean triple.
The lengths of the three sides must satisfy the relationship that the sum of the squares of two of the
sides is equal to the square of the hypotenuse. Write an application that displays a table of the
Pythagorean triples for side1 , side2 and the hypotenuse , all no larger than 500. Use a triple-nested
for loop that tries all possibilities. This method is an example of “brute-force” computing. You'll
learn in more advanced computer science courses that for many interesting problems there's no
known algorithmic approach other than using sheer brute force.
5.22 (Modified Triangle Printing Program) Modify Exercise 5.15 to combine your code from
the four separate triangles of asterisks such that all four patterns print side by side. [ Hint: Make clev-
er use of nested for loops.]
5.23 (De Morgan's Laws) In this chapter, we discussed the logical operators && , & , || , | , ^ and ! .
De Morgan's laws can sometimes make it more convenient for us to express a logical expression.
These laws state that the expression !( condition1 && condition2 ) is logically equivalent to the expres-
sion (! condition1 || ! condition2 ) . Also, the expression !( condition1 || condition2 ) is logically
equivalent to the expression (! condition1 && ! condition2 ) . Use De Morgan's laws to write equivalent
 
Search WWH ::




Custom Search