Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.

Similar presentations


Presentation on theme: "ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot."— Presentation transcript:

1 ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot

2 Lab’s Agenda Loops Loops in Java Arrays Arrays in Java

3 Loops in Java Java while (Test) { Body } Body true false Test?

4 Sum of Squares GIVENS: N (a number  1) RESULTS: Sum (sum of squares from 1 2 to N 2 ) INTERMEDIATES: Value (current value to square) HEADER: Sum  SumOfSquares(N) BODY: Sum  Sum + Value * Value Value = Value + 1 true false Sum  0 Value  1 Value  N ?

5 Sum of Odd Integers GIVENS: N (a number  1) RESULTS: Sum (sum of odd integers up to N) INTERMEDIATES: Value (current odd integer) HEADER:Sum  SumOddIntegers(N) BODY: Sum  Sum + Value Value = Value + 2 true false Sum  0 Value  1 Value  N ?

6 Programming Model for Tracing SumOddIntegers(5) N Sum Value 5 0 1 4 9 1 3 5 7 GIVENS: N (a number  1) RESULTS: Sum (sum of odd integers up to N) INTERMEDIATES: Value (current odd integer) HEADER: Sum  SumOddIntegers(N) BODY:

7 StatementNValueSum Initial values5?? Sum  00 Value  11 Index <=N ? (1 <= 5) true Sum  Sum + Value1 Value = Value + 23 Value <=N ? (3 <= 5) true Sum  Sum + Value4 Value = Value + 25 Value <=N ? (5 <= 5) true Sum  Sum + Value9 Value = Value + 27 Value <=N ? (5 <= 5) false Trace for Sum of Odd Integers

8 Exercise 1 The code below is supposed to print the integers from 10 to 1 backwards. –You need to find 2 logical errors in the code. –Take the time to draw an algorithm body to follow the logic and find the errors BEFORE any coding. –Correct the code and insert it into a main method to check out your answer. count = 10; while (count >= 0) { System.out.println(count); count = count + 1; } 8

9 More loop examples (to practice by yourself later) 1.Write a algorithm that calculates the sum of the first N terms of the series 1^2 + 2^2 + 3^2 + 4^2 +... 2.Write a algorithm that returns the sum of odd integers from 1 up to no larger than N 1 + 3 + 5 + … See if you can do this without a branch!

10 Loops in Java Implement in Java -The sum of squares -The sum of odd integers

11 Exercise 2 Develop an algorithm that asks the user to enter two integers. The numbers should be added and the sum displayed. Then the user is asked if she/he wishes to perform the operation again. If so, the operation is repeated, otherwise it should terminate. Translate the algorithm to a Java method. 11

12 Exercise 3 Design a 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=3 and length=5, then the program should print: *** Design the software using the two methods model. 12

13 Array Example: Fibonacci Numbers Create an array containing N values (N is 1 or greater) such that A[0] = 1 A[1] = 1 A[I] = A[I-1] + A[I-2] for all I greater than 1 < I < N. –For example, the array of length 7 would be: A = 1 1 2 3 5 8 13

14 Fibonacci Algorithm GIVENS: N (a number  1) RESULTS: A (an array of Fibonacci numbers) INTERMEDIATES: I (index for the array) HEADER: A  Fibonacci(N) BODY: I  I + 1 A[I]  A[I-1]+A[I-2] true false A[0]  1 A[1]  1 I  2 I < N ? Fibonacci Rule: A[0] = 1 A[1] = 1 A[I] = A[I-1] + A[I-2]

15 Trace for N=5 Statement NIA initial values5?? 1. A[0]  1 ? (no array yet)

16 Array creation bug… Any algorithm that creates an array must call an array-creation algorithm A  MakeNewArray( size ) This creates an array with "size" positions. In our example, it must be done as the very first statement.

17 Fibonacci Algorithm, version 2 GIVENS: N (a number  1) RESULTS: A (an array of Fibonacci numbers) INTERMEDIATES: I (index for the array) HEADER: A  Fibonacci(N) BODY: I  I + 1 A[I]  A[I-1]+A[I-2] true false A  MakeNewArray(5) A[0]  1 A[1]  1 I  2 I < N ? Fibonacci Rule: A[0] = 1 A[1] = 1 A[I] = A[I-1] + A[I-2]

18 Trace for N=5 Statement NIA initial values5?? ? ? ? ? 1. A  MakeNewArray(5) 2. A[0]  1 1 ? ? ? ? 3. A[1]  1 1 1 ? ? ? 4. I  2 2 5. I < N ? : true 6. I  I + 1 3 7. A[I]  A[I-1]+A[I-2] 1 1 ? uh oh!

19 Fibonacci Algorithm, version 3 GIVENS: N (a number  1) RESULTS: A (an array of Fibonacci numbers) INTERMEDIATES: I (index for the array) HEADER: A  Fibonacci(N) BODY: A[I]  A[I-1]+A[I-2] I  I + 1 true false A  MakeNewArray(5) A[0]  1 A[1]  1 I  2 I < N ? Fibonacci Rule: A[0] = 1 A[1] = 1 A[I] = A[I-1] + A[I-2]

20 Trace for N=5 Statement NIA initial values5?? 1. A  MakeNewArray(5) ? ? ? ? ? 2. A[0]  1 1 ? ? ? ? 3. A[1]  1 1 1 ? ? ? 4. I  2 2 5. I < N ? : true 6. A[I]  A[I-1]+A[I-2] 1 1 2 ? ? 7. I  I + 1 3 [continued next page]

21 Trace for N=5 StatementNIA [continued from previous page] 5. I < N ? : true 6. A[I]  A[I-1]+A[I-2] 1 1 2 3 ? 7. I  I + 1 4 5. I < N ? : true 6. A[I]  A[I-1]+A[I-2] 1 1 2 3 5 7. I  I + 1 5 5. I < N ? : false

22 Arrays in Java Arrays must be declared with [] after the type of the array values, and before the name of the variable. double[] d; Creating the array: D  MakeNewArray( Length ) d = new double[length];

23 Array creation Until the array has been created, the value of the array is the special value null. –Attempting to use an array before new will result in a “null pointer exception” that will crash a program. –After the array has been created, the values are still empty, and the values must be filled in separately.

24 Array indices Once you have created an array, the type and length are fixed. Example: double[] d; d = new double[4]; –The indices that can be used for this array are 0, 1, 2, 3 –Attempting to use a negative index value, or a value that is 4 or greater, will result in an “Array index out of bounds exception” and the program will crash.

25 Array lengths You can ask an array for its length: double[] d; int len; d = new double[4]; len = d.length; // len is now 4 Notes: –Do not use [] or () when asking for the length –Attempting to ask for the length of an array before creating it will result in a “null pointer exception” that will crash the program. –The length is NOT the maximum index; it is one larger

26 Reading arrays from the keyboard Use the provided ITI1120 class (get it from the virtual campus, Lab area): ITI1120.readIntLine() Reads an array of integers When entering the values, be sure to leave at least one space between them. The result is of type int[] You should ask for the length of the array that is returned, to find out its size. ITI1120.readDoubleLine() Similar to readIntLine (), except for double values ITI1120.readCharLine() Reads an array of character values Includes ALL characters, including spaces Result is of type char[]

27 Example 1 Array: Calculate the Average of the elements in the array Write an algorithm that will calculate the average of an array X of numbers, of length N (in the main method for now).

28 Example 1 Array Implement your algorithm for the average of array elements in Java.


Download ppt "ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot."

Similar presentations


Ads by Google