Homework 8 - PowerPoint PPT Presentation

About This Presentation
Title:

Homework 8

Description:

( MT sections ) ( WTh sections ) at midnight. Sun., 10/28. Mon., 10/29. Problems ... quip = new String[len]; // create data variables. for (int i=0 ; i len ; i) ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 62
Provided by: zachd
Category:
Tags: homework | mt

less

Transcript and Presenter's Notes

Title: Homework 8


1
Homework 8
Sun., 10/28
( MT sections )
  • Due

at midnight
Mon., 10/29
( WTh sections )
  • Problems

http//www.cs.hmc.edu/courses/2001/fall/cs5/week_0
8/homework.html
Reading is under week 7, however.
  • Tutors available

Saturday afternoons Lac Lab (1-5) Sunday
afternoons Lac Lab and A/C (1-5) Sunday
evenings Lac Lab and A/C (7-10) Monday
evenings Lac Lab and A/C (8-12)
names and hours linked from the CS 5 home page
2
Problem 1
What is it supposed to do ?
Get the number of stock prices from the user
Initial Input
Create an array of the appropriate number of
elements.
Get each stock price from the user into the array.
1 Display prices 2 Compute average of prices 3
Compute variance of prices 4 Display index and
value of lowest price 5 Display index and value
of highest price 6 Your TTS investment
strategy 9 Quit
Menu
3
Problem 1
How do we do this ? Pseudocode...
4
Methods
public static void main(String args) //
first, create array stocks and fill it with
values double stocks // etc. double total
sumArray(stocks) public static double
sumArray(double arr) double sum 0.0
for (int i0 iltarr.length i) sum
sum arri return sum
5
7 Methods you will need
public static void displayMenu() public static
void displayPrices(double arr) double
sumArray(double arr) double averageArray(double
arr) double variance(double arr) int
indexOfSmallest(double arr) int
indexOfLargest(double arr)
How do we know this ?
6
Method Notes
int indexOfSmallest(double arr)
7
double smallest(double arr)
8
Problem 2
on
off
on
off
off
on
A starting row of lights
0
1
2
3
4
5
Each turn, a light is selected -- It and
its neighbors switch states.
2 is selected
0
1
2
3
4
5
2
5
0
1
3
4
Goal get all the lights off
9
on
off
on
off
off
on
Problem 2
2
5
0
1
3
4
Pseudocode
// input the desired of lights // create the
array and fill randomly // print the lights //
let the user select a light // switch lights as
appropriate // check if the user has won
10
What is a light ?
on
off
on
off
off
on
How should we represent these lights in Java ?
0
1
2
3
4
5
11
Lights Out -- Printing
// draw the current set of lights
lights should be separated with vertical bars
on
off
on
off
off
on
on lights should be 4x4 blocks of stars
0
1
2
3
4
5



0 1 2 3 4 5 6 7

off lights should be 4x4 blocks of spaces
OK to display all light numbers up to 15
print light numbers close to the center of each
light
12
Lights Out -- Printing


0
1 2 3 4 5
13
Lights Out -- Printing
// draw the current set of lights
lights should be separated with vertical bars
on
off
on
off
off
on
on lights should be 4x4 blocks of stars
0
1
2
3
4
5



0 1 2 3 4 5 6 7

off lights should be 4x4 blocks of spaces
OK to display all light numbers up to 15
print light numbers close to the center of each
light
14
Lights Out -- Printing


0
1 2 3 4 5
15
Lights Out -- Selecting
What lights are valid for the user to select?
on
off
on
off
off
on
2
5
0
1
3
4
16
Lights Out -- Switching
How do we switch the necessary lights on or off?
on
off
on
off
off
on
2
5
0
1
3
4
17
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
18
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
String quip
19
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
array reference
quip1
quip3
quip0
quip2
quip4
String quip
20
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
quip1
quip3
quip0
quip2
quip4
leaves
after
leaves
fall
fall
String quip
i is 0
i is 2
i is 4
i is 1
i is 3
21
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
quip1
quip3
quip0
quip2
quip4
leaves
after
leaves
fall
fall
String quip
i is 4
i is 3
i is 1
i is 0
i is 2
fall leaves after leaves fall
22
This week in CS 5
  • Getting data together -- arrays !

due Sunday, 10/28 at midnight
M/T sections
  • HW 8 (2 problems)

due Monday, 10/29 at midnight
W/Th sections
Recitation for HW8 will be Friday 10/26
No recitation this Friday, 10/19
Reading Week 7s online notes
See HW for notes on aligning things correctly, as
well as deleting files.
  • Indenting will count on HW 8 (and after)!

23
Syllable counting
public static int numSyllables(String w)
int numSyls 0 int len w.length()
if ( isVowel(w.charAt(0)) ) // an
initial vowel ? numSyls for (int
i1 iltw.length() i)
// vowel preceded by a consonant if (
isVowel(w.charAt(i)) !isVowel(w.charAt(i-1)) )
numSyls //
final e preceded by a consonant if (
w.charAt(len-1) 'e len gt 2
!isVowel(w.charAt(len-2))
) --numSyls if (numSyls lt 1) //
every word has at least 1 syllable numSyls
1 return numSyls
24
A puzzle...
Take in a number of words and print them out in
reverse order. (To be specific, suppose its 5
words.)
25
A puzzle...
Take in a number of words and print them out in
reverse order. (To be specific, suppose its 5
words.)
String s1 H.in.nextWord() String s2
H.in.nextWord() String s3 H.in.nextWord() Stri
ng s4 H.in.nextWord() String s5
H.in.nextWord() H.out.print( s5
) H.out.print( s4 ) H.out.print( s3
) H.out.print( s2 ) H.out.print( s1
\n )
Not a very flexible solution...
26
Arrays - lists of data items
declares a String array named quip
String quip
declares five Strings named quip0quip4
quip new String5
for (int i0 ilt5 i) quipi
H.in.nextWord()
loop through the array
index
27
Arrays in code
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
// create an empty array quip new
Stringlen // create array elements for
(int i0 iltlen i) quipi
H.in.nextWord() // input each element //
now print them out in reverse order
28
Sentence palindromes
fall leaves after leaves fall
bores are people that say that people are bores
First Ladies rule the state and state the rule,
Ladies First!
you can cage a swallow, cant you, but you cant
swallow a cage, can you?
29
Strings
Arrays
vs
double, int, String, boolean, char, (any type)
char
Element types
double arr arr new double100
Example Declaration
String s
arri
s.charAt(i)
The ith element
Length
arr.length
s.length()
Range
from 0 up to length-1
Be careful not to go out of bounds!
Warning
java.lang.StringIndexOutOfBoundsException -1
java.lang.ArrayIndexOutOfBoundsException -1
30
T. T. Securities
Input stock prices for a number of days in a
row, and then analyze that data .
Menu
1 Display prices 2 Compute average of prices 3
Compute standard deviation of prices 4 Display
index and value of lowest price 5 Display index
and value of highest price 6 Your TTS investment
strategy 9 Quit Which choice would you like?
31
Arrays and Methods
public static double sumArray(double arr)
32
Using sumArray
public static void main(String args) //
prompt for and input nStocks double stocks
new doublenStocks // input and assign
each stocksi double stockSum
sumArray(stocks) H.out.println(The sum is
stockSum)
90.0
60.0
42.0
70.0
10.0
75.0
double stocks
public static double sumArray(double arr)
// see previous page return sum
double arr
33
Using sumArray
public static void main(String args) //
prompt for and input nStocks double stocks
new doublenStocks // input and assign
each stocksi double stockSum
sumArray(stocks) H.out.println(The sum is
stockSum)
90.0
60.0
42.0
70.0
10.0
75.0
double stocks
2 references referring to the same list of data
public static double sumArray(double arr)
// see previous page return sum
double arr
34
Array Searching
public static double findMax(double arr)
35
T. T. Securities
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day 0 Price is 90.0 Day 1 Price is 10.0 Day
2 Price is 60.0 Day 3 Price is 42.0 Day 4
Price is 75.0 Day 5 Price is 70.0
36
Lights Out !
on
on
off
off
off
on
A starting row of lights
0
1
2
3
4
5
Each turn, a light is selected -- It and
its neighbors switch states.
2 is selected
0
1
2
3
4
5
2
5
0
1
3
4
Goal get all the lights off
37
Lights Out !
Features of the game
// allow the user to set the // number of
lights from 3 to 15 // start each light randomly
on or off // draw the current set of lights //
allow the user to select a light // only allow
valid lights ! // update the set of lights and
repeat
38
Lights Out !
// draw the current set of lights
lights should be separated with vertical bars
0
1
2
3
4
5
on lights should be 4x4 blocks of stars



0 1 2 3
4 5 6 7
off lights should be 4x4 blocks of spaces
may display all light numbers up to 15
print light numbers close to the center of each
light
39
Lights Out !
// allow the user to select a light // only
allow valid lights !
40
Summary
To declare an array
double stocks String quip int grades
To declare an arrays individual elements
stocks new doublenStocks quip new
StringnWords grades new int42
To loop through an array
for (int i0 iltstocks.length i) do
something with stocksi in here ...
41
Drawing room
?
(xi - xav)2
i
N
0
1
2
3
4
5
0
1
2
3
4
5
0
1
2
3
4
5
42
Printed version following this slide
43
This week in CS 5
  • Putting data together with arrays

due Sunday, 10/28 at midnight
M/T sections
  • HW 8 (2 problems)

due Monday, 10/29 at midnight
W/Th sections
Recitation for HW8 will be Friday 10/26
No recitation this Friday, 10/19
-- Reading Week 7s online notes
See HW for notes on aligning things correctly, as
well as deleting files.
  • Indenting will count on HW 8 (and after)!

if ( isVowel(w.charAt(0)) ) numSyls
44
Arrays - lists of data items
String quip
- declares a String array named quip
String quip
quip new String5
- declares five Strings named quip0quip4
quip1
quip3
quip0
quip2
quip4
String quip
leaves
after
leaves
fall
fall
- looping through the array
for (int i0 ilt5 i) quipi
H.in.nextWord()
45
Arrays in code
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
// create array variable quip new
Stringlen // create data variables for
(int i0 iltlen i) quipi
H.in.nextWord() // input each word // now
print them out in reverse order for (int
ilen-1 igt0 --i) H.out.print( quipi
)
46
Arrays in pictures
5
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
quip new Stringlen for
(int i0 iltlen i) quipi
H.in.nextWord() for (int ilen-1 igt0
--i) H.out.print( quipi )
int len
quip1
quip3
quip0
quip2
quip4
leaves
after
leaves
fall
fall
String quip
47
Arrays vs. Strings
double, int, String, boolean, char, (any type)
char
Element types
double arr arr new double100
Example Declaration
String s
arri
s.charAt(i)
The ith element
Length
arr.length
s.length()
Range
from 0 up to length-1
Be careful not to go out of bounds!
Warnings
java.lang.StringIndexOutOfBoundsException -1
java.lang.ArrayIndexOutOfBoundsException -1
48
T. T. Securities
Input stock prices for a number of days in a
row, and then analyze that data .
Menu
1 Display prices 2 Compute average of prices 3
Compute standard deviation of prices 4 Display
index and value of lowest price 5 Display index
and value of highest price 6 Your TTS investment
strategy 9 Quit Which choice would you like?
49
Arrays and Methods
public static double sumArray(double arr)
50
Using sumArray
public static void main(String args) //
prompt for and input nStocks double stocks
new doublenStocks // input and assign
each stocksi double stockSum
sumArray(stocks) H.out.println(The sum is
stockSum)
90.0
60.0
42.0
70.0
10.0
75.0
double stocks
stocks1
stocks2
stocks3
stocks4
stocks5
stocks0
public static double sumArray(double arr)
// see previous page return sum
double arr
51
Array Searching
public static double findMax(double arr)
52
T. T. Securities
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day 0 Price is 90.0 Day 1 Price is 10.0 Day
2 Price is 60.0 Day 3 Price is 42.0 Day 4
Price is 75.0 Day 5 Price is 70.0
53
Lights Out !
Features of the game
// allow the user to set the // number of
lights from 3 to 15 // start each light randomly
on or off // draw the current set of lights //
allow the user to select a light // only allow
valid lights ! // update the set of lights and
repeat
54
Summary
To declare an array
double stocks String quip int grades
To declare an arrays individual elements
stocks new doublenStocks quip new
StringnWords grades new int42
To loop through an array
for (int i0 iltstocks.length i) do
something with stocksi in here ...
55
A puzzle...
Take in a number of words and print them out in
reverse order. (To be specific, suppose its 5
words.)
String s1 H.in.nextWord() String s2
H.in.nextWord() String s3 H.in.nextWord() Stri
ng s4 H.in.nextWord() String s5
H.in.nextWord() H.out.print( s1
) H.out.print( s2 ) H.out.print( s3
) H.out.print( s4 ) H.out.print( s5
\n )
Not a very flexible solution...
56
Arrays - lists of data items
declares a String array named quip
String quip
declares five Strings named quip0quip4
quip new String5
for (int i0 ilt5 i) quipi
H.in.nextWord()
loop through the array
index
57
Arrays in code
H.out.println(Type the number of words ) int
len H.in.nextInt() String quip
// create an empty array quip new
Stringlen // create array elements for
(int i0 iltlen i) quipi
H.in.nextWord() // input each element //
now print them out in reverse order for (int
ilen-1 igt0 --i) H.out.print( quipi
)
58
Strings
Arrays
vs
double, int, String, boolean, char, (any type)
char
Element types
double arr arr new double100
Example Declaration
String s
arri
s.charAt(i)
The ith element
Length
arr.length
s.length()
Range
from 0 up to length-1
Be careful not to go out of bounds!
Warning
java.lang.StringIndexOutOfBoundsException -1
java.lang.ArrayIndexOutOfBoundsException -1
59
Methods
displayMenu displayPrices sumArray averageArray s
tdDevArray indexOfSmallest indexOfLargest
Day 0 Price is 95.0 Day 1 Price is 15.0 Day
2 Price is 90.0 Day 3 Price is 10.0 Day 4
Price is 60.0 Day 5 Price is 42.0 Day 6
Price is 75.0 Day 7 Price is 70.0
Its not always with the minimum or maximum !
60
T. T. Securities
Find the most profitable strategy for buying and
selling the stock among the prices in the array...
Day 0 Price is 90.0 Day 1 Price is 10.0 Day
2 Price is 60.0 Day 3 Price is 42.0 Day 4
Price is 75.0 Day 5 Price is 70.0
61
Using sumArray
public static void main(String args) //
prompt for and input nStocks double stocks
new doublenStocks // input and assign
each stocksi double stockSum
sumArray(stocks) H.out.println(The sum is
stockSum)
90.0
60.0
42.0
70.0
10.0
75.0
double stocks
public static double sumArray(double arr)
// see previous page return sum
double arr
Write a Comment
User Comments (0)
About PowerShow.com