CS 5 Reminders • Take advantage of the tutors. Reading:

advertisement
CS 5 Reminders
• Hw 3 - (3 problems)
Reading:
due Sunday, 9/19 at midnight
due Monday, 9/20 at midnight
M/T sections
W/Th sections
Week 3’s sections of the online text
Hw3Pr1) The virtual songwriter
Programs:
Hw3Pr2) Mathematical Menu
Hw3Pr3) Rock-Paper-Scissors
• Take advantage of the tutors.
Linde Lab
Academic Computing Labs
- their schedule is linked from the CS5 webpage
• Friday 8:00 am recitation section: HW hints and Q&A
Today
• If Robert Frost had been a programmer...
if (road < traveledBy)
{
take(road);
that = all - the;
}
Two roads diverged in a wood, and I-I took the one less traveled by,
And that has made all the difference.
• Indecisive? CS 5 can help!
• Java conjunctions
• String theory -- it’s not just for physicists.
Last time
Variables as storage spaces
String animal = “”, noise = “”;
type: String
name: animal
H.pl(“Enter an animal and a noise it makes:”);
animal = H.nw();
noise = H.nw();
// next word
type: String
name: noise
Last time
Variables as storage spaces
String animal = “”, noise = “”;
type: String
name: animal
type: String
name: noise
H.pl(“Enter an animal and a noise it makes:”);
animal = H.nw();
noise = H.nw();
// next word
H.pl(“Old MacDonald had a farm.”);
H.pl(“E I E I O!”);
H.pl(“And on that farm he had a ” + animal);
H.pl(“E I E I O!”);
H.pl(“With a ” + noise + “ ” + noise + “ here”);
H.pl(“ and a ” + noise + “ ” + noise + “ there”);
This time
Hw3Pr1) The virtual songwriter
First, some questions:
What is one thing you really hate?
spam
Type three plural nouns related to that thing:
Hit return after each one.
tin cans
camping trips
unwanted emails
What is your favorite color? blue
What is your favorite poet? Frost
What is the name of a significant other?
Rita
Virtual Alanis
I Think
------I think tin cans are a really huge problem
I think camping trips are too much on my mind
I think unwanted emails are bringing the world down
But what can you do?
Like a blue rain, beating down on me
Like a Frost line, which won't let go of my brain
Like Rita’s voice, it is in my head
Blame it on spam
Blame it on spam
Blame it on spam
Either emulate this example, or adapt to suit your tastes...
Hw 3
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr1) The virtual songwriter
Hw 3
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr1) The virtual songwriter
Hw3Pr2) A mathematical menu
use switch to select among different cases
use the Moth Menu program to get you started…
check your numbers against the examples in the Hw !
Hw 3
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr1) The virtual songwriter
Hw3Pr2) A mathematical menu
use switch to select among different cases
use the Moth Menu program to get you started…
check your numbers against the examples in the Hw !
Option #2:
get L, a, and x from the
user and then output the
results of plugging them
into this formula... !
Straight-line code
Hw3Pr2) The math menu
thread of execution
int points = 0, leagues = 0;
H.pl(“Enter a number of leagues”);
H.pl(“And I will convert it to points”);
leagues = H.ni();
points = leagues * 3 * 5280 * 12 * 72 ;
H.pl(“That is ” + points + “ points.”);
straightforward code?
The Others: Java’s built-in types
int x = 5;
holds from -2,147,483,648 to 2,147,483,647
The Others: Java’s built-in types
int x = 5;
holds from -2,147,483,648 to 2,147,483,647
byte b = 3;
holds from
-128 to 127
short s = 4;
holds from
-32768 to 32767
long l = 6;
holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The Others: Java’s built-in types
int x = 5;
holds from -2,147,483,648 to 2,147,483,647
byte b = 3;
holds from
-128 to 127
short s = 4;
holds from
-32768 to 32767
long l = 6;
holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
String s = “this is a string of text”;
char c = ‘t’;
used to hold single characters
Notice the
single quotes !
The Others: Java’s built-in types
int x = 5;
holds from -2,147,483,648 to 2,147,483,647
byte b = 3;
holds from
-128 to 127
short s = 4;
holds from
-32768 to 32767
long l = 6;
holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
String s = “this is a string of text”;
char c = ‘t’;
used to hold single characters
Notice the
single quotes !
double d = 42.0;
float f = 42.0;
about 8 places of precision (vs. double’s 16)
The Others: Java’s built-in types
int x = 5;
holds from -2,147,483,648 to 2,147,483,647
byte b = 3;
holds from
-128 to 127
short s = 4;
holds from
-32768 to 32767
long l = 6;
holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
String s = “this is a string of text”;
char c = ‘t’;
used to hold single characters
Notice the
single quotes !
double d = 42.0;
float f = 42.0;
boolean b = true;
about 8 places of precision (vs. double’s 16)
holds either true or false
What’s different about String ?
Code:
String cc = “rock”;
What it seems like:
rock
String cc
What’s different about String ?
Code:
String cc = “rock”;
What it seems like:
rock
String cc
reference
What it really is:
#42
a memory location: #42
‘r’ ‘o’ ‘c’ ‘k’
char
String cc
char
char
char
#42 #43 #44 #45
mental models!
Input: Know your type!
H.in.nextWord();
H.in.nextLine();
return a String
H.in.nextChar();
H.in.nextAnyChar();
return a char
H.in.nextInt();
returns an int
H.in.nextLong();
returns a long
H.in.nextDouble();
returns a double
including whitespace
including whitespace
Input: Know your type!
H.in.nextWord();
H.in.nextLine();
return a String
H.in.nextChar();
H.in.nextAnyChar();
return a char
H.nc();
H.nanyc();
H.in.nextInt();
returns an int
H.ni();
H.in.nextLong();
returns a long
H.nlong();
H.in.nextDouble();
returns a double
H.nd();
including whitespace
including whitespace
H.nw();
H.nl();
shortcuts !
Variables are cheap...
Hw3Pr2) The math menu
double a=0, b=0, c=0;
H.pl(“Enter the three coefficients of a quadratic ” +
“equation and I’ll solve it…”);
a = H.nd();
b = H.nd();
Feel free to create variables as needed!
c = H.nd();
H.pl(“The solutions are ” +
+ “ and ” +
);
Math functions
Hw3Pr2) Mathematical Menu
Math.sqrt( 9 )
square root
Math.sin( Math.PI )
sine
Math.abs( -5 )
absolute value
Math.log( Math.E )
natural log (ln)
Math.exp( x )
e to the x
Math.toRadians( 360 )
angle conversion
Full Java library:
http://java.sun.com/j2se/1.4.2/docs/api/
Math.random()
Well, this is random…
Math.random()
returns a double
Code to store a random double from 0.0 to 2.0 :
including 0.0 excluding 2.0
To store a random double from 1.0 to 2.0 :
including 1.0 excluding 2.0
To store a random integer from 0 to 9 :
inclusive
To store a random integer from 1 to 3 :
inclusive
Hw3Pr2) Mathematical Menu
• greater or equal to 0.0
• less than 1.0
Randomness vs. Determinism
Are there random numbers?
Can a computer generate them?
RNG
Output
A “black box” model of a
random number generator.
Randomness vs. Determinism
Are there random numbers?
Can a computer generate them?
Yes
Not without help!
// initialize with current time
long seed = System.currentTimeMillis();
seed = (seed * 0x5DEECE66DL + 0xBL) &
((1L << 48) - 1);
int i1 = (int)(seed >>> (48 - 26));
seed = (seed * 0x5DEECE66DL + 0xBL) &
((1L << 48) - 1);
int i2 = (int)(seed >>> (48 - 27));
double randomNumber = (((long)i1 << 27) + i2)/
(double)(1L << 53);
The RNG revealed.
Output
What would a nondeterministic
computing machine be like?
True Randomness !
an enthusiastic endorser
http://www.leapzine.com/hr/
three “random” lava
lamp owners & mom
LavaRand’s lava lamps
using a chaotic physical system to
seed random number generators
(Patent 5,732,138: "Method for seeding a pseudorandom number generator with a cryptographic
hash of a digitization of a chaotic system.")
www.wired.com/wired/archive/11.08/random.html
This has since been “improved”…
Hw 3
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr1) The virtual songwriter
Hw3Pr2) A mathematical menu
use switch to select among different cases
use the Moth Menu program to get you started…
check your numbers against the examples in the Hw !
Extra Credit: find the max and min of 4 different
integers in as few comparisons as possible ?!
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
”);
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
if ( y == 1984 )
{
H.pl(“You’re a Leaper!”);
}
”);
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
”);
What is missing here ?!?
The if
block
if ( y == 1984 )
The “test” or “comparison”
{
H.pl(“You’re a Leaper!”);
}
this code happens only if the test is true
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
”);
What should this test be ?
if (
{
)
H.pl(“You’re a Leaper!”);
}
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
”);
is y divisible by 4 ?
if ( y%4 == 0 )
{
H.pl(“You’re a Leaper!”);
}
two uses of equals !!
==
==
=
vs. =
indicates a comparison between two values
It’s usually used with if
if ( road == 10 )
indicates an assignment from right to left
It’s usually a stand-alone statement.
road = 10;
==
==
=
vs. =
indicates a comparison between two values
It’s usually used with if
if ( road == 10 )
indicates an assignment from right to left
It’s usually a stand-alone statement.
road = 10;
== “are they equal ?”
=
“set equal !”
a question, a test
a command, an assignment
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
”);
if ( y%4 == 0 )
{
H.pl(“You’re a Leaper!”);
}
What about the rest of us?
The code not taken:
if
H.p(“What year were you born?
int y = H.ni();
The if
block
The else
block
”);
if ( y%4 == 0 )
{
H.pl(“You’re a Leaper!”);
}
Code run only when the
else
No test needed
previous if is false
{
H.pl(“You’re Leapfree.”);
}
A warning !
Inspired by Robert Frost and Route 94:
String message;
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
H.pl( message );
What if we want to go to Las Vegas?
Initialize variables!
Be sure to give your variables initial values:
String message = “The road more traveled”;
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
H.pl( message );
Now there is a message to print,
regardless of the road chosen.
Brace yourself…
H.p(“What year were you born?
int y = H.ni();
if ( y%4 == 0 )
H.pl(“You’re a Leaper!”);
else
H.pl(“You’re Leapfree.”);
H.pl(“My condolences…”);
Why is this program too apologetic?
”);
Brace yourself…
H.p(“What year were you born?
int y = H.ni();
if ( y%4 == 0 )
{
H.pl(“You’re a Leaper!”);
}
else
{
H.pl(“You’re Leapfree.”);
H.pl(“My condolences…”);
}
Watch out for your curly braces !
”);
Or
And
Not
What years match these conditions?
if ( year%4 == 0 && year > 1776 )
“and”
if ( year%4 == 0 || year%4 == 2 )
“or”
if ( year%20 != 0 )
“not”
“not equal”
if ( !(year%20 == 0) )
Presidential Problems
William H. Harrison
Abraham Lincoln
James A. Garfield
William McKinley
Warren G. Harding
Franklin D. Roosevelt
John F. Kennedy
Ronald Reagan
-- He died in office on April 4, 1841.
-- He was shot and died April 15, 1865.
-- He was shot and died September 19, 1881.
-- He was shot and died September 14, 1901.
-- He died in office August 2, 1923.
-- He died in office April 12, 1945.
-- He was shot and died November 22, 1963.
-- He was shot, but survived...
Presidential Problems
William H. Harrison
Abraham Lincoln
James A. Garfield
William McKinley
Warren G. Harding
Franklin D. Roosevelt
John F. Kennedy
Ronald Reagan
Determining
leap years
-- He died in office on April 4, 1841.
-- He was shot and died April 15, 1865.
-- He was shot and died September 19, 1881.
-- He was shot and died September 14, 1901.
-- He died in office August 2, 1923.
-- He died in office April 12, 1945.
-- He was shot and died November 22, 1963.
-- He was shot, but survived...
if ( yr%400 == 0 )
H.pl(“It is a leap year.”);
else if ( yr%4 == 0 && yr%100 != 0 )
H.pl(“It is a leap year.”);
else
H.pl(“It’s NOT a leap year.”);
Time for a switch
int month = H.ni();
int numDays = 0;
switch ( month )
{
case 2:
{
numDays = 28;
break;
}
case 4: case 6:
case 9: case 11:
{
numDays = 30;
break;
}
default:
{
numDays = 31;
}
}
Time for a switch
int month = H.ni();
int numDays = 0;
switch ( month )
{
case 2:
{
numDays = 28;
break;
}
case 4: case 6:
case 9: case 11:
{
numDays = 30;
break;
}
default:
{
numDays = 31;
}
}
Suppose
month == 9
.
It jumps to the appropriate case
Time for a switch
int month = H.ni();
int numDays = 0;
switch ( month )
{
case 2:
{
numDays = 28;
break;
}
case 4: case 6:
case 9: case 11:
{
numDays = 30;
break;
}
default:
{
numDays = 31;
}
}
Suppose
month == 9
.
It jumps to the appropriate case
It does everything up to the break
Time for a switch
int month = H.ni();
int numDays = 0;
switch ( month )
{
case 2:
{
numDays = 28;
break;
}
case 4: case 6:
case 9: case 11:
{
numDays = 30;
break;
}
default:
{
numDays = 31;
}
}
Suppose
month == 9
.
It jumps to the appropriate case
It does everything up to the break
It then jumps to the end of
the whole switch block.
“Quiz”
1
2
3
4
5
6
Names:
Jan 31
Feb 28
Mar 31
Apr 30
May 31
Jun 30
7 Jul 31
8 Aug 31
9 Sep 30
10 Oct 31
11 Nov 30
12 Dec 31
(1) Assume it is NOT a leap year. This code still assigns the
wrong number of days to February. WHY? Can you fix it?
int month = H.ni();
int numDays = 0;
if ( month == 2 )
{
numDays = 28;
}
if ( month == 4 || month == 6 ||
month == 9 || month == 11
)
{
numDays = 30;
}
else
{
numDays = 31;
}
(2) What could you change to
make this code shorter?
Write a switch statement that has the same behavior
as the if / else if / else statements to the left:
if (x == 0)
{
H.pl(0);
}
if (x == 1)
{
H.pl(1);
}
else if (x == 2)
{
H.pl(2);
}
else
{
H.pl(42);
}
switch (x)
{
case : {
}
3 cases and default.
“Quiz”
1 Jan 31
2 Feb 28
3 Mar 31
4 Apr 30
5 May 31
6 Jun 30
Names:
7 Jul 31
8 Aug 31
9 Sep 30
10 Oct 31
11 Nov 30
12 Dec 31
(1) Assume it is NOT a leap year. This code still assigns the
wrong number of days to February. WHY? Can you fix it?
int month = H.ni();
int numDays = 0;
if ( month == 2 )
{
numDays is set to
numDays = 28;
28 right here…
}
if ( month == 4 || month == 6 ||
month == 9 || month == 11
)
{
numDays = 30;
}
else
{
but numDays is incorrectly
numDays = 31;
reset to 31 right here!
}
(2) What could you change to
make this code shorter?
initial values!
“Quiz”
1 Jan 31
2 Feb 28
3 Mar 31
4 Apr 30
5 May 31
6 Jun 30
Names:
7 Jul 31
8 Aug 31
9 Sep 30
10 Oct 31
11 Nov 30
12 Dec 31
(1) Assume it is NOT a leap year. This code still assigns the
wrong number of days to February. WHY? Can you fix it?
int month = H.ni();
int numDays = 31;
if ( month == 2 )
{
numDays = 28;
}
if ( month == 4 || month == 6 ||
month == 9 || month == 11
)
{
numDays = 30;
}
(2) What could you change to
make this code shorter?
initial values!
Write a switch statement that has the same behavior
as the if / else if / else statements to the left:
if (x == 0)
{
H.pl(“zero”);
}
if (x == 1)
{
H.pl(“one”);
}
else if (x == 2)
{
H.pl(“two”);
}
else
{
H.pl(“more or less”);
}
switch (x)
{
case 0:
H.pl(“zero”);
break;
case 1:
H.pl(“one”);
break;
case 2:
H.pl(“two”);
break;
default:
H.pl(“m or l”);
break;
}
something’s still different!
Write a switch statement that has the same behavior
as the if / else if / else statements to the left:
if (x == 0)
{
H.pl(“zero”);
}
if (x == 1)
{
H.pl(“one”);
}
else if (x == 2)
{
H.pl(“two”);
}
else
{
H.pl(“more or less”);
}
switch (x)
{
case 1:
H.pl(“one”);
break;
case 2:
H.pl(“two”);
break;
case 0:
H.pl(“zero”);
default:
H.pl(“m or l”);
break;
}
Juggling ifs
int month = H.ni();
int numDays;
// Sets the number of days in month month. (1-12)
if ( month == 2 )
{
numDays = 28;
}
Anthony Gatto
else if ( month == 4 || month == 6 ||
’91 IJA numbers
month == 9 || month == 11 )
competition
{
numDays = 30;
}
else
if … else if … else if … else
{
creates mutually exclusive
numDays = 31;
blocks of code
}
Perspective
Programmers are not measured by
their ingenuity and their logic but by the
completeness of their case analysis.
- Alan Perlis
writer of the first compiler
Hw 3
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr1) The virtual songwriter
Hw3Pr2) A mathematical menu
use switch to select among different cases
use the Moth Menu program to get you started…
check your numbers against the examples in the Hw !
Hw3Pr3) Rock-Paper-Scissors: the last great decision-maker
use the
Evens && Odds
program to get you started…
What’s different about String ?
Variable Types
int
double
long
...
String
basic types -- simple values
composite type -- made up of other values
String is a class.
Classes should start
with a capital letter.
Don’t use == with Strings !
String cc = “paper”;
String uc = H.nl();
and the user types paper
if ( cc == uc )
{
H.pl(“It’s a draw!”);
}
This will not work!
== compares variables’ immediate contents
Use .equals
String cc =
randomly chosen “rock”
“paper”
or
String uc = H.nl();
if
{
( uc.equals(cc) )
“scissors”
Use .equals to
compare strings !
H.pl(“It’s a draw. Play again?”);
}
else if ( uc.equals(“paper”) && cc.equals(“rock”) )
{
H.pl(“You win!”);
}
else
{
H.pl(“I win!”);
}
.equals compares variables’ “true” contents
A-J: Mac Lab
Lab Today
Hw3Pr1) The virtual songwriter
M-Z: PC Lab
The example programs are available from the
“Files for download” link from the CS 5 webpage.
Hw3Pr2) A mathematical menu
use switch to select among different cases
use the Moth Menu program to get you started…
check your numbers against the examples in the Hw !
Hw3Pr3) Rock-Paper-Scissors: the last great decision-maker
use the
Evens && Odds
program to get you started…
• I’d suggest starting on Problem 2 or Problem 3
to try out switch && if / else if / else statements
What’s different about String ?
Code:
String cc = “paper”;
What it seems like:
paper
String cc
reference
What it really is:
#42
a memory location: #42
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
String cc
char
char
char
char
#42 #43 #44 #45 #46
Don’t use == with Strings !
String cc = “paper”;
String uc = H.nl();
cc == uc
and the user types paper
looks true ?!?
paper
String cc
What it seems like:
paper
String uc
Don’t use == with Strings !
String cc = “paper”;
String uc = H.nl();
cc == uc
reference
#42
What’s really
happening:
String cc
reference
#1000
String uc
and the user types paper
looks true ?!?
a memory location: #42
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
char
char
char
char
#42 #43 #44 #45 #46
a memory location: #1000
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
char
char
char
char
#1000 #1001 #1002 #1003 #1004
Use .equals
String cc = “paper”;
String uc = H.nl();
reference
#42
String cc
What’s really
happening:
and the user types paper
a memory location: #42
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
char
char
char
char
#42 #43 #44 #45 #46
cc.equals(uc) is true
reference
#1000
String uc
a memory location: #1000
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
char
char
char
char
#1000 #1001 #1002 #1003 #1004
The Others
String s = “this is a string of text”;
double d = 42.0;
int x = 5;
used to hold single characters
char c = ‘t’;
float f = 42.0;
about 8 places of precision (vs. double’s 16)
holds from -2,147,483,648 to 2,147,483,647
byte b = 3;
holds from
-128 to 127
short s = 4;
holds from
-32768 to 32767
long l = 6;
boolean b = true;
holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
holds either true or false
Initializing variables
String message;
int road = H.in.nextInt();
if ( road
{
message
}
if ( road
{
message
}
== 94 )
= “The road less traveled”;
== 10 )
= “The road more traveled”;
H.out.println( message );
What’s wrong here (potentially) ?
Presidential Problems
1840, William H. Harrison
1860, Abraham Lincoln
1880, James A. Garfield
1900, William McKinley
1920, Warren G. Harding
1940, Franklin D. Roosevelt
1960, John F. Kennedy
1980, Ronald Reagan
And leap years?
-- He died in office on April 4, 1841.
-- He was shot and died April 15, 1865.
-- He was shot and died September 19, 1881.
-- He was shot and died September 14, 1901.
-- He died in office August 2, 1923.
-- He died in office April 12, 1945.
-- He was shot and died November 22, 1963.
-- He was shot, but survived...
if ( yr%400 == 0 )
H.out.println(“LEAP!”);
else if ( yr%4 == 0 && yr%100 != 0 )
H.out.println(“LEAP”);
else
H.out.println(“No Leap.”);
Switch
switch ( month )
{
case 2:
numDays = 29;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
default:
numDays = 31;
break;
}
Suppose month
== 9
.
it jumps to that case
it does everything up to the break
it then jumps to the end of the switch
0.480017049832311
Election year
braces are safe everywhere
Olympic year
{
}
{
Good news for presidents...
}
braces are critical here
Stopping along the way
String message;
int road = H.in.nextInt();
The “if”
block
if ( road == 94 )
The “test” or “comparison”
{
message = “The road less traveled”;
}
this code happens only if the test is true
if ( road == 10 )
{
message = “The road more traveled”;
}
H.out.println( message );
What’s different about String ?
Code:
Abstraction:
String my = “paper”;
paper
String my
reference
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
Detail:
char
String my
char
char
char
char
Classes are both bad and good
Why bad?
== compares variables’ immediate contents
String my = “paper”;
String your = H.in.nextLine();
The user types paper
paper
String my
my == your
Abstraction:
looks true...
paper
String your
reference
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
String my
Detail:
reference
char
char
char
char
my == your is false !
These two references are different!
‘p’ ‘a’ ‘p’ ‘e’ ‘r’
char
String your
char
char
char
char
Use .equals with Strings
Why Good?
Classes come with lots of built-in capabilities.
String my = “paper”;
String your = H.in.nextLine();
if ( your.equals(“paper”) )
{
H.out.println(“It’s a draw. Play again?”);
}
else if ( your.equals(“scissors”) )
{
H.out.println(“Are you cheating?”);
}
else
{
H.out.println(“I win!”);
HW3PR3) Rock-Paper-Scissors
}
Code Warrior
starting to seem less fierce...
• If you’re using an old copy
you will need the old Java stationery
• Running CW from KATO
the instructions are at
http://www.hmc.edu/comp/doc/
Win2000, Windows ME seem to work (mostly) so far...
you will need Novell’s “client 32” & KeyAccess
there’s a lengthy one-time download
Using Code Warrior
• Open your project file.
HW2PR4
HW2PR4.mcp
always start with these!
Under windows: you need to show “all files” to view this !
• Submit your .java file.
CS5App
CS5App.java
GridMain
GridMain.java
submit only these!
Variables are cheap...
double a, b, c;
H.out.println(“Enter the coefficients of a quadratic ” +
“equation and I’ll solve it…”);
a = H.in.nextDouble();
b = H.in.nextDouble();
c = H.in.nextDouble();
Consider what variables
would be useful…
double d = b*b – 4*a*c;
double sol1 = ( -b + Math.sqrt(d) ) / (2*a);
double sol2 = ( -b - Math.sqrt(d) ) / (2*a);
H.out.println(“The solutions are ” +
+ “ and ” +
);
Comparisons
not equal
or
and
not
== vs =
Pisces -- tie in
with leap year ?
Today
• If Robert Frost had been a programmer...
if (road < traveledBy)
{
take(road);
that = all - the;
}
Two roads diverged in a wood, and I-I took the one less traveled by,
And that has made all the difference.
• Indecisive? CS 5 can help!
• Java conjunctions
• String theory -- not just for physicists.
What’s different about String ?
Code:
String s = “1000 W that a P is W”;
hello
Abstraction:
String s
reference
‘1’ ‘0’ ‘0’ ‘0’ ‘ ’ ‘W’ ‘ ’ ‘t’
Detail:
char
String s
char
char
char
char
char
char
char
…
Randomness vs. Determinism
… and free will
At heart, computers are
deterministic creatures.
Even randomly generated
numbers are explainable.
But we can still think outside the box:
What would a nondeterministic
computing machine be like?
plinko
Randomness vs. Determinism
… and free will
At heart, computers are
deterministic creatures.
Even randomly generated
numbers are explainable.
But we can still think outside the box:
What would a nondeterministic
computing machine be like?
plinko
CS 5 Reminders
• HW 3 - (4 problems)
due Sunday, 9/23 at midnight
due Monday, 9/24 at midnight
Reading: Class notes for week 3
HW3PR1) The virtual songwriter
Programs:
HW3PR2) Mathematical Menu
HW3PR3) Rock-Paper-Scissors
HW3PR4) Superlative computing
4
1 - sin(
L
9.8
a
2
) sin(x)
2
M/T sections
W/Th sections
CodeWarrior Tips
• Does your code look badly formatted when you submit ?
It’s the TAB character!
Option 1: use only spaces
Option 2: before coding, go to the edit…preferences menu
click on “fonts and tabs” and choose “tab inserts spaces”
• Are you annoyed by the code-completion pop-up box?
To remove it: go to the edit…preferences menu
click on “code completion”
unclick the “Automatic Invocation” checkbox
http://www.kurumi.com/roads/signmaker/signmaker.html
Initializing variables
String message = “The road untraveled”;
int road = H.in.nextInt();
Because message is given
an initial value here,
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.out.println( message );
message is guaranteed to
have a value when needed.
if
H.p(“What year were you born?
int y = H.ni();
The “if”
block
”);
if ( y == 1984 )
The “test” or “comparison”
{
H.pl(“You’re a Leaper!”);
}
else
{
this code happens only if the test is true
H.pl(“You’re Leapfree.”);
}
Choosing a road… if
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
Choosing a road… if
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
An “if” block
if ( road == 94 ) The “test” or “comparison” in parentheses.
{
message = “The road less traveled”;
}
The code in the “if” block is
executed only if the test is true.
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
Notice anything missing ?!?
Choosing a road… if
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
An “if” block
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
No semicolon after the test !
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
The road more traveled
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
The road less traveled
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
Taking both roads
Roads
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
if ( road == 10 )
{
message = “The road more traveled”;
}
H.pl( message );
The road untraveled
Or else
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
if ( road == 94 )
{
message = “The road less traveled”;
}
else
{
message = “The road more traveled”;
}
H.pl( message );
Or else
String message = “The road untraveled”;
H.p(“Choose a road: ”);
int road = H.ni();
An “else” block
if ( road == 94 )
{
message = “The road less traveled”;
}
else
NO “test” or “comparison” at all
{
message = “The road more traveled”;
The code in the “else” block is
}
executed in all other cases.
H.pl( message );
Download