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