Using Arrays of Data in Java - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Using Arrays of Data in Java

Description:

... 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. ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 12
Provided by: BruceP53
Category:
Tags: arrays | data | java | numbered | using

less

Transcript and Presenter's Notes

Title: Using Arrays of Data in Java


1
Using Arrays of Data in Java
2
News
  • Read chapter 8, through 8.6
  • Matt and Princes office hours will be held in
    the computer lab until further notice

3
Warm-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.

4
First, 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)

6
Arrays in Java
  • An array is a data structure for a list or
    table
  • Remember this data structure from Lecture 2?

7
Algorithms 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
8
Translating 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

9
Exercises
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.
10
The 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
11
String 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)
Write a Comment
User Comments (0)
About PowerShow.com