Transcript Lab05_new

ITI 1120
Lab #5
Loops
Introduction to arrays
Contributors: G. Arbez, M. Eid, D. Inkpen, A.
Williams, D. Amyot
1
For today’s lab:
•
•
•
•
•
While statement
For loop
Do/While Loop
Arrays
Exercises
2
Sum of Squares
GIVENS:
RESULTS:
INTERMEDIATES:
HEADER:
BODY:
N
Sum
Value
Sum 
(a number  1)
(sum of squares from 12 to N2)
(current value to square)
SumOfSquares(N)
Sum  0
Value  1
Value  N ?
false
true
Sum  Sum + Value * Value
Value = Value + 1
Sum of Odd Integers
GIVENS:
RESULTS:
INTERMEDIATES:
HEADER:
BODY:
N
Sum
Value
Sum 
(a number  1)
(sum of odd integers up to N)
(current odd integer)
SumOddIntegers(N)
Sum  0
Value  1
Value  N ?
false
true
Sum  Sum + Value
Value = Value + 2
Programming Model for Tracing
SumOddIntegers(5)
Program Memory
GIVENS: N (a number  1)
RESULTS: Sum (sum of odd integers up to N)
INTERMEDIATES: Value (current odd
integer)
HEADER: Sum  SumOddIntegers(N)
BODY:
Working Memory
N
5
Sum
0 1 4 9
Value
1 3 5 7
Sum  0
Value  1
Value  N ?
false
true
Sum  Sum + Value
Value = Value + 2
CPU
Trace for Sum of Odd Integers
Statement
N
Initial values 5
Value
Sum
?
?
0
Sum  0
Value  1
1
Index <=N ? (1 <= 5) true
1
Sum  Sum + Value
Value = Value + 2
3
Value <=N ? (3 <= 5) true
4
Sum  Sum + Value
Value = Value + 2
5
Value <=N ? (5 <= 5) true
9
Sum  Sum + Value
Value = Value + 2
Value <=N ? (5 <= 5) false
7
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
(create the Lab4Ex1 class in the file Lab4Ex1.java) to
check out your answer.
count = 10;
while (count >= 0)
{
System.out.println(count);
count = count + 1;
}
7
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.
8
Exercise 3
• Develop software to play a guessing game with the user. The
program should randomly generate an integer between 1 and 10,
and then asks the user to try to guess the number. If the user
guesses incorrectly, the program indicates if the guess is high
or low and ask them to try again until the guess is correct; when
the guess is correct, the program should print a congratulatory
message and the number of guesses required to find the
number. Design the software as follows:
– Develop your algorithms with Visio and Word.
– Translate the algorithms to Java. Start with Template.java.
– The main() algorithm/method generates a random number and calls
the guessing() method. It prints the congratulation message to the
user and the number of guesses used to get the number.
– The guessing() method receives the random number plays the game
with the user. Finally, when the user gets the number, number of
guesses used to find the number is returned.
9
Exercise 4-a
• The factorial of n (written n!) is the product of the integers
between 1 and n. Thus 4! = 1*2*3*4 = 24. By definition, 0! = 1.
Factorial is not defined for negative numbers.
• Write a program that asks the user for a non-negative integer and
computes and prints the factorial of that integer. Computing the
factorial is a lot like computing the sum of integers from 1 to N,
but uses multiplication instead of addition. Don't forget about
dealing with 0.
– Develop your algorithms with Visio and Word.
– Translate the algorithms to Java. Start with Lab4Ex4a.java
and create a new class Lab4Ex4a in the file Lab4Ex4a.java
– The main algorithm/method prompts the user to enter a
positive integer, calls calcFact() to get its factorial, and prints
the results the user.
– The calcFact algorithm/method computes the factorial.
10
Exercise 4-b
• Now modify the main algorithm/method as follows:
– Check to see if the user entered a negative number.
– If a negative number is entered, print a message saying
that a nonnegative number is required and ask the user
to enter another number.
– Keep prompting the user enters a nonnegative number,
after which the factorial can be computed (by calling
the problem solving algorithm/method) and printed.
– Develop the new algorithm using Word and Viso.
– Translate to Java. Start with Lab4Ex4a.java and
create a new class Lab4Ex4b in the file Lab4Ex4b.java.
11
Exercise 5
• 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