+ All Categories
Home > Documents > Nelson Series Talk Wed, 10/13 7:00 pm HMC’s Galileo Auditorium On Robots, Probability, and...

Nelson Series Talk Wed, 10/13 7:00 pm HMC’s Galileo Auditorium On Robots, Probability, and...

Date post: 29-Dec-2015
Category:
Upload: shanon-cain
View: 212 times
Download: 0 times
Share this document with a friend
Popular Tags:
93
Nelson Series Talk Wed, 10/13 7:00 pm HMC’s Galileo Auditorium ts, Probability, and Artificial Intell 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, Sebastian Thrun & robots, Stanford
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

CS 5

• Hw 6 (3 problems)

• Midterm exam #1:

• Today: is software engineering a valid term?

Hw6Pr2 – Throwing darts at PI

Hw6Pr1 – Mudd Morning Meander

M/T sections

W/Th sections

due Sunday, 10/10 at midnight

due Monday, 10/11 at midnight

• emails are being sent out about taking the exam this week• it is closed-book & take-home with 4 problems (up to 2 hours)• exam will be available from Olin 1265 on Friday, Oct. 8

• practice exam and solutions available on the “Files for Download” page

Hw6Pr3 – A program that reads?

• they are due under the door of Olin 1265 by 5:00 pm on Sunday, Oct. 10

• Reading: Online text, wk 6 • Recitation: Friday, 8:00 am

N-ZLast Names

LAB:

PAIRS

public static void main(String[] args){ H.pl(“Enter a line of text:”); String s = H.nl(); for (int j=0 ; j<26 ; ++j) { for (int i=0 ; i<s.length() ; ++i) { char c = s.charAt(i); H.p(advanceChar(c,j)); } H.pl(); }}

Caesar Cipher

What’s missing here?

public static void main(String[] args){ H.pl(“Enter a line of text:”); String s = H.nl(); for (int j=0 ; j<26 ; ++j) // j is the shift amt { for (int i=0 ; i<s.length() ; ++i) { // i indexes each letter char c = s.charAt(i); H.p(advanceChar(c,j)); } H.pl(); }}

Caesar Cipher

one-line comments for each block

public static void main(String[] args){ H.pl(“Enter a line of text:”); String s = H.nl(); for (int j=0 ; j<26 ; ++j) // j is the shift amt { for (int i=0 ; i<s.length() ; ++i) { // i indexes each letter char c = s.charAt(i); H.p(advanceChar(c,j)); } H.pl(); }}

Caesar Cipher

/* advanceChar shifts letters with wrapping * INPUT: char c to be shifted * int n the shift amt. * OUTPUT: the shifted char */public static char advanceChar(char c, int n)

input/output comment for whole

method

public static void main(String[] args){ int x = H.ni(); int y = H.ni(); int z = confusing(x,y); H.pl(“z is ” + z); }

public static int confusing(int y, int x){ int ans = 0; ans = 10*y + x; return ans;}

What’s in a name ?

public static void main(String[] args){ int x = H.ni(); int y = H.ni(); int z = confusing(x,y); H.pl(“z is ” + z); }

public static int confusing(int y, int x){ int ans = 0; ans = 10*y + x; return ans;}

What’s in a name ?

intx

inty

intz

intx

inty

intans

What is CS really about?

thinking like a machine

What is CS really about?

for and while loops

variables

if … else …making decisions

storage

repeated actions

“switch” or “stay”

charc

thinking like a machine

‘a’

thinking like a machine

for and while loops

variables

if … else …making decisions

storage

repeated actions

thinking for a machine

“switch” or “stay”

deciding how to use these toolstoolkit

What is CS really about!

charc

‘a’

What is CS really about!

thinking like a machine

for and while loops

variables

if … else …making decisions

storage

repeated actions

thinking for a machine

“switch” or “stay”

deciding how to use these toolstoolkit

methodscreating your own functions ...

charc

‘a’

What is CS really about!

thinking like a machine

for and while loops

variables

if … else …making decisions

storage

repeated actions

thinking for a machine

“switch” or “stay”

deciding how to use these toolstoolkit

methodscreating your own functions ...

classescreating your own data structures ...

(week 10)

charc

‘a’

Top-down program design

Given: a description of the problem

Wanted: a program that solves ittranslation!

Top-down program design

Given: a description of the problem

1. Visualize what the program will do -- the more detail, the better

2. Break the work required into a set of smaller tasks.

3. Sketch out these tasks (in a file or on paper)

- If so, write ( and test ) a method that does the job.

Wanted: a program that solves it

• Do any of the pieces you need occur often ?

• What do you need: variables, if/switch, for/while loops?

1. Visualize what the method will do ...

translation!

Top-down program design

Given: a description of the problem

1. Visualize what the program will do -- the more detail, the better

2. Break the work required into a set of smaller tasks.

3. Sketch out these tasks (in a file or on paper)

- If so, write ( and test ) a method that does the job.

Wanted: a program that solves it

• Do any of the pieces you need occur often ?

• What do you need: variables, if/switch, for/while loops?

1. Visualize what the method will do ...

translation!

an example...

An example “close to home”

......0 1 2 3 N-1-2-3-N

An overworked HMC student (S) leaves Platt after their “late-night” breakfast and, each moment, randomly

stumbles toward Olin (W) or toward Linde (E)

Olin Linde(E)(W)

Platt

Write a program to model this scenario...

Hw6 Pr1

S

Once the student arrives at the dorm or classroom, the trip is complete.

The program should then print the total number of steps taken.

......0 1 2 3 N-1-2-3-N

an overworked HMC student leaves Platt after their “late-night” breakfast and, each moment, randomly stumbles toward Olin (W) or

toward Linde (E)

“Quiz”

Olin Linde

List variables you’ll need…

(E)(W)

Platt

What is a method that would behelpful to write for this program?What are its inputs and outputs?

123

(more)

Method:

S

What true/false tests will be needed?

12

(more)

What does this code print?

for (int i = -7 ; i <= 7 ; ++i){ if (i == 3) { H.p(“0->-<”); } else { H.p(“z”); }}

public static void main(String[] args){ H.p(“How wide is the path? ”); int N = H.ni(); // get path width (N) from user int s = 0; // the student’s initial position is 0

while ( stillGoing(s,N) ) { printLine(s,N); s = s + H.randInt(-1,1); }}

public static boolean stillGoing(int x, int N){ if ( x > -N && x < N ) return true; else return false;}

Methods can be used anywhere!

public static void main(String[] args){ H.p(“How wide is the path? ”); int N = H.ni(); // get path width (N) from user int s = 0; // the student’s initial position is 0

while ( stillGoing(s,N) ) { printLine(s,N); s = s + H.randInt(-1,1); }}

public static boolean stillGoing(int x, int N){ if ( x > -N && x < N ) return true; else return false;}

Methods can be used anywhere!

public static void main(String[] args){ H.p(“How wide is the path? ”); int N = H.ni(); // get path width (N) from user int s = 0; // the student’s initial position is 0

while ( x > -N && x < N ) { printLine(s,N); s = s + H.randInt(-1,1); }}

public static boolean stillGoing(int x, int N){ if ( x > -N && x < N ) return true; else return false;}

Methods can be used anywhere!

What’s wrong here?

public static void main(String[] args){ H.p(“How wide is the path? ”); int N = H.ni(); // get path width (N) from user int s = 0; // the student’s initial position is 0

while ( s > -N && s < N ) { printLine(s,N); s = s + H.randInt(-1,1); }}

Methods can be used anywhere!

How else might this be done?

public static void main(String[] args){ H.p(“How wide is the path? ”); int N = H.ni(); // get path width (N) from user int s = 0; // the student’s initial position is 0

while ( Math.abs(s) < N ) { printLine(s,N); s = s + H.randInt(-1,1); }}

A method’s variables are its own

uses a basic random-walk model with unequal step probabilities

Gel electrophoresis

Used to separate proteins and nucleic acids (DNA) from a

biological sample. Molecules with different properties travel

different distances.

one of many applications for random walks…

Monte Carlo A engineering challenge: to estimate using everyday items...

Hw6 Pr2

Visualize:

Easy as

(-1,-1)

(1,1)

Breaking into pieces…

0. The user specifies how many darts to throw.

1. The user specifies how accurately to estimate pi.

// get numDarts from user

// use a loop to throw that many darts

// keep track of how many hits we get

// estimate

// get tolerance from user

// start throwing darts

// keep track of ??

Monte Carlo

0. The user specifies how many darts to throw.

1. The user specifies how accurately to estimate pi.

// get tolerance from user

// start throwing darts

// keep track of # of hits and # of darts!

// estimate each time// continue while we are OUTSIDE the tol.

// get numDarts from user

// use a loop to throw that many darts

// keep track of how many hits we get

// estimate

Methods ?

What gets done all the time in this problem?

public static boolean throwDart()

(0,0)

x

y

1 (1,1)

(-1,-1)

Can computers read? Hw6 Pr3 Pair Prog

Can computers read?

Too much dependence on context!

Vision

Understanding speech

Understanding text

Planning the consequences of actions

Why can’t computers read?

They can look up the meaning of any word instantly.

They can scan through millions of words a second. +

-we don’t know how to break these tasks into small enough pieces!

but I can!

We don’t know how we read!

Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't

mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt

tihng is taht the frist and lsat ltteer be at the rghit pclae.

The rset can be a total mses and you can sitll raed it wouthit

porbelm. Tihs is bcuseae the huamn mnid deos not raed

ervey lteter by istlef, but the wrod as a wlohe.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.English: A stone of the bearing does not gather foam.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.English: A stone of the bearing does not gather foam.French: Une pierre du roulement ne rassemble pas la mousse.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.English: A stone of the bearing does not gather foam.French: Une pierre du roulement ne rassemble pas la mousse.English: A stone of rolling does not collect moss.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.English: A stone of the bearing does not gather foam.French: Une pierre du roulement ne rassemble pas la mousse.English: A stone of rolling does not collect moss.Italian: Una pietra di rolling non raccoglie muschio.

Machine translation

El alcohol está dispuesto pero la carne es débil.The alcohol is arranged but the meat is weak.

The spirit is willing but the flesh is weak.

http://babel.altavista.com/tr

English

Spanish

English

English: A stone of stocks does not enter foam rubber.German: Ein Stein des Lagers erfaßt nicht Schaumgummi.English: A stone of the bearing does not gather foam.French: Une pierre du roulement ne rassemble pas la mousse.English: A stone of rolling does not collect moss.Italian: Una pietra di rolling non raccoglie muschio.English: A rolling stone gathers no moss.

Human translation ?

"Leave your clothes here and spend the afternoon having a good time."

- In a Rome laundry

"We take your bags and send them in all directions."- In a Copenhagen airline ticket office

“Please leave your values at the desk”- In a Paris hotel

Perhaps an answer...

Make no mistake about it: computers process numbers, not symbols. Computers can perform tasks only in so far as we can arithmetize them.

- Alan Perlis

-- the importance of thinking for computers!

Problem: to determine the readability of text

Arithmetizing reading: readability

Problem: to determine the readability of text

Arithmetizing reading: readability

The cow is of the bovine ilk. One end is moo the other milk! END

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal. END

- Ogden Nash

- Abraham Lincoln

Problem: to determine the readability of text

206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)

The Flesch readability index

Arithmetizing reading: readability

The cow is of the bovine ilk. One end is moo the other milk! END

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal. END

Problem: to determine the readability of text

The cow is of the bovine ilk. One end is moo the other milk! END

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal. END

very readable

a bit harder...

206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)

The Flesch readability index

103

37

Arithmetizing reading: readability

Problem: to determine the readability of text

206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)

very readable

• 14 words (don’t count the punctuation marks!)

• 2 sentences (do count the punctuation marks!)

• 16 syllables (“bovine” and “other” are the two-syllable words)

206.835 - 84.6*(16/14) - 1.015*(14/2)

watch out! ~ 103

Arithmetizing reading: readability

The cow is of the bovine ilk. One end is moo the other milk! END

95 - Comics 82 - Advertisements 65 - Sports Illustrated 57 - Time 39 - New York Times 10 - Auto insurance policy -6 - Internal Revenue Code

37

103

Typical Readability Scores

The cow is of the bovine ilk. One end is moo the other milk! END

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal. END

Handling inputThe cow is of the bovine ilk. One end is moo the other milk! END

H.inputFromFile("../testfile.txt");

while (true) { String w1 = H.nw(); // H.pl("w1 is " + w1);

if (w1.equals("END")) break; String w2 = dePunct(w1); // H.pl("w2 is " + w2); }

starting code

Handling inputThe cow is of the bovine ilk. One end is moo the other milk! END

H.nw() handles all whitespace as word separators. Newlines are not different than spaces. the input will end with exactly the word “END”

H.inputFromFile("../testfile.txt");

while (true) { String w1 = H.nw(); // H.pl("w1 is " + w1);

if (w1.equals("END")) break; String w2 = dePunct(w1); // H.pl("w2 is " + w2); }

starting code text will be read in from a file – feel free to paste any text you want!

you need to count sentences, words, and

syllables

for counting words and syllables you should use words stripped of their punctuation

Handling inputThe cow is of the bovine ilk. One end is moo the other milk! END

H.inputFromFile("../testfile.txt");

while (true) { String w1 = H.nw(); // H.pl("w1 is " + w1);

if (w1.equals("END")) break; String w2 = dePunct(w1); // H.pl("w2 is " + w2); }

starting code

you need to count sentences, words, and

syllables

sentences

words

Sentences are any inputs w1

that end with . !

or ?

Words are any inputs without punctuation w2

with length > 0

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: count sentences

and print the total @ the end

// distinguish REAL words from “.” “!” “?”

// Stage 2: count words

print both totals @ the end

// count the syllables of each word

// Stage 3: count syllables

try one word at a time at first…

// put it all together...

Syllables

What’s a syllable ?

Basically, a syllable is a cluster of 1 or more vowels in a word.

Fourscoreother she

aeiouyAEIOUY

Put another way, a syllable is• any vowel that starts a word, plus• any vowel that follows a consonant

except

• a lone e at the end of the word

But above all, every word must count as at least 1 syllable!

Helpful Methods

public static boolean isVowel(char c)

Helpful Methods

public static int countSyllables(String word)

Summary

(1) thinking like a machine (2) thinking for a machine

for and while loops

variables

if … else …

charc

‘a’

making decisions

storage

repeated actions

methods

deciding how to use these toolsWhat the machine can do:

creating your own tools !

1. Visualize what the program will do.

2. Break it into smaller pieces

3. Sketch those pieces in a file / on paper

-- if any occur often, create a method ...“switch” or “stay”

Lab this week

• Problem 2: throwing darts at pi

You’ll need to write (and use) the method:

public static boolean dartThrow()

• Problem 3: Flesch readability score

You’ll need to write (and use) 2 methods:

public static int countSyllables(String word)

public static boolean isVowel(char c)

• Problem 1: The Meandering Student (ASCII “Animation”)

Last Names N-Z

(0,0)

x

y

1 (1,1)

(-1,-1)

“Jotto”

Problem: to find the hidden word...

Looking ahead...

• What is CS all about ?

(1) thinking like a machine

for and while loops

variables

if … else …

booleanmore

true

making decisions

storage

“switch” or “stay”

repeated actions

Looking ahead...

• What is CS all about ?

(1) thinking like a machine

for and while loops

variables

if … else …

booleanmore

true

making decisions

storage

repeated actions

(2) thinking for a machine

deciding how to use these tools

toolkit

“switch” or “stay”

Monte Carlo

1. The user specifies how many darts to throw.

2. The user specifies how accurately to estimate pi.

// get numDarts from user

// use a for loop to throw that many darts

// keep track of how many hits we get

// after all the darts, estimate

// get tolerance from user

// start throwing darts

// keep track of # of hits and # of darts!

// estimate each time// continue while we are OUTSIDE the tol.

Hall-of-fame code...

H.out.println("\n\n\nLet's make a deal!!"); H.out.println("Behind one of these two curtains is an new car!!"); H.out.println("Unfortunately, behind the other two are cans of spam."); H.out.println("\n/----1----\\ /----2----\\ /----3----\\"); H.out.println("|#########| |#########| |#########|"); H.out.println("|####_####| |###___###| |###___###|"); H.out.println("|###__####| |##_###_##| |##_###_##|"); H.out.println("|####_####| |#####_###| |####__###|"); H.out.println("|####_####| |####_####| |##_###_##|"); H.out.println("|###___###| |##_____##| |###___###|"); H.out.println("|#########| |#########| |#########|"); H.out.print("\nPlease pick a curtain: ");

BEFORE

public static void main(String[] args){ int loc = 0; while ( stillDriving(loc) ) { H.out.println(“I’m at ” + loc); if ( Math.random() > 0.5 ) ++loc; else --loc; } H.out.println(“Help!”);}

public static boolean stillDriving(int x){ if ( x >= -10 && x <= 10 ) return true; else return false;}

Methods can be used anywhere!

The cow is of the bovine ilk . One end is moo the other milk !

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal .

37

103

Readability Scores

Problem: to determine the readability of text

Computers evaluating reading?

The cow is of the bovine ilk . One end is moo the other milk !

Fourscore and seven years ago our fathers brought forth on this continent a new nation conceived in Liberty and dedicated to the proposition that all men are created equal .

Readability score depends on • the number of words per sentence• the number of syllables per word

206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)

very readable

a bit harder...

Breaking up the problem:

Readability ScoreHW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

HW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: simply count up the # of words

and print out the total @ the end

HW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: simply count up the # of words

and print out the total @ the end

// distinguish REAL words from “.” “!” “?”

HW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: simply count up the # of words

and print out the total @ the end

// distinguish REAL words from “.” “!” “?”

// Stage 2: count up the # of words + sents.

print out both totals @ the end

HW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: simply count up the # of words

and print out the total @ the end

// distinguish REAL words from “.” “!” “?”

// Stage 2: count up the # of words + sents.

print out both totals @ the end

// count the syllables of each word

HW6PR2

Breaking up the problem:

Readability Score

// get each word in turn - using nextWord()

// stop when END is seen

// Stage 1: simply count up the # of words

and print out the total @ the end

// distinguish REAL words from “.” “!” “?”

// Stage 2: count up the # of words + sents.

print out both totals @ the end

// count the syllables of each word

// Stage 3: take in SINGLE words and print

the number of syllables in each

HW6PR2

Syllables

What’s a syllable ?

Basically, a syllable is a cluster of 1 or more vowels in a word.

Fourscoreother she

aeiouyAEIOUY

Syllables

What’s a syllable ?

Basically, a syllable is a cluster of 1 or more vowels in a word.

Fourscoreother she

aeiouyAEIOUY

2 3 1

Syllables

What’s a syllable ?

Basically, a syllable is a cluster of 1 or more vowels in a word.

Fourscoreother she

aeiouyAEIOUY

Put another way, a syllable is• any vowel that starts a word, plus• any vowel that follows a consonant

2 3 1

Syllables

What’s a syllable ?

Basically, a syllable is a cluster of 1 or more vowels in a word.

Fourscoreother she

aeiouyAEIOUY

Put another way, a syllable is• any vowel that starts a word, plus• any vowel that follows a consonant

except

• a lone e at the end of the word

2 2 0

Monte Carlo

1. The user specifies how many darts to throw.

2. The user specifies how accurately to estimate pi.

Monte Carlo

1. The user specifies how many darts to throw.

2. The user specifies how accurately to estimate pi.

// get numDarts from user

// use a for loop to throw that many darts

// keep track of ...

Monte Carlo

1. The user specifies how many darts to throw.

2. The user specifies how accurately to estimate pi.

// get numDarts from user

// use a for loop to throw that many darts

// keep track of how many hits we get

// after all the darts, estimate

......0 1 2 3 10-1-2-3-10

an overworked HMC student gets onto a very wide highway and randomly switches lanes to make better time to get home

What code will check if they are still on the road?

Out for a random drive...

public static boolean stillDriving(int x){ if ( x >= -10 && x <= 10 ) return true; else return false;}

Today in CS 5

• HW 6 (2 problems)

• Midterm exam #1:

• Today: further practice with methods & design

HW6PR2 -- Getting a computer to read...

How this person will make CS 5 immeasurably easier!

HW6PR1 -- The problem

M/T sections

W/Th sections

due Sunday, 10/13 at midnight

due Monday, 10/14 at midnight

• emails have been sent out about taking the exam• it is closed-book & take-home with 4 problems (up to 2 hours)• exam will be available from Olin 1265 on Friday, Oct. 11

• practice exam and solutions available at

http://www.cs.hmc.edu/courses/2002/fall/cs5/week_06/lecture/lecture.html (Week 6’s notes)

HW6PR3 -- (Ex.Cr.) A guessing game

• they are due under the door of Olin 1265 by 5:00 pm on Sunday, Oct. 13

public static void main(String[] args){ int x = H.ni(); int y = H.ni(); int z = confusing(x,y); H.pl(“z is ” + z); }

public static int confusing(int y, int x){ int a = 0; a = 10*y + x; return a;}

What’s in a name ?

public static void main(String[] args){ int loc = 0; while ( stillGoing(loc) ) { loc = loc + swerve(); }}

public static int swerve(){ if ( Math.random() > 0.5 ) return 1; else return -1;} public static boolean stillGoing(int x){ if ( x >= -10 && x <= 10 ) return true; return false;}

Methods can be used anywhere!

public static void main(String[] args){ int loc = 0; while ( x >= -10 && x <= 10 ) { loc = loc + swerve(); }}

public static int swerve(){ if ( Math.random() > 0.5 ) return 1; else return -1;}

Methods can be used anywhere!

What’s wrong here?

public static void main(String[] args){ int loc = 0; while ( loc >= -10 && loc <= 10 ) { loc = loc + swerve(); }}

public static int swerve(){ if ( Math.random() > 0.5 ) return 1; else return -1;}

Methods can be used anywhere!

Better !

public static void main(String[] args){ int loc = 0; while ( Math.abs(loc) <= 10 ) { loc = loc + swerve(); }}

public static int swerve(){ if ( Math.random() > 0.5 ) return 1; else return -1;}

Methods can be used anywhere!

Even better !

Math.abs(x) returns x

Math.PI is 3.14159265…

Methods !!

Any often-needed capability can be

methodized...

thank Conor for these...

public static void p(String s){ H.out.print(s);}

public static void pl(String s){ H.out.println(s);}

Put these 4 lines in every program, and use them!

printlnprint

Methods !!

Any often-needed capability can be

methodized...

public static void p(String s){ H.out.print(s);}

public static void pl(String s){ H.out.println(s);}

Put these 4 lines in every program, and use them!

printlnprint

thank Conor for these...

Hall-of-fame code...

BEFORE

AFTER

pl("\n\n\nLet's make a deal!!"); pl("Behind one of these two curtains is an new car!!"); pl("Unfortunately, behind the other two are cans of spam."); pl("\n/----1----\\ /----2----\\ /----3----\\"); pl("|#########| |#########| |#########|"); pl("|####_####| |###___###| |###___###|"); pl("|###__####| |##_###_##| |##_###_##|"); pl("|####_####| |#####_###| |####__###|"); pl("|####_####| |####_####| |##_###_##|"); pl("|###___###| |##_____##| |###___###|"); pl("|#########| |#########| |#########|"); p("\nPlease pick a curtain: ");

H.out.println("\n\n\nLet's make a deal!!"); H.out.println("Behind one of these two curtains is an new car!!"); H.out.println("Unfortunately, behind the other two are cans of spam."); H.out.println("\n/----1----\\ /----2----\\ /----3----\\"); H.out.println("|#########| |#########| |#########|"); H.out.println("|####_####| |###___###| |###___###|"); H.out.println("|###__####| |##_###_##| |##_###_##|"); H.out.println("|####_####| |#####_###| |####__###|"); H.out.println("|####_####| |####_####| |##_###_##|"); H.out.println("|###___###| |##_____##| |###___###|"); H.out.println("|#########| |#########| |#########|"); H.out.print("\nPlease pick a curtain: ");

pl("\n\n\nLet's make a deal!!"); pl("Behind one of these two curtains is an new car!!"); pl("Unfortunately, behind the other two are cans of spam."); pl("\n/----1----\\ /----2----\\ /----3----\\"); pl("|#########| |#########| |#########|"); pl("|####_####| |###___###| |###___###|"); pl("|###__####| |##_###_##| |##_###_##|"); pl("|####_####| |#####_###| |####__###|"); pl("|####_####| |####_####| |##_###_##|"); pl("|###___###| |##_____##| |###___###|"); pl("|#########| |#########| |#########|"); p("\nPlease pick a curtain: ");

Hall-of-fame code...

H.out.println("\n\n\nLet's make a deal!!"); H.out.println("Behind one of these two curtains is an new car!!"); H.out.println("Unfortunately, behind the other two are cans of spam."); H.out.println("\n/----1----\\ /----2----\\ /----3----\\"); H.out.println("|#########| |#########| |#########|"); H.out.println("|####_####| |###___###| |###___###|"); H.out.println("|###__####| |##_###_##| |##_###_##|"); H.out.println("|####_####| |#####_###| |####__###|"); H.out.println("|####_####| |####_####| |##_###_##|"); H.out.println("|###___###| |##_____##| |###___###|"); H.out.println("|#########| |#########| |#########|"); H.out.print("\nPlease pick a curtain: ");

BEFORE

AFTER


Recommended