Lab 4: Loops Solved

20.00 $ 10.00 $



Lab 4: Loops

Your task is to complete three problems on loops. Upload each of the files as a different program.
1 Assignment 1
Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The rest line will have one asterisk, the next will have two, and so on, with each line
having one more asterisk than the previous line, up to and including the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line
until only one asterisk is displayed.
For example, if the user enters the number 3, the output should be
1.1 Hint
Use nested for loops; the outside loop controls the number of lines to write and the inside loop controls the number of asterisks to display on a line.
The method System.out.print() will print a string without putting a new line after it.

Assignment 2
Write a program that stimulates a bouncing ball by computing its height in feet at each second as time passes on a simulated clock. At time zero, the ball begins at height zero and has an initial velocity supplied by the user.
(An initial velocity of at least 100 feet per second is a good choice.) After each second, change the height by adding the current velocity; then subtract 32 from the velocity. If the new height is less than zero, multiply both the
height and the velocity by -0.5 to simulate the bounce. Stop at the fifth bounce. The output from your program should have the following form:
Enter the initial velocity of the ball: 100
Time: 0 Height: 0.0
Time: 1 Height 100.0
Time: 2 Height 168.0
Time: 3 Height 204.0
Time: 4 Height 208.0
Time: 5 Height 180.0
Time: 6 Height 120.0
Time: 7 Height 28.0
Bounce !
Time: 8 Height 48.0
3 Assignment 3
Holy digits Batman! The Riddler is planning his next caper somewhere on Pennsylvania Avenue. In his usual sporting fashion, he has left the address in the form of a puzzle. The address on Pennsylvania is a four-digit number
with the following properties:
 All four digits are di erent
 The digit in the thousands place is three times the digit in the tens
 The number is odd
 The sum of the digits is 27.
Write a program that uses a loop or loops to nd the address where the Riddler plans to strike.
3.1 Hint
Figure out how to get each of the ones, tens, hundreds, and thousands place
rst. You will need to use division and the modulus operators.