CS 5 Reminders Hw 3 - (3 problems) Take advantage of the tutors. Friday 8:00 am recitation section:...

Post on 25-Dec-2015

218 views 3 download

Tags:

Transcript of CS 5 Reminders Hw 3 - (3 problems) Take advantage of the tutors. Friday 8:00 am recitation section:...

CS 5 Reminders

• Hw 3 - (3 problems)

• Take advantage of the tutors.

• Friday 8:00 am recitation section: HW hints and Q&A

- their schedule is linked from the CS5 webpage

Linde Lab

Academic Computing Labs

M/T sections

W/Th sections

due Sunday, 9/19 at midnight

due Monday, 9/20 at midnight

Reading: Week 3’s sections of the online text

Hw3Pr1) The virtual songwriter

Hw3Pr2) Mathematical Menu

Hw3Pr3) Rock-Paper-Scissors

Programs:

Today

• Indecisive? CS 5 can help!

• String theory -- it’s not just for physicists.

• If Robert Frost had been a programmer...

• Java conjunctions

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.

Last time

Variables as storage spaces

String animal = “”, noise = “”;

H.pl(“Enter an animal and a noise it makes:”);

animal = H.nw(); // next wordnoise = H.nw();

type: String name: animal

type: String name: noise

Last time

Variables as storage spaces

String animal = “”, noise = “”;

H.pl(“Enter an animal and a noise it makes:”);

animal = H.nw(); // next wordnoise = H.nw();

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”);

type: String name: animal

type: String name: noise

This time

What is one thing you really hate?spam

Type three plural nouns related to that thing:Hit return after each one.tin canscamping tripsunwanted emails

What is your favorite color? blueWhat is your favorite poet? FrostWhat is the name of a significant other? Rita

First, some questions:Hw3Pr1) The virtual songwriter

Virtual Alanis

I Think-------

I think tin cans are a really huge problemI think camping trips are too much on my mindI think unwanted emails are bringing the world downBut what can you do?

Like a blue rain, beating down on meLike a Frost line, which won't let go of my brainLike Rita’s voice, it is in my headBlame it on spamBlame it on spamBlame it on spam

Either emulate this example, or adapt to suit your tastes...

Hw 3

Hw3Pr1) The virtual songwriter

The example programs are available from the “Files for download” link from the CS 5 webpage.

Hw 3

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 !

The example programs are available from the “Files for download” link from the CS 5 webpage.

Hw 3

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 !

The example programs are available from the “Files for download” link from the CS 5 webpage.

Option #2:get L, a, and x from the user and then output the results of plugging them

into this formula... !

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.”);

Straight-line code

straightforward code?

thread of execution

Hw3Pr2) The math menu

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;

byte b = 3;

short s = 4;

long l = 6;

holds from -128 to 127

holds from -32768 to 32767

holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

holds from -2,147,483,648 to 2,147,483,647

The Others: Java’s built-in types

int x = 5;

byte b = 3;

short s = 4;

long l = 6;

holds from -128 to 127

holds from -32768 to 32767

holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

holds from -2,147,483,648 to 2,147,483,647

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;

byte b = 3;

short s = 4;

long l = 6;

holds from -128 to 127

holds from -32768 to 32767

holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

holds from -2,147,483,648 to 2,147,483,647

String s = “this is a string of text”;

char c = ‘t’;

float f = 42.0;

double d = 42.0;

used to hold single characters

about 8 places of precision (vs. double’s 16)

Notice the single quotes

!

The Others: Java’s built-in types

boolean b = true;

int x = 5;

byte b = 3;

short s = 4;

long l = 6;

holds from -128 to 127

holds from -32768 to 32767

holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

holds either true or false

holds from -2,147,483,648 to 2,147,483,647

String s = “this is a string of text”;

char c = ‘t’;

float f = 42.0;

double d = 42.0;

used to hold single characters

about 8 places of precision (vs. double’s 16)

Notice the single quotes

!

What’s different about String ?

What it seems like:String cc

rock

String cc = “rock”;Code:

What’s different about String ?

What it really is:String cc

‘r’ ‘o’ ‘c’ ‘k’

reference

char char char char

What it seems like:String cc

rock

String cc = “rock”;Code:

a memory location: #42

#42 #43 #44 #45

#42

mental models!

Input: Know your type!

H.in.nextWord();H.in.nextLine();

H.in.nextInt();

H.in.nextLong();

H.in.nextChar();

H.in.nextDouble();

H.in.nextAnyChar();

return a String

return a char

returns an int

returns a long

returns a double

including whitespace

including whitespace

Input: Know your type!

H.in.nextWord();H.in.nextLine();

H.in.nextInt();

H.in.nextLong();

H.in.nextChar();

H.in.nextDouble();

H.in.nextAnyChar();

H.nw();H.nl();

H.nc();H.nanyc();

H.ni();

H.nlong();

H.nd();shortcuts !

return a String

return a char

returns an int

returns a long

returns a double

including whitespace

including whitespace

Variables are cheap...

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();

c = H.nd();

Hw3Pr2) The math menu

H.pl(“The solutions are ” + + “ and ” + );

Feel free to create variables as needed!

Math functions

Math.sqrt( 9 )

Math.sin( Math.PI )

Math.abs( -5 )

square root

absolute value

Hw3Pr2) Mathematical Menu

sine

Math.random()

Math.log( Math.E )

Math.exp( x )

Math.toRadians( 360 )

Full Java library: http://java.sun.com/j2se/1.4.2/docs/api/

natural log (ln)

angle conversion

e to the x

Well, this is random…

To store a random integer from 0 to 9 :

returns a double• greater or equal to 0.0• less than 1.0

To store a random integer from 1 to 3 :

Hw3Pr2) Mathematical Menu

Math.random()

Code to store a random double from 0.0 to 2.0 :

To store a random double from 1.0 to 2.0 :

including 0.0 excluding 2.0

including 1.0 excluding 2.0

inclusive

inclusive

Randomness vs. Determinism

Are there random numbers?

Output

RNG

Can a computer generate them?

A “black box” model of a random number generator.

Randomness vs. Determinism

Are there random numbers?

Can a computer generate them?

The RNG revealed.

// 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);

Output

Yes Not without help!

What would a nondeterministic computing machine be like?

True Randomness !

LavaRand’s lava lamps

using a chaotic physical system to seed random number

generators

(Patent 5,732,138: "Method for seeding a pseudo-random number generator with a cryptographic

hash of a digitization of a chaotic system.")

This has since been “improved”…

www.wired.com/wired/archive/11.08/random.html

an enthusiastic endorser

http://www.leapzine.com/hr/

three “random” lava lamp owners

& mom

Hw 3

Hw3Pr1) The virtual songwriter

Hw3Pr2) A mathematical menu

use switch to select among different cases

use the Moth Menu program to get you started…

Extra Credit: find the max and min of 4 different integers in as few comparisons as

possible ?!

check your numbers against the examples in the Hw !

The example programs are available from the “Files for download” link from the CS 5 webpage.

The code not taken: if

H.p(“What year were you born? ”); int y = H.ni();

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();

if ( y == 1984 ){ H.pl(“You’re a Leaper!”);}

The “test” or “comparison”

this code happens only if the test is true

The code not taken: if

What is missing here ?!?

The if block

H.p(“What year were you born? ”); int y = H.ni();

if ( ){ H.pl(“You’re a Leaper!”);}

The code not taken: if

What should this test be ?

H.p(“What year were you born? ”); int y = H.ni();

if ( y%4 == 0 ){ H.pl(“You’re a Leaper!”);}

The code not taken: if

is y divisible by 4 ?

two uses of equals !!

== vs. =

== indicates a comparison between two valuesIt’s usually used with if

= indicates an assignment from right to leftIt’s usually a stand-alone statement.

if ( road == 10 )

road = 10;

== vs. =

== indicates a comparison between two valuesIt’s usually used with if

= indicates an assignment from right to leftIt’s usually a stand-alone statement.

if ( road == 10 )

road = 10;

= “set equal !”

== “are they equal ?” a question, a test

a command, an assignment

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();

if ( y%4 == 0 ){ H.pl(“You’re a Leaper!”);}else{ H.pl(“You’re Leapfree.”);}

No test needed

The code not taken: if

The if block

The else block

Code run only when the previous if is false

A warning !

Inspired by Robert Frost and Route 94:

What if we want to go to Las Vegas?

String message;

int road = H.ni();

if ( road == 94 ){ message = “The road less traveled”;}

H.pl( message );

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.

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…”);

Brace yourself…

Why is this program too apologetic?

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…”);}

Brace yourself…

Watch out for your curly braces !

Or And Not

if ( year%4 == 0 && year > 1776 )“and”

if ( year%4 == 0 || year%4 == 2 )

“or”

if ( !(year%20 == 0) )

“not”

if ( year%20 != 0 )

“not equal”

What years match these conditions?

Presidential Problems

William H. Harrison -- He died in office on April 4, 1841. Abraham Lincoln -- He was shot and died April 15, 1865.James A. Garfield -- He was shot and died September 19, 1881.William McKinley -- He was shot and died September 14, 1901. Warren G. Harding -- He died in office August 2, 1923.Franklin D. Roosevelt -- He died in office April 12, 1945.John F. Kennedy -- He was shot and died November 22, 1963. Ronald Reagan -- He was shot, but survived...

Presidential Problems

William H. Harrison -- He died in office on April 4, 1841. Abraham Lincoln -- He was shot and died April 15, 1865.James A. Garfield -- He was shot and died September 19, 1881.William McKinley -- He was shot and died September 14, 1901. Warren G. Harding -- He died in office August 2, 1923.Franklin D. Roosevelt -- He died in office April 12, 1945.John F. Kennedy -- He was shot and died November 22, 1963. Ronald Reagan -- He was shot, but survived...

Determining leap years

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 switchint 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

Suppose month == 9 .

It jumps to the appropriate case

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

Suppose month == 9 .

It jumps to the appropriate case

It does everything up to the break

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

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.

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; }}

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?

“Quiz”(1) Assume it is NOT a leap year. This code still assigns

the wrong number of days to February. WHY? Can you fix it?

7 Jul 318 Aug 319 Sep 3010 Oct 3111 Nov 3012 Dec 31

1 Jan 312 Feb 283 Mar 314 Apr 305 May 316 Jun 30

Names:

if (x == 0){ H.pl(0);}if (x == 1){ H.pl(1);}else if (x == 2){ H.pl(2);}else{ H.pl(42);}

Write a switch statement that has the same behavior as the if / else if / else statements to the left:

switch (x){ case : {

}

3 cases and default.

}

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?

“Quiz”(1) Assume it is NOT a leap year. This code still assigns

the wrong number of days to February. WHY? Can you fix it?

7 Jul 318 Aug 319 Sep 3010 Oct 3111 Nov 3012 Dec 31

1 Jan 312 Feb 283 Mar 314 Apr 305 May 316 Jun 30

Names:

numDays is set to 28 right here…

but numDays is incorrectly reset to 31 right here!

initial values!

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?

“Quiz”(1) Assume it is NOT a leap year. This code still assigns

the wrong number of days to February. WHY? Can you fix it?

7 Jul 318 Aug 319 Sep 3010 Oct 3111 Nov 3012 Dec 31

1 Jan 312 Feb 283 Mar 314 Apr 305 May 316 Jun 30

Names:

initial values!

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”);}

Write a switch statement that has the same behavior as the if / else if / else statements to the left:

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!

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”);}

Write a switch statement that has the same behavior as the if / else if / else statements to the left:

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;

if ( month == 2 ){ numDays = 28;}else if ( month == 4 || month == 6 || month == 9 || month == 11 ){ numDays = 30;} else{ numDays = 31;}

Anthony Gatto ’91 IJA numbers

competition

// Sets the number of days in month month. (1-12)

if … else if … else if … elsecreates mutually

exclusive blocks of code

Perspective

Programmers are not measured by their ingenuity and their logic but by the

completeness of their case analysis.

- Alan Perliswriter of the first compiler

Hw 3

Hw3Pr1) The virtual songwriter

Hw3Pr2) A mathematical menu

Hw3Pr3) Rock-Paper-Scissors: the last great decision-maker

use switch to select among different cases

use the Moth Menu program to get you started…

use the Evens && Odds program to get you started…

check your numbers against the examples in the Hw !

The example programs are available from the “Files for download” link from the CS 5 webpage.

What’s different about String ?

intdoublelong...

String

Variable Types

basic types -- simple values

composite type -- made up of other values

Classes should start with a capital letter.

String is a class.

Don’t use == with Strings !

== compares variables’ immediate contents

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!

if ( uc.equals(cc) ){ 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!”);}

String cc =

String uc = H.nl(); Use .equals to compare strings !

Use .equals

randomly chosen “rock” “paper” or “scissors”

.equals compares variables’ “true” contents

Lab Today

• I’d suggest starting on Problem 2 or Problem 3

to try out switch && if / else if / else statements

A-J: Mac Lab M-Z: PC Lab

Hw3Pr1) The virtual songwriter

Hw3Pr2) A mathematical menu

Hw3Pr3) Rock-Paper-Scissors: the last great decision-maker

use switch to select among different cases

use the Moth Menu program to get you started…

use the Evens && Odds program to get you started…

check your numbers against the examples in the Hw !

The example programs are available from the “Files for download” link from the CS 5 webpage.

What’s different about String ?

What it really is:String cc

‘p’ ‘a’ ‘p’ ‘e’ ‘r’

reference

char char char char char

What it seems like:String cc

paper

String cc = “paper”;Code:

a memory location: #42

#42 #43 #44 #45 #46

#42

String cc = “paper”;String uc = H.nl(); and the user types paper

String cc

paper

String uc

paper

What it seems like:

cc == uc looks true ?!?

Don’t use == with Strings !

String cc = “paper”;String uc = H.nl(); and the user types paper

What’s really happening:

cc == uc looks true ?!?

Don’t use == with Strings !

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

reference

reference

String cc

String uc

a memory location: #42

#42 #43 #44 #45 #46#42

a memory location: #1000

#1000 #1001 #1002 #1003 #1004#1000

String cc = “paper”;String uc = H.nl(); and the user types paper

What’s really happening:

cc.equals(uc) is true

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

reference

reference

String cc

String uc

a memory location: #42

#42 #43 #44 #45 #46#42

a memory location: #1000

#1000 #1001 #1002 #1003 #1004#1000

Use .equals

The Others

String s = “this is a string of text”;

double d = 42.0;

int x = 5;

char c = ‘t’;

boolean b = true;

float f = 42.0;

used to hold single characters

about 8 places of precision (vs. double’s 16)

byte b = 3;

short s = 4;

long l = 6;

holds from -128 to 127

holds from -32768 to 32767

holds from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

holds either true or false

holds from -2,147,483,648 to 2,147,483,647

String message;

int road = H.in.nextInt();

if ( road == 94 ){ message = “The road less traveled”;}if ( road == 10 ){ message = “The road more traveled”;}

H.out.println( message );

Initializing variables

What’s wrong here (potentially) ?

Presidential Problems

1840, William H. Harrison -- He died in office on April 4, 1841. 1860, Abraham Lincoln -- He was shot and died April 15, 1865.1880, James A. Garfield -- He was shot and died September 19, 1881.1900, William McKinley -- He was shot and died September 14, 1901. 1920, Warren G. Harding -- He died in office August 2, 1923.1940, Franklin D. Roosevelt -- He died in office April 12, 1945.1960, John F. Kennedy -- He was shot and died November 22, 1963. 1980, Ronald Reagan -- He was shot, but survived...

And leap years?

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

{

}

braces are safe everywhere

{

}

braces are critical here

Good news for presidents...

Election year

Olympic year

Stopping along the way

The “if” block

The “test” or “comparison”

this code happens only if the test is true

String message;

int road = H.in.nextInt();

if ( road == 94 ){ message = “The road less traveled”;}

if ( road == 10 ){ message = “The road more traveled”;}

H.out.println( message );

What’s different about String ?

Detail:String my

‘p’ ‘a’ ‘p’ ‘e’ ‘r’

reference

char char char char char

Abstraction:String my

paper

String my = “paper”;Code:

Classes are both bad and good

String my = “paper”;String your = H.in.nextLine(); The user types paper

Why bad?

== compares variables’ immediate contents

Detail:

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

‘p’ ‘a’ ‘p’ ‘e’ ‘r’char char char char char

my == your is false !

reference

reference These two references are different!

String my

String your

String my

paper

String your

paper

Abstraction:my == your looks true...

Why Good? Classes come with lots of built-in capabilities.

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!”);}

String my = “paper”;

String your = H.in.nextLine();

Use .equals with Strings

HW3PR3) Rock-Paper-Scissors

Code Warrior

• If you’re using an old copy

• Running CW from KATO

you will need the old Java stationery

you will need Novell’s “client 32” & KeyAccess

the instructions are at http://www.hmc.edu/comp/doc/

Win2000, Windows ME seem to work (mostly) so far...

there’s a lengthy one-time download

starting to seem less fierce...

Using Code Warrior

• your project file.

HW2PR4

HW2PR4.mcp

CS5App

CS5App.java

GridMain

GridMain.java

Open

• Submit your .java file.

Under windows: you need to show “all files” to view this !

always start with these!

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();

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 ” + );

Consider what variables would be useful…

Comparisons

and

or

not equal

not

== vs =Pisces -- tie in with leap year ?

Today

• Indecisive? CS 5 can help!

• String theory -- not just for physicists.

• If Robert Frost had been a programmer...

• Java conjunctions

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.

What’s different about String ?

Detail:String s

… ‘1’ ‘0’ ‘0’ ‘0’ ‘ ’ ‘W’ ‘ ’ ‘t’

reference

char char char char char char char char

Abstraction:String s

hello

String s = “1000 W that a P is W”;Code:

Randomness vs. Determinism

At heart, computers are deterministic creatures.

… and free will

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

At heart, computers are deterministic creatures.

… and free will

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)M/T sections

W/Th sections

due Sunday, 9/23 at midnight

due Monday, 9/24 at midnight

Reading: Class notes for week 3

HW3PR1) The virtual songwriter

HW3PR2) Mathematical Menu

HW3PR3) Rock-Paper-Scissors

HW3PR4) Superlative computing

Programs:

4

1 - sin( ) sin(x)2

L

9.8

a2

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

String message = “The road untraveled”;

int road = H.in.nextInt();

if ( road == 94 ){ message = “The road less traveled”;}if ( road == 10 ){ message = “The road more traveled”;}

H.out.println( message );

Initializing variables

message is guaranteed to have a value when needed.

Because message is given an initial value here,

http://www.kurumi.com/roads/signmaker/signmaker.html

if

H.p(“What year were you born? ”); int y = H.ni();

if ( y == 1984 ){ H.pl(“You’re a Leaper!”);}else{ H.pl(“You’re Leapfree.”);}

The “if” block

The “test” or “comparison”

this code happens only if the test is true

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

An “if” block

The “test” or “comparison” in parentheses.

The code in the “if” block is executed only if the test is true.

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 );

Notice anything missing ?!?

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 );

No semicolon after the test !

An “if” block

Taking both roads

RoadsString 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

RoadsString 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

RoadsString 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

RoadsString 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

RoadsString 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

RoadsString 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();

if ( road == 94 ){ message = “The road less traveled”;}else{ message = “The road more traveled”;}

H.pl( message );

An “else” block

NO “test” or “comparison” at all

The code in the “else” block is executed in all other cases.