On Robots, Probability, and Artificial Intelligence Nelson Series Talk

advertisement
On Robots, Probability, and Artificial Intelligence
Nelson Series Talk
Wed, 10/13
Sebastian Thrun &
robots, Stanford
7:00 pm
HMC’s Galileo Auditorium
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.
Last Names
LAB: N-Z
CS 5
• Hw 6 (3 problems)
due Sunday, 10/10 at midnight
due Monday, 10/11 at midnight
Hw6Pr1 – Mudd Morning Meander
M/T sections
W/Th sections
Hw6Pr3 – A program that reads?
Hw6Pr2 – Throwing darts at PI
PAIRS
• Today: is software engineering a valid term?
• Midterm exam #1:
• 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
• they are due under the door of Olin 1265 by 5:00 pm on Sunday, Oct. 10
• practice exam and solutions available on the “Files for Download” page
• Reading: Online text, wk 6
• Recitation: Friday, 8:00 am
Caesar Cipher
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();
}
}
What’s missing here?
Caesar Cipher
public static void main(String[]
{
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()
{
//
char c = s.charAt(i);
H.p(advanceChar(c,j));
}
H.pl();
}
}
args)
j is the shift amt
; ++i)
i indexes each letter
one-line comments for each block
Caesar Cipher
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();
}
/* advanceChar shifts letters with wrapping
}
* INPUT: char c to be shifted
input/output comment
for whole method
*
int n the shift amt.
* OUTPUT: the shifted char
*/
public static char advanceChar(char c, int n)
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 ?
public static void main(String[] args)
{
int x = H.ni();
int
int
int y = H.ni();
y
x
int z = confusing(x,y);
H.pl(“z is ” + z);
int
}
z
public static int confusing(int y, int x)
{
int ans = 0;
int
int
y
x
ans = 10*y + x;
return ans;
}
int
ans
What is CS really about?
thinking like a machine
What is CS really about?
thinking like a machine
variables
storage
‘a’
char
c
if … else …
making decisions
“switch” or “stay”
for and while loops
repeated actions
What is CS really about!
thinking like a machine
variables
storage
‘a’
char
c
if … else …
making decisions
“switch” or “stay”
for and while loops
repeated actions
thinking for a machine
toolkit
deciding how to use these tools
What is CS really about!
thinking like a machine
variables
storage
‘a’
char
c
if … else …
making decisions
“switch” or “stay”
for and while loops
repeated actions
thinking for a machine
toolkit
deciding how to use these tools
methods
creating your own functions ...
What is CS really about!
thinking like a machine
variables
storage
‘a’
char
c
if … else …
making decisions
thinking for a machine
toolkit
deciding how to use these tools
methods
creating your own functions ...
“switch” or “stay”
for and while loops
repeated actions
classes
creating your own data structures ...
(week 10)
Top-down program design
Given:
a description of the problem
Wanted:
a program that solves it
translation!
Top-down program design
Given:
a description of the problem
Wanted:
a program that solves it
translation!
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)
• What do you need:
variables, if/switch, for/while loops?
• Do any of the pieces you need occur often ?
- If so, write ( and test ) a method that does the job.
1. Visualize what the method will do ...
Top-down program design
Given:
a description of the problem
Wanted:
a program that solves it
translation!
1. Visualize what the program will do
-- the more detail, the better
2. Break the work required into a set of smaller tasks.
an example...
3. Sketch out these tasks (in a file or on paper)
• What do you need:
variables, if/switch, for/while loops?
• Do any of the pieces you need occur often ?
- If so, write ( and test ) a method that does the job.
1. Visualize what the method will do ...
An example “close to home”
...
Olin
(W)
-N
Platt
...
S
-3 -2 -1 0
Hw6 Pr1
1
2
3
An overworked HMC student (S) leaves Platt after their
“late-night” breakfast and, each moment, randomly
stumbles toward Olin (W) or toward Linde (E)
Once the student arrives at the dorm or classroom, the trip is complete.
The program should then print the total number of steps taken.
Write a program to model this scenario...
Linde
(E)
N
“Quiz”
an overworked HMC student leaves Platt after their “late-night” breakfast
and, each moment, randomly stumbles toward Olin (W) or toward Linde (E)
Platt
...
Olin
(W)
-N
...
S
-3 -2 -1 0
List variables you’ll need…
1
2
3
2
3
What true/false tests will be needed?
1
2
(more)
(more)
What is a method that would be
helpful to write for this program?
What are its inputs and outputs?
1
Method:
Linde
(E)
N
What does this code print?
for (int i = -7 ; i <= 7 ; ++i)
{
if (i == 3)
{
H.p(“0->-<”);
}
else
{
H.p(“z”);
}
}
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 ( 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);
}
What’s wrong here?
}
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 ( s > -N && s < N )
{
printLine(s,N);
s = s + H.randInt(-1,1);
}
}
How else might
this be done?
A method’s variables are its own
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);
}
}
Gel electrophoresis
one of many applications for random walks…
uses a basic randomwalk model with unequal
step probabilities
Used to separate proteins and
nucleic acids (DNA) from a
biological sample. Molecules
with different properties travel
different distances.
Hw6 Pr2
Monte Carlo p
A engineering challenge: to estimate p using everyday items...
Easy as p
(1,1)
Visualize:
(-1,-1)
Breaking p into pieces…
0. The user specifies how many darts to throw.
// get numDarts from user
// use a loop to throw that many darts
// keep track of how many hits we get
// estimate p
1. The user specifies how accurately to estimate pi.
// get tolerance from user
// start throwing darts
// keep track of ??
Monte Carlo p
0. The user specifies how many darts to throw.
// get numDarts from user
// use a loop to throw that many darts
// keep track of how many hits we get
// estimate p
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 p each time
// continue while we are OUTSIDE the tol.
Methods ?
What gets done all the time in this problem?
public static boolean throwDart()
(1,1)
1
y
(-1,-1)
(0,0)
x
Can computers read?
Hw6 Pr3
Pair Prog
but I can!
Can computers read?
Why can’t computers read?
They can look up the meaning of any word instantly.
They can scan through millions of words a second.
Too much dependence on context!
Vision
Understanding speech
Understanding text
Planning the consequences of actions
+
-
we don’t know how to
break these tasks into
small enough pieces!
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
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
English
Spanish
English
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
English
Spanish
English
English: A stone of stocks does not enter foam rubber.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
English
Spanish
English
English: A stone of stocks does not enter foam rubber.
German: Ein Stein des Lagers erfaßt nicht Schaumgummi.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
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.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
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.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
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.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
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.
http://babel.altavista.com/tr
Machine translation
The spirit is willing but the flesh is weak.
El alcohol está dispuesto pero la carne es débil.
The alcohol is arranged but the meat is weak.
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.
http://babel.altavista.com/tr
Human translation ?
"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
"Leave your clothes here and spend the afternoon
having a good time."
- In a Rome laundry
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!
Arithmetizing reading: readability
Problem: to determine the readability of text
Arithmetizing reading: readability
Problem: to determine the readability of text
The cow is of the bovine ilk.
One end is moo the other milk!
END
- Ogden Nash
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
- Abraham Lincoln
Arithmetizing reading: readability
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
The Flesch readability index
206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)
Arithmetizing reading: readability
Problem: to determine the readability of text
The cow is of the bovine ilk.
One end is moo the other milk!
END
very readable
103
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
a bit harder...
37
The Flesch readability index
206.835 - 84.6*(syllables/word) - 1.015*(words/sentence)
Arithmetizing reading: readability
Problem: to determine the readability of text
The cow is of the bovine ilk.
One end is moo the other milk!
END
• 14 words
very readable
(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*(syllables/word) - 1.015*(words/sentence)
206.835 - 84.6*(16/14) - 1.015*(14/2)
watch out!
~ 103
Typical Readability Scores
95 - Comics
82 - Advertisements
65 - Sports Illustrated
57 - Time
39 - New York Times
10 - Auto insurance policy
-6 - Internal Revenue Code
103
The cow is of the bovine ilk. One
end is moo the other milk! END
37
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 input
starting code
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);
}
The cow is of the bovine
ilk. One end is moo the
other milk! END
Handling input
starting code
The cow is of the bovine
ilk. One end is moo the
other milk! END
text will be read in from
a file – feel free to
paste any text you want!
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);
}
you need to count sentences,
words, and syllables
H.nw() handles all whitespace
as word separators. Newlines
are not different than spaces.
the input will end with
exactly the word “END”
for counting words and
syllables you should use
words stripped of their
punctuation
Handling input
starting code
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);
}
you need to count sentences,
words, and syllables
The cow is of the bovine
ilk. One end is moo the
other milk! END
sentences
Sentences are
any inputs w1
that end with
. ! or ?
words
Words are any
inputs without
punctuation w2
with length > 0
Readability Score
Breaking up the problem:
// 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 ?
aeiouyAEIOUY
Basically, a syllable is a cluster of 1 or more vowels in a word.
other
Fourscore
she
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
What the machine can do:
deciding how to use these tools
methods
variables
storage
(2) thinking for a machine
char
c
creating your own tools !
1. Visualize what the program will do.
if … else …
2. Break it into smaller pieces
making decisions
3. Sketch those pieces in a file / on paper
“switch” or “stay”
-- if any occur often, create a method ...
for and while loops
repeated actions
Lab this week
Last Names N-Z
• Problem 2: throwing darts at pi
(1,1)
1
You’ll need to write (and use) the method:
public static boolean dartThrow()
• Problem 3: Flesch readability score
y
(-1,-1)
(0,0)
x
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”)
“Jotto”
Problem: to find the hidden word...
Looking ahead...
• What is CS all about ?
(1) thinking like a machine
variables
storage
boolean
more
if … else …
making decisions
“switch” or “stay”
for and while loops
repeated actions
Looking ahead...
• What is CS all about ?
(1) thinking like a machine
variables
storage
(2) thinking for a machine
toolkit
boolean
more
if … else …
making decisions
“switch” or “stay”
for and while loops
repeated actions
deciding how to use these tools
Monte Carlo p
1. The user specifies how many darts to throw.
// 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 p
2. The user specifies how accurately to estimate pi.
// get tolerance from user
// start throwing darts
// keep track of # of hits and # of darts!
// estimate p each time
// continue while we are OUTSIDE the tol.
Hall-of-fame code...
BEFORE
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: ");
Methods can be used anywhere!
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;
}
Readability Scores
103
The cow is of the bovine ilk .
One end is moo the other milk !
37
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 .
Computers evaluating reading?
Problem: to determine the readability of text
The cow is of the bovine ilk .
One end is moo the other milk !
very readable
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 .
a bit harder...
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)
HW6PR2
Readability Score
Breaking up the problem:
HW6PR2
Readability Score
Breaking up the problem:
// get each word in turn - using nextWord()
// stop when END is seen
HW6PR2
Readability Score
Breaking up the problem:
// 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
Readability Score
Breaking up the problem:
// 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
Readability Score
Breaking up the problem:
// 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
Readability Score
Breaking up the problem:
// 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
Readability Score
Breaking up the problem:
// 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
Syllables
What’s a syllable ?
aeiouyAEIOUY
Basically, a syllable is a cluster of 1 or more vowels in a word.
other
Fourscore
she
Syllables
What’s a syllable ?
aeiouyAEIOUY
Basically, a syllable is a cluster of 1 or more vowels in a word.
other
2
Fourscore
3
she
1
Syllables
What’s a syllable ?
aeiouyAEIOUY
Basically, a syllable is a cluster of 1 or more vowels in a word.
other
2
Fourscore
3
Put another way, a syllable is
• any vowel that starts a word, plus
• any vowel that follows a consonant
she
1
Syllables
What’s a syllable ?
aeiouyAEIOUY
Basically, a syllable is a cluster of 1 or more vowels in a word.
other
2
Fourscore
2
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
she
0
Monte Carlo p
1. The user specifies how many darts to throw.
2. The user specifies how accurately to estimate pi.
Monte Carlo p
1. The user specifies how many darts to throw.
// get numDarts from user
// use a for loop to throw that many darts
// keep track of ...
2. The user specifies how accurately to estimate pi.
Monte Carlo p
1. The user specifies how many darts to throw.
// 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 p
2. The user specifies how accurately to estimate pi.
Out for a random drive...
...
-10
...
-3 -2 -1 0
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?
public static boolean stillDriving(int x)
{
if ( x >= -10 && x <= 10 )
return true;
else
return false;
}
Today in CS 5
• HW 6 (2 problems)
due Sunday, 10/13 at midnight
due Monday, 10/14 at midnight
M/T sections
W/Th sections
HW6PR1 -- The p problem
HW6PR2 -- Getting a computer to read...
HW6PR3 -- (Ex.Cr.) A guessing game
• Midterm exam #1:
How this person will make
CS 5 immeasurably easier!
• 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
• they are due under the door of Olin 1265 by 5:00 pm on Sunday, Oct. 13
• practice exam and solutions available at
http://www.cs.hmc.edu/courses/2002/fall/cs5/week_06/lecture/lecture.html
(Week 6’s notes)
• Today: further practice with methods & design
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 a = 0;
a = 10*y + x;
return a;
}
Methods can be used anywhere!
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;
}
What’s wrong here?
Methods can be used anywhere!
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;
}
Better !
Methods can be used anywhere!
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;
}
Math.abs(x)
returns
Math.PI
3.14159265…
is
x
Even better !
Methods !!
Any often-needed
capability can be
methodized...
thank Conor for these...
public static void pl(String s)
{
H.out.println(s);
}
public static void p(String s)
{
H.out.print(s);
}
println
print
Put these 4 lines in
every program,
and use them!
Methods !!
Any often-needed
capability can be
methodized...
public static void pl(String s)
{
H.out.println(s);
}
thank Conor for these...
println
print
public static void p(String s)
{
H.out.print(s);
}
Put these 4 lines in
every program,
and use them!
Hall-of-fame code...
BEFORE
AFTER
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...
BEFORE
AFTER
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: ");
Download