Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however."— Presentation transcript:

1 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

2 Problem 1 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 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.

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 i=0 ; i<arr.length ; ++i) { sum = sum + arr[i]; } 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 0 1 2 34 5 0 1 2 34 5 0 1 2 34 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… onoffonoff on

9 Problem 2 0 1 2 34 5 onoffonoff 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…

10 What is a light ? 0 1 2 34 5 How should we represent these lights in Java ? onoffonoff on

11 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 34 5 onoffonoff on

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

13 |****| |****| | |****| |****| |****| | |****| 0 1 2 3 4 5 6 7 Lights Out -- Printing // draw the current set of lights 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 34 5 onoffonoff on

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

15 Lights Out -- Selecting 0 1 2 34 5 What lights are valid for the user to select? onoffonoff on

16 Lights Out -- Switching 0 1 2 34 5 How do we switch the necessary lights on or off? onoffonoff on

17 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

18 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

19 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

20 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 0 i is 1 i is 2 i is 3 i is 4

21 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 1i is 0 fall leaves after leaves fall

22 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.

23 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

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

25 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...

26 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 String s named quip[0]…quip[4] 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 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…

28 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!”

29 Strings Be careful not to go out of bounds! Element types double, 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.lengths.length() Warning Range from 0 up to length-1 vs Arrays

30 T. T. Securities Input stock prices for a number of days in a row, and then analyze that data…. 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? Menu:

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 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.042.0 75.0 70.0 double[] arr

33 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.042.0 75.0 70.0 double[] arr 2 references referring to the same list of data

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 ! 0 1 2 34 5 0 1 2 34 5 0 1 2 34 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

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 0 1 2 34 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

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

40 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:

41 (x[i] - x av ) 2  N i 0 1 2 34 5 0 1 2 34 5 0 1 2 34 5 Drawing room

42 Printed version following this slide

43 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; }

44 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 quip[0] quip[1] quip[2]quip[4] quip[3] fall leaves after leaves fall - declares a String array named quip - declares five String s named quip[0]…quip[4] - looping through the array

45 Arrays in code H.out.println(“Type the number of words: ”); int len = H.in.nextInt(); String[] quip; // create array variable quip = 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] + “ ” ); }

46 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

47 Arrays vs. Strings Be careful not to go out of bounds! Element types double, 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.lengths.length() Warnings Rangefrom 0 up to length-1

48 T. T. Securities Input stock prices for a number of days in a row, and then analyze that data…. 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? Menu:

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 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.042.0 75.0 70.0 double[] arr stocks[0] stocks[1]stocks[2]stocks[3]stocks[4]stocks[5]

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 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:

55 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...

56 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 String s named quip[0]…quip[4] 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 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] + “ ” ); }

58 Strings Be careful not to go out of bounds! Element types double, 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.lengths.length() Warning Range from 0 up to length-1 vs Arrays

59 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 It’s not always with the minimum or maximum ! Methods displayMenu displayPrices sumArray averageArray stdDevArray indexOfSmallest indexOfLargest

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 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.042.0 75.0 70.0 double[] arr


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

Similar presentations


Ads by Google