cs 5 reminders hw 3 - (3 problems) take advantage of the tutors. friday 8:00 am recitation section:...

102
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:

Upload: hilary-evans

Post on 25-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 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

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:

Page 2: 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

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.

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

Hw 3

Hw3Pr1) The virtual songwriter

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

Page 8: 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

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.

Page 9: 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

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... !

Page 10: 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

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

Page 11: 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

The Others: Java’s built-in types

int x = 5; holds from -2,147,483,648 to 2,147,483,647

Page 12: 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

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

Page 13: 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

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

!

Page 14: 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

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

!

Page 15: 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

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

!

Page 16: 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

What’s different about String ?

What it seems like:String cc

rock

String cc = “rock”;Code:

Page 17: 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

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!

Page 18: 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

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

Page 19: 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

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

Page 20: 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

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!

Page 21: 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

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

Page 22: 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

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

Page 23: 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

Randomness vs. Determinism

Are there random numbers?

Output

RNG

Can a computer generate them?

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

Page 24: 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

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?

Page 25: 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

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

Page 26: 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

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.

Page 27: 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

The code not taken: if

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

Page 28: 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

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

Page 29: 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

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

Page 30: 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

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 ?

Page 31: 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

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

Page 32: 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

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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

Page 36: 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

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

Page 37: 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

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.

Page 38: 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
Page 39: 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

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?

Page 40: 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

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 !

Page 41: 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

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?

Page 42: 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

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

Page 43: 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

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

Page 44: 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

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

Page 45: 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

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

Page 46: 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

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

Page 47: 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

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

Page 48: 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

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:

Page 49: 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

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.

}

Page 50: 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

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!

Page 51: 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

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!

Page 52: 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

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!

Page 53: 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

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

Page 54: 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

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

Page 55: 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

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

Page 56: 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

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.

Page 57: 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

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.

Page 58: 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

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!

Page 59: 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

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

Page 60: 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

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.

Page 61: 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

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

Page 62: 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

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 !

Page 63: 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

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

Page 64: 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

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

Page 65: 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

The Others

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

double d = 42.0;

int x = 5;

Page 66: 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

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

Page 67: 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

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

Page 68: 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

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

Page 69: 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

Switch

switch ( month ){ case 2: numDays = 29; break;

case 4: case 6: case 9: case 11: numDays = 30; break;

default: numDays = 31; break;}

Page 70: 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

Suppose month == 9 .

it jumps to that case

it does everything up to the break

it then jumps to the end of the switch

Page 71: 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

0.480017049832311

Page 72: 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

{

}

braces are safe everywhere

{

}

braces are critical here

Good news for presidents...

Election year

Olympic year

Page 73: 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

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

Page 74: 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
Page 75: 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
Page 76: 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

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:

Page 77: 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

Classes are both bad and good

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

Why bad?

== compares variables’ immediate contents

Page 78: 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

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

Page 79: 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

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

Page 80: 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

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

Page 81: 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

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!

Page 82: 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

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…

Page 83: 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

Comparisons

and

or

not equal

not

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

Page 84: 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

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.

Page 85: 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

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:

Page 86: 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

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

Page 87: 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

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

Page 88: 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

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

Page 89: 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

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

Page 90: 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

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

Page 91: 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

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

Page 92: 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

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

Page 93: 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

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 ?!?

Page 94: 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

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

Page 95: 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

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

Page 96: 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

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

Page 97: 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

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

Page 98: 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

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

Page 99: 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

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

Page 100: 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

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

Page 101: 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

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

Page 102: 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

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.