# Programming Assignment 2 Solution

Programming Assignment 2

Problem Description(s):

1. (Triangle Printing Program) 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.]

2. (Modified Triangle Printing Program) Modify Part 1 (above) to combine your code from the four separate triangles of asterisks such that all four patterns print side by side. [Hint: Make clever use of nested for loops.]

3. (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.

Analysis: - 10 points (account for all 3 parts)

(Describe the problems including input and output in your own words.)

Problem Description(s):

1. (Triangle Printing Program) 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.]

2. (Modified Triangle Printing Program) Modify Part 1 (above) to combine your code from the four separate triangles of asterisks such that all four patterns print side by side. [Hint: Make clever use of nested for loops.]

3. (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.

Analysis: - 10 points (account for all 3 parts)

(Describe the problems including input and output in your own words.)

You'll get a 125.2KB .ZIP file.