Transcript Lec3r.ppt

Homework 3
• Due
Sun., 9/21
( MT sections )
Mon., 9/22
( WTh sections )
at midnight
• Problems
http://www.cs.hmc.edu/courses/2003/fall/cs5/week_03/homework.html
• CS 5 website
http://www.cs.hmc.edu/courses/2003/fall/cs5/
• Submission problems?
Grade Concerns?
come see me or email me at [email protected]
• Tutors available -- contact information
• Tutors available -- lab places and times
Available in Parsons PC
Labs (the CIS classrooms)
Available in the LAC Lab
(Linde Activities Center)
You may also seek out tutors away from
the labs, by phone, in the dorms, etc.
Problem 3: Rock-Paper-Scissors
you might want to use the “Evens and Odds” code available at www.cs.hmc.edu/~dodds/cs5 as a starting point
overall structure first…
Problem 3
• choosing rock, paper, or scissors...
class CS5App
{
public static void main(String[] args)
{
Why is this value here?
H.pl(“Welcome to RPS!”);
H.p(“Be prepared for my”);
H.pl(“AVALANCHE strategy!”);
String cc = “”;
// cc is computer’s choice
How do we make the real choice?
Problem 3
• Let the user choose rock, paper, or scissors
H.pl(“What do you show -- ”);
H.p(“rock, paper, or scissors? ”);
String uc = H.nl();
Three things are
going on here !?!
Problem 3
• Let the user choose rock, paper, or scissors
H.pl(“What do you show -- ”);
H.p(“rock, paper, or scissors? ”);
String uc = H.nl();
Three things are
going on here !?!
Lots of
Cases!
• Both players have chosen -- what happens next ?
if
{
}
(
)
user chose “paper”
computer chose “rock”
Problem 2 -- Math Menu
• Menu of 6 options:
computer scientists tend to start
counting from 0, instead of 1…
Please choose one of these options:
(0)
(1)
(2)
(3)
(4)
(5)
A friendly (and random) message
Power computing!
Prof Saeta’s physics function (!*?%!)
Convert from seconds
Convert to seconds
Find the max and min of 4 different ints
Choose an option (0-5):
There are really seven cases to consider -- why?
Problem 2 -- getting started
you might want to use the “Moth Menu” code available at www.cs.hmc.edu/~dodds/cs5 as a starting point
• create the general structure of the program then fill in details
Outline of code:
problem decomposition…
Different ways of making choices
if … if … if …
if (choice == 0)
{
}
if (choice == 1)
{
}
if (choice == 2)
{
}
else
{
}
not necessarily
mutually exclusive
if … else
if … else if …
else if … else
if (choice == 0)
{
}
else if (choice == 1)
{
}
else if (choice == 2)
{
}
else
{
}
mutually exclusive blocks of code
Different ways of making choices
switch
switch (choice)
{
case 0:
{
break;
}
case 1:
{
break;
}
default:
{
}
}
mutually exclusive
blocks of code as long
as you use break !
Why is there no need for a
break in this last case?
Problem 2 -- option 0
• creating a random message…
(create the structure first, then details)
recurring theme
Math.random();
produces a double between 0 and 1 (excluding 1)
0
1
produces a double between 0 and 3 (excluding 3)
0
1
2
3
produces an int between 0 and 2 (inclusive)
0
1
2
3
produces an int between 1 and 3 (inclusive)
0
1
2
3
but, you need to store this random number somewhere!
Problem 2 -- option 2
What variables will we need?
Period of a pendulum (almost!)
Problem 2 -- option 2
How to compose these operations and functions together?
Period of a pendulum (almost!)
Problem 2 -- option 3
• from seconds
years, days, hours, minutes, and seconds
How many years are in 1,000,000,000 seconds?
31536000 sec/year
86400 sec/day
3600 sec/hour
60 sec/min
Problem 2 -- option 4
• from years, days, hours, minutes, and seconds
How many seconds are in 42 years?
seconds
Problem 2 -- option 5
Finding the max and min of four different ints.
problem outline:
Problem 2 -- option 5
/*
* Be sure to fill in this comment...
*/
import HMC.*;
class CS5App
{
public static void main(String[] args)
{
int a, b, c, d;
H.out.println(“Input four integers:”);
a = H.in.nextInt();
b = H.in.nextInt();
c = H.in.nextInt();
d = H.in.nextInt();
Input
Problem 2 -- option 5
// is a the smallest among them ?
Min
if (
)
{
H.out.println(“The smallest integer is ” + a);
}
// is b the smallest among them ?
if (
)
{
H.out.println(“The smallest integer is ” + b);
}
// and so on…
// with a similar construction for the max
How many comparisons will this take (in the worst case)?
Problem 1
• The Virtual Lyricist or, perhaps, conversationalist
class CS5App
{
public static void main(String[] args)
{
H.p(“Enter your favorite mascot: ”);
What’s the
effect of
this H.p?
String mascot = H.nl();
H.pl(“Here we go, ” + mascot);
H.pl(“Here we go!”);
}
}
Use at
least 4
variables
Problem 1
class CS5App
{
public static void main(String[] args)
{
H.p(“Enter your favorite mascot: ”);
String mascot = H.nl();
H.pl(“Here we go, ” + mascot);
H.pl(“Here we go!”);
H.p(“Enter an enemy mascot: ”);
String mascot = H.nl();
H.pl(“Here we go, ” + mascot);
H.pl(“Here we go!”);
}
}
What’s wrong here?!
Problem 1
class CS5App
{
public static void main(String[] args)
{
H.p(“Enter your favorite mascot: ”);
String mascot = H.nl();
declaring the
variable -- can
be done once!
H.pl(“Here we go, ” + mascot);
H.pl(“Here we go!”);
H.p(“Enter an enemy mascot: ”);
assigning the
variable -- can be mascot = H.nl();
done any time...
H.pl(“Here we go, ” + mascot);
H.pl(“Here we go!”);
}
}
using the variable -done any time...
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 !
use reasonable
variable names
/*
*
*
*
*
*
*
*/
Style
Author:
Date:
Homework
Problem
Comment:
introductory comment
me!
9/20/03
3
2
I spent 10 hours on this problem
(in binary)
leave space before
blocks of code
class CS5App
{
public static void main(String[] args)
{
H.p(“Enter your favorite mascot: ”);
align curly
braces that
correspond
String mascot = H.in.nextWord();
… more code here …
}
}
indent code evenly within
the surrounding braces
leave space between parts of
code that do different things