Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 15

Write an application that displays the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form System.out.print( '*' ); which causes the asterisks to print side by side. A statement of the form System.out.println(); can be used to move to the next line. A statement of the form System.out.print( ' ' ); can be used to display a space for the last two patterns. There should be no other output statements in the program. [Hint: The last two patterns require that each line begin with an appropriate number of blank spaces.]

Problem 16

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 is read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******.

Problem 17

A mail-order house 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.

Problem 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 ) );

Problem 20

Calculate the value of \(\pi\) from the infinite series $$\pi=4-\frac{4}{3}+\frac{4}{5}-\frac{4}{7}+\frac{4}{9}-\frac{4}{11}+\cdots$$ Print a table that shows the value of \(\pi\) approximated by computing one term of this series, by two terms, by three terms, and so on. How many terms of this series do you have to use before you first get \(3.14 ? 3.141 ? 3.1415 ? 3.14159 ?\)

Problem 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 to find all 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 will learn in more advanced computer science courses that for many interesting problems there is no known algorithmic approach other than using sheer brute force.

Problem 23

(De Morgan’s Laws) In this chapter, we have 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 expression (!condition1 || !condition2). Also, the expression !(condition1 || condition2) is logically equivalent to the expression (!condition1 && !condition2). Use De Morgan’s Laws to write equivalent expressions for each of the following, then write an application to show that both the original expression and the new expression in each case produce the same value: a) !( x < 5 ) && !( y >= 7 ) b) !( a == b ) || !( g != 5 ) c) !( ( x <= 8 ) && ( y > 4 ) ) d) !( ( i > 4 ) || ( j <= 6 ) )

Problem 24

Write an application that prints the following diamond shape. You may use output statements that print a single asterisk (*), a single space or a single newline character. Maximize your use of repetition (with nested for statements), and minimize the number of output statements.

Problem 26

A criticism of the break statement and the continue statement is that each is unstructured. Actually, these statements can always be replaced by structured statements, although doing so can be awkward. Describe in general how you would remove any break statement from a loop in a program and replace it with some structured equivalent. [Hint: The break statement exits a loop from the body of the loop. The other way to exit is by failing the loop-continuation test. Consider using in the loop-continuation test a second test that indicates “early exit because of a ‘break’ condition.”] Use the technique you develop here to remove the break statement from the application in Fig. 5.12.

Problem 27

What does the following program segment do? for ( i = 1; i <= 5; i++ ) { for ( j = 1; j <= 3; j++ ) { for ( k = 1; k <= 4; k++ ) System.out.print( '*' ); System.out.println(); } // end inner for System.out.println(); } // end outer for

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks