61
Homework 8 • Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however. http://www.cs.hmc.edu/courses/2001/fall/cs5/week_08/homework.html • 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

Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Embed Size (px)

Citation preview

Page 1: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Homework 8

• Due ( MT sections )

( WTh sections )

at midnightSun., 10/28

Mon., 10/29

• Problems

Reading is under week 7, however.

http://www.cs.hmc.edu/courses/2001/fall/cs5/week_08/homework.html

• Tutors availableSaturday 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

Page 2: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Problem 1

1 Display prices2 Compute average of prices3 Compute variance of prices4 Display index and value of lowest price5 Display index and value of highest price6 Your TTS investment strategy

9 Quit

Menu

What is it supposed to do ?

Initial Input Get the number of stock prices from the user

Get each stock price from the user into the array.

Create an array of the appropriate number of elements.

Page 3: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Problem 1

How do we do this ? Pseudocode...

Page 4: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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 i=0 ; i<arr.length ; ++i) { sum = sum + arr[i]; } return sum;}

Page 5: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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 ?

Page 6: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Method Notes

int indexOfSmallest(double[] arr)

Page 7: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

double smallest(double[] arr)

Page 8: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Problem 2

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

A starting row of lights

2 is selected Each turn, a light is selected -- It and its neighbors switch states.

Goal: get all the lights off…

on off on off off on

Page 9: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Problem 20 1 2 3 4 5

on off on off off on

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…

Page 10: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

What is a light ?

0 1 2 3 4 5

How should we represent these lights in Java ?

on off on off off on

Page 11: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out -- Printing

// draw the current set of lights

|****| |****| | |****| |****| |****| | |****| |****| |****| | |****| |****| |****| | |****|

0 1 2 3 4 5 6 7

lights should be separated with vertical bars

OK to display all light numbers up to 15

print light numbers close to the center of each light

“off” lights should be 4x4 blocks of spaces

“on” lights should be 4x4 blocks of stars

0 1 2 3 4 5

on off on off off on

Page 12: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out -- Printing

|****| |****| | |****| |****| |****| | |****| |****| |****| | |****| |****| |****| | |****|

0 1 2 3 4 5

Page 13: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

|****| |****| | |****| |****| |****| | |****| |****| |****| | |****| |****| |****| | |****|

0 1 2 3 4 5 6 7

Lights Out -- Printing

// draw the current set of lightslights should be separated with vertical bars

OK to display all light numbers up to 15

print light numbers close to the center of each light

“off” lights should be 4x4 blocks of spaces

“on” lights should be 4x4 blocks of stars

0 1 2 3 4 5

on off on off off on

Page 14: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out -- Printing

|****| |****| | |****| |****| |****| | |****| |****| |****| | |****| |****| |****| | |****|

0 1 2 3 4 5

Page 15: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out -- Selecting

0 1 2 3 4 5

What lights are valid for the user to select?

on off on off off on

Page 16: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out -- Switching

0 1 2 3 4 5

How do we switch the necessary lights on or off?

on off on off off on

Page 17: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

5

Page 18: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

5

String[] quip

Page 19: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

String[] quip

quip[0]quip[1]

quip[2] quip[4]quip[3]

5

array reference

Page 20: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

String[] quip

quip[0]quip[1]

quip[2] quip[4]quip[3]

5

fall

leaves

after

leaves

fall

i is 0i is 1

i is 2i is 3

i is 4

Page 21: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

String[] quip

quip[0]quip[1]

quip[2] quip[4]quip[3]

5

fall

leaves

after

leaves

fall

i is 4 i is 3 i is 2 i is 1 i is 0

fall leaves after leaves fall

Page 22: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

This week in CS 5

• HW 8 (2 problems)M/T sections

W/Th sections

due Sunday, 10/28 at midnight

due Monday, 10/29 at midnight

Recitation for HW8 will be Friday 10/26

• Getting data together -- arrays !

No recitation this Friday, 10/19

Reading: Week 7’s online notes

• Indenting will count on HW 8 (and after)!See HW for notes on

aligning things correctly, as well as deleting files.

Page 23: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

public static int numSyllables(String w){ int numSyls = 0; int len = w.length();

if ( isVowel(w.charAt(0)) ) // an initial vowel ? ++numSyls;

for (int i=1 ; i<w.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 >= 2 && !isVowel(w.charAt(len-2)) ) --numSyls;

if (numSyls < 1) // every word has at least 1 syllable numSyls = 1;

return numSyls;}

Syllable counting

Page 24: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Take in a number of words and print them out in reverse order. (To be specific, suppose it’s 5 words.)

A puzzle...

Page 25: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Take in a number of words and print them out in reverse order. (To be specific, suppose it’s 5 words.)

A puzzle...

String s1 = H.in.nextWord();String s2 = H.in.nextWord();String s3 = H.in.nextWord();String 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...

Page 26: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays - lists of data items

String[] quip;

quip = new String[5];

for (int i=0 ; i<5 ; ++i){ quip[i] = H.in.nextWord();}

declares a String array named quip

declares five Strings named quip[0]…quip[4]

loop through the array

index

Page 27: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays in code

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; // create an empty arrayquip = new String[len]; // create array elements

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord(); // input each element}

// now print them out in reverse order…

Page 28: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Sentence palindromes

bores are people that say that people are bores

fall leaves after leaves fall

you can cage a swallow, can’t you, but you can’t swallow a cage, can you?

First Ladies rule the state and state the rule, “Ladies First!”

Page 29: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Strings

Be careful not to go out of bounds!

Element typesdouble, int, String,boolean, char, (any type) … char

The ith element

Example Declaration

Length

arr[i] s.charAt(i)

double[] arr;arr = new double[100];

String s;

java.lang.StringIndexOutOfBoundsException: -1

java.lang.ArrayIndexOutOfBoundsException: -1

arr.length s.length()

Warning

Range from 0 up to length-1

vsArrays

Page 30: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

T. T. Securities

Input stock prices for a number of days in a row, and then analyze that data… .

1 Display prices2 Compute average of prices3 Compute standard deviation of prices4 Display index and value of lowest price5 Display index and value of highest price6 Your TTS investment strategy

9 Quit

Which choice would you like?

Menu:

Page 31: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays and Methods

public static double sumArray(double[] arr)

Page 32: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Using sumArray

public static void main(String[] args){ // prompt for and input nStocks double[] stocks = new double[nStocks]; // input and assign each stocks[i] double stockSum = sumArray(stocks); H.out.println(“The sum is ” + stockSum);}

public static double sumArray(double[] arr){ // see previous page … return sum;}

double[] stocks

90.0 10.0 60.0 42.0 75.0 70.0

double[] arr

Page 33: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Using sumArray

public static void main(String[] args){ // prompt for and input nStocks double[] stocks = new double[nStocks]; // input and assign each stocks[i] double stockSum = sumArray(stocks); H.out.println(“The sum is ” + stockSum);}

public static double sumArray(double[] arr){ // see previous page … return sum;}

double[] stocks

90.0 10.0 60.0 42.0 75.0 70.0

double[] arr

2 references referring to the same list of data

Page 34: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Array Searching

public static double findMax(double[] arr)

Page 35: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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.0Day 1 Price is 10.0Day 2 Price is 60.0Day 3 Price is 42.0Day 4 Price is 75.0Day 5 Price is 70.0

Page 36: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out !

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

A starting row of lights

2 is selected Each turn, a light is selected -- It and its neighbors switch states.

Goal: get all the lights off…

on on off off off on

Page 37: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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

Page 38: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out !

// draw the current set of lights

0 1 2 3 4 5

| |****|****|****| |****| | |****|****|****| |****| | |****|****|****| |****| | |****|****|****| |****|

0 1 2 3 4 5 6 7

lights should be separated with vertical bars

may display all light numbers up to 15

print light numbers close to the center of each light

“off” lights should be 4x4 blocks of spaces

“on” lights should be 4x4 blocks of stars

Page 39: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Lights Out !

// allow the user to select a light

// only allow valid lights !

Page 40: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Summary

double[] stocks;String[] quip;int[] grades;

To declare an array:

To loop through an array:

stocks = new double[nStocks];quip = new String[nWords];grades = new int[42];

for (int i=0 ; i<stocks.length ; ++i){ do something with stocks[i] in here ...}

To declare an array’s individual elements:

Page 41: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

(x[i] - xav)2

Ni

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

Drawing room

Page 42: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Printed version following this slide

Page 43: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

This week in CS 5

• HW 8 (2 problems)M/T sections

W/Th sections

due Sunday, 10/28 at midnight

due Monday, 10/29 at midnight

Recitation for HW8 will be Friday 10/26

• Putting data together with arrays

No recitation this Friday, 10/19

-- Reading: Week 7’s online notes

• Indenting will count on HW 8 (and after)!See HW for notes on

aligning things correctly, as well as deleting files.

if ( isVowel(w.charAt(0)) ){ ++numSyls;}

Page 44: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays - lists of data items

String[] quip;

quip = new String[5];

for (int i=0 ; i<5 ; ++i){ quip[i] = H.in.nextWord();}

String[] quip

String[] quip

quip[0]quip[1]

quip[2] quip[4]quip[3]

fall

leaves

after

leaves

fall

- declares a String array named quip

- declares five Strings named quip[0]…quip[4]

- looping through the array

Page 45: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays in code

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; // create array variablequip = new String[len]; // create data variables

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord(); // input each word}

// now print them out in reverse order…

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Page 46: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; quip = new String[len];

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord();}

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Arrays in pictures

int len

String[] quip

quip[0]quip[1]

quip[2] quip[4]quip[3]

5

fall

leaves

after

leaves

fall

Page 47: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays vs. Strings

Be careful not to go out of bounds!

Element typesdouble, int, String,boolean, char, (any type) … char

The ith element

Example Declaration

Length

arr[i] s.charAt(i)

double[] arr;arr = new double[100];

String s;

java.lang.StringIndexOutOfBoundsException: -1

java.lang.ArrayIndexOutOfBoundsException: -1

arr.length s.length()

Warnings

Range from 0 up to length-1

Page 48: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

T. T. Securities

Input stock prices for a number of days in a row, and then analyze that data… .

1 Display prices2 Compute average of prices3 Compute standard deviation of prices4 Display index and value of lowest price5 Display index and value of highest price6 Your TTS investment strategy

9 Quit

Which choice would you like?

Menu:

Page 49: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays and Methods

public static double sumArray(double[] arr)

Page 50: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Using sumArray

public static void main(String[] args){ // prompt for and input nStocks double[] stocks = new double[nStocks]; // input and assign each stocks[i] double stockSum = sumArray(stocks); H.out.println(“The sum is ” + stockSum);}

public static double sumArray(double[] arr){ // see previous page … return sum;}

double[] stocks

90.0 10.0 60.0 42.0 75.0 70.0

double[] arr

stocks[0] stocks[1] stocks[2] stocks[3] stocks[4] stocks[5]

Page 51: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Array Searching

public static double findMax(double[] arr)

Page 52: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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.0Day 1 Price is 10.0Day 2 Price is 60.0Day 3 Price is 42.0Day 4 Price is 75.0Day 5 Price is 70.0

Page 53: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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

Page 54: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Summary

double[] stocks;String[] quip;int[] grades;

To declare an array:

To loop through an array:

stocks = new double[nStocks];quip = new String[nWords];grades = new int[42];

for (int i=0 ; i<stocks.length ; ++i){ do something with stocks[i] in here ...}

To declare an array’s individual elements:

Page 55: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Take in a number of words and print them out in reverse order. (To be specific, suppose it’s 5 words.)

A puzzle...

String s1 = H.in.nextWord();String s2 = H.in.nextWord();String s3 = H.in.nextWord();String 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...

Page 56: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays - lists of data items

String[] quip;

quip = new String[5];

for (int i=0 ; i<5 ; ++i){ quip[i] = H.in.nextWord();}

declares a String array named quip

declares five Strings named quip[0]…quip[4]

loop through the array

index

Page 57: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Arrays in code

H.out.println(“Type the number of words: ”);int len = H.in.nextInt();

String[] quip; // create an empty arrayquip = new String[len]; // create array elements

for (int i=0 ; i<len ; ++i){ quip[i] = H.in.nextWord(); // input each element}

// now print them out in reverse order…

for (int i=len-1 ; i>=0 ; --i){ H.out.print( quip[i] + “ ” );}

Page 58: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Strings

Be careful not to go out of bounds!

Element typesdouble, int, String,boolean, char, (any type) … char

The ith element

Example Declaration

Length

arr[i] s.charAt(i)

double[] arr;arr = new double[100];

String s;

java.lang.StringIndexOutOfBoundsException: -1

java.lang.ArrayIndexOutOfBoundsException: -1

arr.length s.length()

Warning

Range from 0 up to length-1

vsArrays

Page 59: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Day 0 Price is 95.0Day 1 Price is 15.0Day 2 Price is 90.0Day 3 Price is 10.0Day 4 Price is 60.0Day 5 Price is 42.0Day 6 Price is 75.0Day 7 Price is 70.0

It’s not always with the minimum or maximum !

Methods

displayMenu displayPrices

sumArrayaverageArraystdDevArray

indexOfSmallestindexOfLargest

Page 60: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

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.0Day 1 Price is 10.0Day 2 Price is 60.0Day 3 Price is 42.0Day 4 Price is 75.0Day 5 Price is 70.0

Page 61: Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however

Using sumArray

public static void main(String[] args){ // prompt for and input nStocks double[] stocks = new double[nStocks]; // input and assign each stocks[i] double stockSum = sumArray(stocks); H.out.println(“The sum is ” + stockSum);}

public static double sumArray(double[] arr){ // see previous page … return sum;}

double[] stocks

90.0 10.0 60.0 42.0 75.0 70.0

double[] arr