Nelson Series Talk Wed, 10/13 7:00 pm HMC’s Galileo Auditorium On Robots, Probability, and Artificial Intelligence This talk will expose the audience to recent developments in real-world robotics. The speaker and his team have developed mobile robots that have operated as interactive tour-guides in a Smithsonian museum, assisted elderly people in everyday tasks, and explored several abandoned coal mines inaccessible to people, all completely autonomously. His current effort aims at winning the DARPA Grand Challenge, which requires the development of a ground vehicle that can drive from L.A. to Las Vegas without human assistance. These developments would not have been possible without a new paradigm in robot software design, known as probabilistic robotics. Probabilistic robotics imports concepts from statistics and decision theory into the field of robotics. As part of this presentation, the speaker will introduce the audience to the basics of probabilistic robotics, and explain why statistical techniques have become such an essential tool in robotics, in such a remarkably short time. Sebastian Thrun & robots, Stanford
Week 7 in CS 5 HW 8 (2 problems) M/T sections W/Th sections due Sunday, 10/24 at midnight due Monday, 10/25 at midnight Getting data together -- arrays ! Fall break There is no lecture next week! There is no recitation this Friday! There WILL BE recitation NEXT Friday (8 am) Seasons by Mike Maguire Showcasing unusual solutions… A-M Last Names LAB:
public static int numSyllables(String w) { int numSyls = 0; int L = w.length(); if ( isVowel(w.charAt(0)) ) // an initial vowel ? ++numSyls; for (int i=1 ; i < L ; ++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(L-1) == 'e’ && L >= 2 && !isVowel(w.charAt(L-2)) ) --numSyls; if (numSyls < 1) // every word has at least 1 syllable numSyls = 1; return numSyls; } Syllable counting
How could you print a String backwards? A puzzle... String w = H.nw(); for ( int i= ; ;) { } How could you print 5 String s in backwards order? (Not reversing each string…)
How could you print 5 String s in backwards order? (Not reversing each string…) A puzzle... String s1 = H.nw(); String s2 = H.nw(); String s3 = H.nw(); String s4 = H.nw(); String s5 = H.nw(); H.pl( s5 ); H.pl( s4 ); H.pl( s3 ); H.pl( s2 ); H.pl( s1 ); Not a very flexible solution...
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!” Maybe we don’t even need to solve it?!
A palindromic poem... Seasons by Mike Maguire
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int L 5
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int L 5 String[] A
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int L String[] A A[0] A[1] A[2]A[4] A[3] 5 array reference
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int L String[] A A[0] A[1] A[2]A[4] A[3] 5 I sawthe sigh- -ign i is 0 i is 1 i is 2 i is 3 i is 4
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int L 5 i is 4 i is 3 i is 2 i is 1i is 0 -ign sigh- the saw I String[] A A[0] A[1] A[2]A[4] A[3] I saw the sigh- -ign
Arrays - lists of data items double[] A; A = new double[5]; for (int i=0 ; i<5 ; ++i) { A[i] = H.nd(); } declares a double array named A declares five double s named A[0] … A[4] loops through the array, getting input from the user index length
Arrays in code H.pl(“How many doubles would you like to store? ”); int L = H.ni(); double[] A; A = new double[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nd(); } // now print them out (in forward order !)
James Gosling Software architect for Sun Microsystems Primary designer of the Java programming language Sees Bill Gates as a dangerous force…
Arrays Be careful not to go out of bounds! Element types char The ith element Example Declaration Length s.charAt(i) String s; java.lang.StringIndexOutOfBoundsException: -1 java.lang.ArrayIndexOutOfBoundsException: -1 s.length() Warning Range from 0 up to length-1 vs Strings double, int, String, boolean, char, (any type!) A[i] double[] A; A = new double[100]; A.length
T. T. Securities Input stock prices for a number of days in a row, and then analyze that data….
T. T. Securities Input stock prices for a number of days in a row, and then analyze that data…. Software 0 Display prices 1 Sum of prices 2 Average of prices 3 St. dev. of prices 4 Minimum price 5 Index of minimum price 6 Maximum price 7 Index of minimum price 8 Your TTS investment strategy 9 Quit Which choice would you like? Menu
T. T. Securities Input stock prices for a number of days in a row, and then analyze that data…. Hardware Software 0 Display prices 1 Sum of prices 2 Average of prices 3 St. dev. of prices 4 Minimum price 5 Index of minimum price 6 Maximum price 7 Index of minimum price 8 Your TTS investment strategy 9 Quit Which choice would you like? Menu
T. T. Securities Input stock prices for a number of days in a row, and then analyze that data…. 0 Display prices 1 Sum of prices 2 Average of prices 3 St. dev. of prices 4 Minimum price 5 Index of minimum price 6 Maximum price 7 Index of minimum price 8 Your TTS investment strategy 9 Quit Which choice would you like? Menu void menu() void prices(double[] A) double sum(double[] A) double average(double[] A) double stdev(double[] A) double min(double[] A) int minIndex(double[] A) double max(double[] A) int maxIndex(double[] A) no separate method needed methods …
Standard Deviation There are a number of formulas, but we will use this one: 0 Display prices 1 Sum of prices 2 Average of prices 3 St. dev. of prices 4 Minimum price 5 Index of minimum price 6 Maximum price 7 Index of minimum price 8 Your TTS investment strategy 9 Quit Which choice would you like? Menu methods (A[i] - A av ) 2 L - 1 i double stdev(double[] A)
Code Sketch: main public static void main(String[] args) { H.pl(“How many days?”); int L = H.ni(); // get array length double[] A = new double[L]; // create array A H.pl(“Please input your prices:”); for (int i=0 ; i<L ; ++i) { A[i] = H.nd(); } while (true) { // print menu and handle choices
Printing the prices public static void prices(double[] A)
public static double sum(double[] A) { double s = 0.0; for ( { } return s; } public static double average(double[] A) “Extra Credit”: How concise can this method be? Finish these two methods… “Quiz” This method returns the sum of the elements in the input array. This method returns the average of the elements in the input array.
public static double max(double[] A) Extra: What is something unusual and unrelated to CS 5 that you & the person next to you have in common ?! This method returns the maximum element from the input array.
Using sum public static void main(String[] args) { // set up the array A of stock prices H.pl( “The sum is ” + sum(A) ); } public static double sum(double[] Z) { // see previous page or quiz … return ans; } double[] A double[] Z
Using sum public static void main(String[] args) { // set up the array A of stock prices H.pl( “The sum is ” + sum(A) ); } double[] Z 2 references to the same list of data public static double sum(double[] Z) { // see previous page or quiz … return ans; } double[] A
Array Searching public static double max (double[] A)
Option #8 Find the most profitable strategy for buying and selling the stock among the prices in the array... Day 0 Price 90.0 Day 1 Price 10.0 Day 2 Price 60.0 Day 3 Price 42.0 Day 4 Price 75.0 Day 5 Price 5.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 Hw8 Pr2Pair Program
Lights Out Strategy... // choose HOW you’ll represent the lights ! // let the user set the # 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 !
Lights Out Methods public static void printLights(int[] Lts) You need to choose your own methods to write for this program... Feel free to use (or ignore) my two methods -- public static boolean allAreOff(int[] Lts)
Summary double[] A; String[] song; int[] Lights; To declare an array: To loop through an array: A = new double[L]; quip = new String[nWords]; Lights = new int[42]; for (int i=0 ; i<A.length ; ++i) { do something with A[i] in here... } To declare an array’s individual elements:
Lab this week Problem 1: T. T. Securities You’ll need to write (and use) Problem 2: Lights Out! You may choose what methods to write... Extra Credit: (1) An undo feature for Lights Out... Last Names A-M | |****|****|****| |****| | |****|****|****| |****| void menu() void prices(double[] A) double sum(double[] A) double average(double[] A) double stdev(double[] A) double min(double[] A) int minIndex(double[] A) double max(double[] A) int maxIndex(double[] A) (2) A sound-editing program
A palindromic poem... Seasons by Mike Maguire
Representing Sound continuous plot of air pressure vs. time samples taken every ~ 1/11000th of a second Each sample is measured on a loudness scale from 0 to 65,535. (This fits into 2 bytes.) These two bytes are called a frame. The frames are ordered in an array of bytes. Thus, the raw audio data is a byte[]. sampling quantization storage physics
public static double sum(double[] A) { double s = 0.0; for ( { } return s; } public static double average(double[] A) “Extra Credit”: How concise can this method be? Finish these two methods… “Quiz” This method returns the sum of the elements in the input array. This method returns the average of the elements in the input array.
public static double max(double[] A) Extra: What is something unusual and unrelated to CS 5 that you & the person next to you have in common ?! This method returns the maximum element from the input array.
“Quiz” Follow this code to determine a partner! public static void main(String[] args) { H.pl(“Enter your number -- see overhead slide! ”); int x = H.ni(); if (x > 16 && x%2 == 0) { H.pl( “Your partner is #” + (48-x)/2 ); } else if ( x > 16 ) { H.pl( “Your partner is #” + (64-x) ); } else { H.pl( “Your partner is #” + 2*(24-x) ); } Then, find this partner and try writing the two methods on the other side -- use the prices example as a starting point.
public static double sum(double[] A) public static double average(double[] A) Extra Credit: What is something unusual and unrelated to CS 5 that you two or you three have in common ?! Try to write these two methods Names:
H.out.println(“Type the number of words: ”); int L = H.ni(); String[] A; A = new String[L]; for (int i=0 ; i<L ; ++i) { A[i] = H.nw(); } for (int i=L-1 ; i>=0 ; --i) { H.pl( A[i] ); } Arrays in pictures int len String[] A A[0] A[1] A[2]A[4] A[3] 5 Oops I did it again i is 0 i is 1 i is 2 i is 3 i is 4
“Quiz” Follow this code to determine a partner! public static void main(String[] args) { H.pl(“Enter your number -- see overhead slide! ”); int x = H.ni(); if (x > 16 && x%2 == 0) { H.pl( “Your partner is #” + (48-x)/2 ); } else if ( x > 16 ) { H.pl( “Your partner is #” + (64-x) ); } else { H.pl( “Your partner is #” + 2*(24-x) ); } Then, find this partner and try writing the two methods on the other side -- use the prices example as a starting point.
(A[i] - A av ) 2 L - 1 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 Watch out for code indenting ! 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 ! 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
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
A real application (1) Decide how many days of analysis you’d like to do. (2) Input stock prices for a that number of days in a row (3) Analyze that data…. 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 Which choice would you like? Menu T. T. Securities’ analysis package
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
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
String s; // input the String here for (int i=0 ; i<s.length() ; ++i) { H.pl(s.charAt(i)); } double[] A = new double[5]; // input the doubles here for (int i=0 ; i<A.length ; ++i) { H.pl(A[i]); } Strings vs. Arrays differences?