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.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Homework 6 Due ( MT sections ) ( WTh sections ) about midnight Sun., 10/12 Mon., 10/13 Problems
Two examples of Problem Solving in Programming H. Chad Lane University of Pittsburgh CS7: Introduction to Programming.
Hand Crafting your own program By Eric Davis for CS103.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
1 Modelling 1-D Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing Array.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
The switch Statement, DecimalFormat, and Introduction to Looping
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Java: Chapter 1 Computer Systems Computer Programming II.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
By the end of this session you should be able to...
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/21 Mon., 9/22 Problems
This week in CS 5 HW 9 (2 problems) M/T sections W/Th sections due Sunday, 11/4 at midnight due Monday, 11/5 at midnight Recitation for HW9 -- Friday 11/2.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 4: Control Structures II
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Input and typing trouble! print 'Please input a number of meters' meters = input() # get input from user cm = meters * 100 # convert to centimeters print.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 10 / 29 / 2007 Instructor: Michael Eckmann.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
Code Compression the benefits of looping... Today in CS 5 HW 4 - (3 problems) M/T sections W/Th sections due Sunday, 9/26 at midnight due Monday, 9/27.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
int [] scores = new int [10];
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Homework 10 Due ( MT sections ) ( WTh sections ) at midnight Sun., 11/10 Mon., 11/11 Problems
Information and Computer Sciences University of Hawaii, Manoa
Values vs. References Lecture 13.
Lecture 4b Repeating With Loops
Paul Ammann & Jeff Offutt
CS 177 Week 15 Recitation Slides
One-Dimensional Array Introduction Lesson xx
HMC’s Galileo Auditorium Some of Cope’s generated MP3s:
int [] scores = new int [10];
python.reset() Also moving to a more reasonable room (CSE 403)
int [] scores = new int [10];
Week 4 Lecture-2 Chapter 6 (Methods).
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Week 7 - Monday CS 121.
Presentation transcript:

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?