Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems 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 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.
Problem 1 How do we do this ? Pseudocode...
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; }
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 ?
Method Notes int indexOfSmallest(double[] arr)
double smallest(double[] arr)
Problem 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
Problem 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…
What is a light ? How should we represent these lights in Java ? onoffonoff on
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 onoffonoff on
Lights Out -- Printing |****| |****| | |****| |****| |****| | |****|
|****| |****| | |****| |****| |****| | |****| 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 onoffonoff on
Lights Out -- Printing |****| |****| | |****| |****| |****| | |****|
Lights Out -- Selecting What lights are valid for the user to select? onoffonoff on
Lights Out -- Switching How do we switch the necessary lights on or off? onoffonoff on
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
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
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
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
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
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.
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
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 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...
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
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…
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!”
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
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:
Arrays and Methods public static double sumArray(double[] arr)
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 double[] arr
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 double[] arr 2 references referring to the same list of data
Array Searching public static double findMax(double[] arr)
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
Lights Out ! 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
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 | |****|****|****| |****| | |****|****|****| |****| 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
Lights Out ! // allow the user to select a light // only allow valid lights !
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:
(x[i] - x av ) 2 N i Drawing room
Printed version following this slide
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; }
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
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] + “ ” ); }
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
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
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:
Arrays and Methods public static double sumArray(double[] arr)
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 double[] arr stocks[0] stocks[1]stocks[2]stocks[3]stocks[4]stocks[5]
Array Searching public static double findMax(double[] arr)
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
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 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:
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...
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
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] + “ ” ); }
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
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
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
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 double[] arr