Homework 8 Due Sun 1028 MT sections Mon

  • Slides: 61
Download presentation
Homework 8 • Due Sun. , 10/28 ( MT sections ) Mon. , 10/29

Homework 8 • Due Sun. , 10/28 ( MT sections ) Mon. , 10/29 ( WTh sections ) at midnight • Problems http: //www. cs. hmc. edu/courses/2001/fall/cs 5/week_08/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

Problem 1 What is it supposed to do ? Initial Input Get the number

Problem 1 What is it supposed to do ? Initial Input Get the number of stock prices from the user Create an array of the appropriate number of elements. Get each stock price from the user into the array. Menu 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

Problem 1 How do we do this ? Pseudocode. . .

Problem 1 How do we do this ? Pseudocode. . .

Methods public static void main(String[] args) { // first, create array stocks and fill

Methods public static void main(String[] args) { // first, create array stocks and fill it with values double[] stocks; // etc. double total = sum. Array(stocks); } public static double sum. Array(double[] arr) { double sum = 0. 0; for (int i=0 ; i<arr. length ; ++i) { sum = sum + arr[i]; } return sum; }

7 Methods you will need: public static void display. Menu() public static void display.

7 Methods you will need: public static void display. Menu() public static void display. Prices(double[] arr) double sum. Array(double[] arr) double average. Array(double[] arr) double variance(double[] arr) int index. Of. Smallest(double[] arr) int index. Of. Largest(double[] arr) How do we know this ?

Method Notes int index. Of. Smallest(double[] arr)

Method Notes int index. Of. Smallest(double[] arr)

double smallest(double[] arr)

double smallest(double[] arr)

Problem 2 on off off on A starting row of lights 0 1 2

Problem 2 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 Goal: get all the lights off…

on off off on Problem 2 0 1 2 3 4 5 Pseudocode //

on off off on Problem 2 0 1 2 3 4 5 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…

What is a light ? on off off on How should we represent these

What is a light ? on off off on How should we represent these lights in Java ? 0 1 2 3 4 5

Lights Out -- Printing // draw the current set of lights “on” lights should

Lights Out -- Printing // draw the current set of lights “on” lights should be 4 x 4 blocks of stars on off off on 0 1 2 3 4 5 |****| 0 “off” lights should be 4 x 4 blocks of spaces |****| 1 2 | | 3 print light numbers close to the center of each lights should be separated with vertical bars |****| 4 5 6 7 OK to display all light numbers up to 15

Lights Out -- Printing |****| 0 |****| 1 2 | | 3 |****| 4

Lights Out -- Printing |****| 0 |****| 1 2 | | 3 |****| 4 5

Lights Out -- Printing // draw the current set of lights “on” lights should

Lights Out -- Printing // draw the current set of lights “on” lights should be 4 x 4 blocks of stars on off off on 0 1 2 3 4 5 |****| 0 “off” lights should be 4 x 4 blocks of spaces |****| 1 2 | | 3 print light numbers close to the center of each lights should be separated with vertical bars |****| 4 5 6 7 OK to display all light numbers up to 15

Lights Out -- Printing |****| 0 |****| 1 2 | | 3 |****| 4

Lights Out -- Printing |****| 0 |****| 1 2 | | 3 |****| 4 5

Lights Out -- Selecting What lights are valid for the user to select? on

Lights Out -- Selecting What lights are valid for the user to select? on off off on 0 1 2 3 4 5

Lights Out -- Switching How do we switch the necessary lights on or off?

Lights Out -- Switching How do we switch the necessary lights on or off? on off off on 0 1 2 3 4 5

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } ”); 5 int len

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; String[] quip for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } ”); 5 int len

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; ”); 5 int len array reference quip[1] quip[3] quip[0] quip[2] quip[4] String[] quip for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); }

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; ”); 5 int len for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } i is 0 i is 2 i is 1 fa ll es le av r te af le av ll fa String[] quip es quip[1] quip[3] quip[0] quip[2] quip[4] i is 4 i is 3

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; ”); 5 int len ll fa es le av r te af es av le fa String[] quip ll quip[1] quip[3] quip[0] quip[2] quip[4] for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); i is 4 i is 3 i is 1 i is 0 i is 2 } fall leaves after leaves fall

This week in CS 5 • Getting data together -- arrays ! • HW

This week in CS 5 • Getting data together -- arrays ! • HW 8 (2 problems) due Sunday, 10/28 at midnight due Monday, 10/29 at midnight Recitation for HW 8 will be Friday 10/26 Reading: Week 7’s online notes M/T sections W/Th sections No recitation this Friday, 10/19 • Indenting will count on HW 8 (and after)! See HW for notes on aligning things correctly, as well as deleting files.

public static int num. Syllables(String w) { int num. Syls = 0; int len

public static int num. Syllables(String w) { int num. Syls = 0; int len = w. length(); if ( is. Vowel(w. char. At(0)) ) ++num. Syls; Syllable counting // an initial vowel ? for (int i=1 ; i<w. length() ; ++i) { // vowel preceded by a consonant if ( is. Vowel(w. char. At(i))&& !is. Vowel(w. char. At(i-1)) ) ++num. Syls; } // final ‘e’ preceded by a consonant if ( w. char. At(len-1) == 'e’ && len >= 2 && !is. Vowel(w. char. At(len-2)) ) --num. Syls; if (num. Syls < 1) num. Syls = 1; return num. Syls; } // every word has at least 1 syllable

A puzzle. . . Take in a number of words and print them out

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

A puzzle. . . Take in a number of words and print them out

A puzzle. . . Take in a number of words and print them out in reverse order. (To be specific, suppose it’s 5 words. ) String String s 1 s 2 s 3 s 4 s 5 = = = H. out. print( H. in. next. Word(); s 5 s 4 s 3 s 2 s 1 + + + “ ” ); “n” ); Not a very flexible solution. . .

Arrays - lists of data items declares a String array named quip String[] quip;

Arrays - lists of data items declares a String array named quip String[] quip; quip = new String[5]; declares five Strings named quip[0]…quip[4] for (int i=0 ; i<5 ; ++i) { quip[i] = H. in. next. Word(); } index loop through the array

Arrays in code H. out. println(“Type the number of words: int len = H.

Arrays in code H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } ”); // create an empty array // create array elements // input each element // now print them out in reverse order…

Sentence palindromes fall leaves after leaves fall bores are people that say that people

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, can’t you, but you can’t swallow a cage, can you?

Arrays Element types Example Declaration vs Strings double, int, String, boolean, char, (any type)

Arrays Element types Example Declaration vs Strings double, int, String, boolean, char, (any type) … char double[] arr; arr = new double[100]; String s; The ith element arr[i] s. char. At(i) Length arr. length s. length() Range Warning from 0 up to length-1 Be careful not to go out of bounds! java. lang. String. Index. Out. Of. Bounds. Exception: -1 java. lang. Array. Index. Out. Of. Bounds. Exception: -1

T. T. Securities Input stock prices� for a number of days in a row,

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?

Arrays and Methods public static double sum. Array(double[] arr)

Arrays and Methods public static double sum. Array(double[] arr)

Using sum. Array public static void main(String[] args) { // prompt for and input

Using sum. Array public static void main(String[] args) { // prompt for and input n. Stocks double[] stocks = new double[n. Stocks]; // input and assign each stocks[i] double stock. Sum = sum. Array(stocks); H. out. println(“The sum is ” + stock. Sum); } double[] stocks 90. 0 10. 0 60. 0 42. 0 public static double sum. Array(double[] arr) { // see previous page … return sum; } 75. 0 double[] arr 70. 0

Using sum. Array public static void main(String[] args) { // prompt for and input

Using sum. Array public static void main(String[] args) { // prompt for and input n. Stocks double[] stocks = new double[n. Stocks]; // input and assign each stocks[i] double stock. Sum = sum. Array(stocks); H. out. println(“The sum is ” + stock. Sum); } double[] stocks 90. 0 10. 0 60. 0 42. 0 75. 0 2 references referring to the same list of data public static double sum. Array(double[] arr) { // see previous page … return sum; } double[] arr 70. 0

Array Searching public static double find. Max(double[] arr)

Array Searching public static double find. Max(double[] arr)

T. T. Securities Find the most profitable strategy for buying and selling the stock

T. T. Securities Find the most profitable strategy for buying and selling the stock among the prices in the array. . . Day Day Day 0 1 2 3 4 5 Price Price is is is 90. 0 10. 0 60. 0 42. 0 75. 0 70. 0

Lights Out ! on on off off on A starting row of lights 0

Lights Out ! on 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 Goal: get all the lights off…

Lights Out ! Features of the game: // allow the user to set the

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

Lights Out ! // draw the current set of lights 0 “on” lights should

Lights Out ! // draw the current set of lights 0 “on” lights should be 4 x 4 blocks of stars | | 2 1 3 4 |****|****|****| 0 “off” lights should be 4 x 4 blocks of spaces 1 2 3 print light numbers close to the center of each lights should be separated with vertical bars 5 |****| 4 5 6 7 may display all light numbers up to 15

Lights Out ! // allow the user to select a light // only allow

Lights Out ! // allow the user to select a light // only allow valid lights !

Summary To declare an array: double[] stocks; String[] quip; int[] grades; To declare an

Summary To declare an array: double[] stocks; String[] quip; int[] grades; To declare an array’s individual elements: stocks = new double[n. Stocks]; quip = new String[n. Words]; grades = new int[42]; To loop through an array: for (int i=0 ; i<stocks. length ; ++i) { do something with stocks[i] in here. . . }

Drawing room i (x[i] - xav)2 N 0 1 2 3 4 5

Drawing room i (x[i] - xav)2 N 0 1 2 3 4 5

Printed version following this slide

Printed version following this slide

This week in CS 5 • Putting data together with arrays • HW 8

This week in CS 5 • Putting data together with arrays • HW 8 (2 problems) due Sunday, 10/28 at midnight due Monday, 10/29 at midnight M/T sections W/Th sections Recitation for HW 8 will be Friday 10/26 No recitation this Friday, 10/19 -- Reading: Week 7’s online notes • Indenting will count on HW 8 (and after)! if ( is. Vowel(w. char. At(0)) ) { ++num. Syls; } See HW for notes on aligning things correctly, as well as deleting files.

Arrays - lists of data items String[] quip; - declares a String array named

Arrays - lists of data items String[] quip; - declares a String array named quip String[] quip = new String[5]; - declares five Strings named quip[0]…quip[4] quip[1] quip[3] quip[0] quip[2] quip[4] ll fa es le av r te es av af for (int i=0 ; i<5 ; ++i) { quip[i] = H. in. next. Word(); } le fa - looping through the array ll String[] quip

Arrays in code H. out. println(“Type the number of words: int len = H.

Arrays in code H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } // create array variable // create data variables // input each word // now print them out in reverse order… for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } ”);

Arrays in pictures H. out. println(“Type the number of words: int len = H.

Arrays in pictures H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; ”); 5 int len for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } ll fa es le av r te af es av le fa String[] quip ll quip[1] quip[3] quip[0] quip[2] quip[4]

Arrays Element types Example Declaration vs. Strings double, int, String, boolean, char, (any type)

Arrays Element types Example Declaration vs. Strings double, int, String, boolean, char, (any type) … char double[] arr; arr = new double[100]; String s; The ith element arr[i] s. char. At(i) Length arr. length s. length() Range Warnings from 0 up to length-1 Be careful not to go out of bounds! java. lang. String. Index. Out. Of. Bounds. Exception: -1 java. lang. Array. Index. Out. Of. Bounds. Exception: -1

T. T. Securities Input stock prices� for a number of days in a row,

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?

Arrays and Methods public static double sum. Array(double[] arr)

Arrays and Methods public static double sum. Array(double[] arr)

Using sum. Array public static void main(String[] args) { // prompt for and input

Using sum. Array public static void main(String[] args) { // prompt for and input n. Stocks double[] stocks = new double[n. Stocks]; // input and assign each stocks[i] double stock. Sum = sum. Array(stocks); H. out. println(“The sum is ” + stock. Sum); } double[] stocks 90. 0 stocks[0] 10. 0 stocks[1] 60. 0 stocks[2] 42. 0 stocks[3] 75. 0 stocks[4] public static double sum. Array(double[] arr) { // see previous page … return sum; } 70. 0 stocks[5] double[] arr

Array Searching public static double find. Max(double[] arr)

Array Searching public static double find. Max(double[] arr)

T. T. Securities Find the most profitable strategy for buying and selling the stock

T. T. Securities Find the most profitable strategy for buying and selling the stock among the prices in the array. . . Day Day Day 0 1 2 3 4 5 Price Price is is is 90. 0 10. 0 60. 0 42. 0 75. 0 70. 0

Lights Out ! Features of the game: // allow the user to set the

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

Summary To declare an array: double[] stocks; String[] quip; int[] grades; To declare an

Summary To declare an array: double[] stocks; String[] quip; int[] grades; To declare an array’s individual elements: stocks = new double[n. Stocks]; quip = new String[n. Words]; grades = new int[42]; To loop through an array: for (int i=0 ; i<stocks. length ; ++i) { do something with stocks[i] in here. . . }

A puzzle. . . Take in a number of words and print them out

A puzzle. . . Take in a number of words and print them out in reverse order. (To be specific, suppose it’s 5 words. ) String String s 1 s 2 s 3 s 4 s 5 = = = H. out. print( H. in. next. Word(); s 1 s 2 s 3 s 4 s 5 + + + “ ” ); “n” ); Not a very flexible solution. . .

Arrays - lists of data items declares a String array named quip String[] quip;

Arrays - lists of data items declares a String array named quip String[] quip; quip = new String[5]; declares five Strings named quip[0]…quip[4] for (int i=0 ; i<5 ; ++i) { quip[i] = H. in. next. Word(); } index loop through the array

Arrays in code H. out. println(“Type the number of words: int len = H.

Arrays in code H. out. println(“Type the number of words: int len = H. in. next. Int(); String[] quip; quip = new String[len]; for (int i=0 ; i<len ; ++i) { quip[i] = H. in. next. Word(); } // create an empty array // create array elements // input each element // now print them out in reverse order… for (int i=len-1 ; i>=0 ; --i) { H. out. print( quip[i] + “ ” ); } ”);

Arrays Element types Example Declaration vs Strings double, int, String, boolean, char, (any type)

Arrays Element types Example Declaration vs Strings double, int, String, boolean, char, (any type) … char double[] arr; arr = new double[100]; String s; The ith element arr[i] s. char. At(i) Length arr. length s. length() Range Warning from 0 up to length-1 Be careful not to go out of bounds! java. lang. String. Index. Out. Of. Bounds. Exception: -1 java. lang. Array. Index. Out. Of. Bounds. Exception: -1

Methods display. Menu display. Prices sum. Array average. Array std. Dev. Array index. Of.

Methods display. Menu display. Prices sum. Array average. Array std. Dev. Array index. Of. Smallest index. Of. Largest Day Day 0 1 2 3 4 5 6 7 It’s not always with the minimum or maximum ! Price Price is is 95. 0 15. 0 90. 0 10. 0 60. 0 42. 0 75. 0 70. 0

T. T. Securities Find the most profitable strategy for buying and selling the stock

T. T. Securities Find the most profitable strategy for buying and selling the stock among the prices in the array. . . Day Day Day 0 1 2 3 4 5 Price Price is is is 90. 0 10. 0 60. 0 42. 0 75. 0 70. 0

Using sum. Array public static void main(String[] args) { // prompt for and input

Using sum. Array public static void main(String[] args) { // prompt for and input n. Stocks double[] stocks = new double[n. Stocks]; // input and assign each stocks[i] double stock. Sum = sum. Array(stocks); H. out. println(“The sum is ” + stock. Sum); } double[] stocks 90. 0 10. 0 60. 0 42. 0 public static double sum. Array(double[] arr) { // see previous page … return sum; } 75. 0 double[] arr 70. 0