Title: Using Arrays of Data in Java
1Using Arrays of Data in Java
2News
- Read chapter 8, through 8.6
- Matt and Princes office hours will be held in
the computer lab until further notice
3Warm-up Exercise
- Write an algorithm, then a Java program, that
plays the number-guessing game with a user. - The program starts with a number between 1 and
100. The user tries to guess it. The computer
responds with - You guessed the number!
- Your guess is too big.
- Your guess is too small.
- The computer keeps count of the number of guesses.
4First, an algorithm
- Set number-of-guesses to 0
- Set secret-num to a random number between 1 and
100 - Set guessed-it to n
- Repeat until guessed-it y
- Ask user to enter a guess
- increment number-of-guesses
- if guess secret-num then
- print you guessed the number!
- set guessed-it to y
- if guess gt secret-num then print your guess is
too big - if guess lt secret-num then print your guess is
too small - Print number-of-guesses
Next, translate to Java
5- int numberOfGuesses 0
- char guessedIt 'n'
- int secretNum 50
- int guess
- while (guessedIt 'n')
-
- guess Console.readInt ("Please enter a
guess ") - numberOfGuesses numberOfGuesses 1
- if (guess secretNum)
-
- System.out.println("you guessed the
number!") - guessedIt 'y'
-
- if (guess gt secretNum)
-
- System.out.println("your guess is too
big.") -
- if (guess lt secretNum)
-
6Arrays in Java
- An array is a data structure for a list or
table - Remember this data structure from Lecture 2?
7Algorithms that use arrays, such as Sequential
Search
Given values for Name, N1, , N10 and for T1, ,
T10 Set the value of i to 1 and set the value of
Found to NO Repeat until either Found YES or i
gt 10 If Name is equal to Ni, then Print
Ti Set the value of Found to YES Else Increme
nt i If (Found NO) then Print Sorry, but
the names not in the directory Stop
8Translating to Java
- Here are examples of creating arrays
- int years new int10 // a list of 10
integers - double area new double2000 // a list of
2000 // real numbers - String name new String40 // a list of 40
strings - Here are examples of referencing elements
- firstyear years0 // warning array
elements are // numbered starting at
zero ! - lastarea area1999 // the last element in
the list of areas -
9Exercises
1. Translate the pseudocode for Sequential Search
into Java. 2. Write an algorithm that fills an
array with the users lottery picks.
Note When you translate your algorithm into
Java, dont bother with the mumbo-jumbo opening
lines Start with the variable declarations.
10The Algorithm (Fig 2.9 in the text)
Sequential Search Algorithm Given values for
Name, N1, , N10 and for T1, , T10 Set the
value of i to 1 and set the value of Found to
NO Repeat until either Found YES or i gt 10 If
Name is equal to Ni then Print Ti Set the
value of Found to YES Else Increment i If
(Found NO) then Print Sorry, but the names
not in the directory Stop
11String name Console.readString(enter
name) String N new String10 //Need
some statements to String T new String10
// fill the arrays with data int i 0 char
found n while (found n i lt 9) if
(name Ni) System.out.println(The
phone number for Ni is
Ti) found y else i i
1 If (found n) System.out.println(n
ame is not in the directory)