ITI 1120 Lab #5

$30.00

Download Details:

  • Name: Lab-5-qmsfra.zip
  • Type: zip
  • Size: 352.13 KB

Category:

Description

5/5 - (1 vote)

Loops in Java • Java while (Test) { Body } Body true false Test? Sum of Squares GIVENS: n (a number ≥ 1) RESULTS: sum (sum of squares from 12 to n2) sum ← sum + value * value value ← value + 1 true false sum ← 0 value ← 1 value ≤ n ? Sum of Odd Integers GIVENS: n (a number ≥ 1) RESULTS: sum (sum of odd integers up to n) sum ← sum + value value = value + 2 true false sum ← 0 value ← 1 value ≤ n ? Sum of odd integers What does this program print for n =5. Let’s execute this program on paper. Statement n value sum (User entered 5) Initial values 5 ? ? Line 7: sum = 0 0 Line 8: value = 1 1 Line 9: value <=n ? (1 <= 5) true Line 11: sum = sum + value 1 Line 12: value = value + 2 3 Line 9: value <=n ? (3 <= 5) true Line 11: sum = sum + value 4 Line 12: value = value + 2 5 Line 9: value <=n ? (5 <= 5) true Line 11: sum = sum + value 9 Line 12: value = value + 2 7 Line 9: value <=n ? (5 <= 5) false Line 15: System.out.println( …) Trace for Sum of Odd Integers Exercise 1 (on paper) • What does the program on the left print if the input user entered was: 1 5 -3 0 • (make a table as in the previous example, if uncertain) • Can you tell what the program does? Write one sentence explaining it in plane English. Exercise 2 (on paper) Analyze the following code. Is count<100 always true, always false or sometimes true and sometimes false at Point A? Answer the same question for Point B and Point C? 8 Exercise 3 (on paper) 9 A programmer who wrote a method below thinks that the method tests if a number is a prime. However there is a logical mistake. 1. Where is the mistake? 2. For what number does the method give a wrong answer? 3. Fix the mistake. for loop syntax for (; ; ) { ; ; … ; } – Perform once. – Repeat the following: • Check if the is true. If not skip the loop. • Execute the s. • Perform the . body Exercise 4 (on paper) How many starts does the following program print? 11 A. 0 B. 15 C. 45 D. 48 E. 68 Exercise 5 (on paper): Nested Loops What does the following program print? 12 Programming Exercise 1 • Design a Java program, by using do-while loop that asks the user to enter two integers. The numbers should be added and the sum displayed. Then the user is asked to enter ‘Y’ if she wishes to perform the operation again, and otherwise she enters any character other than ‘Y’. The operation is repeated, as long as the user chooses ‘Y’. 13 Programming Exercise 2 • Design a Java program that asks the user for two positive integers no greater than 15 (length and width of a rectangle). The program should then display a rectangle of ‘*’ on the screen with the corresponding dimensions. For example, if the user inputs width=5 and length=3, then the program should print: ***** ***** ***** Your program should have a method called printRectangle that takes as input width and length and draws the rectangle. 14 Programming Exercise 3: Experiment with Perfect Numbers • A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is perfect number since 6=1+2+3. The next is 28=1+2+4+7+12. There are four perfect numbers less than 10,000. Write a program that finds all these four numbers. • Once you are done. Modify your program so that it looks for all perfect numbers smaller than 35 million. What do you notice? Assuming that you computer can do billion instructions in a sec, can you figure out how long, roughly, will it take your computer to find 5th perfect number (it is 33,550,336). Is the answer roughly: couple of minutes, couple of hours, couple of days, … weeks, months, years ? • What if you wanted to wait until it prints 6th perfect number, which is 8,589,869,056? 15 Arrays: Prog. Exercise 4: Fibonacci Numbers Write a java program that creates an array containing n values (n is 2 or greater) such that A[0] = 1 A[1] = 1 A[i] = A[i-1] + A[i-2] for all i in the range 1 < i < n. – For example, the array of length 7 would be: A = 1 1 2 3 5 8 13 • Once you are done, trace your program on paper to see what the values in the array will be for n=5.