A1261658708vMEzT - PowerPoint PPT Presentation

About This Presentation
Title:

A1261658708vMEzT

Description:

How to compose these operations and functions together? Problem 2 ... assigning the variable -- can be done any time... using the variable -- done any time... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 25
Provided by: zachd
Category:

less

Transcript and Presenter's Notes

Title: A1261658708vMEzT


1
Homework 3
Sun., 9/21
( MT sections )
  • Due

at midnight
Mon., 9/22
( WTh sections )
  • Problems

http//www.cs.hmc.edu/courses/2003/fall/cs5/week_0
3/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 dodds_at_cs.hmc.edu
2
  • Tutors available -- contact information

3
  • 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.
4
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
5
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 computers choice
Why is this value here?
How do we make the real choice?
6
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 !?!
7
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 (
)
computer chose rock
user chose paper
8
Problem 2 -- Math Menu
computer scientists tend to start counting from
0, instead of 1
  • Menu of 6 options
  • Please choose one of these options
  • (0) A friendly (and random) message
  • (1) Power computing!
  • (2) Prof Saetas 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?
9
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

problem decomposition
Outline of code
10
Different ways of making choices
if else if else if else
if if if
if else
if (choice 0) if (choice 1) if
(choice 2) else
if (choice 0) else if (choice
1) else if (choice 2) else
not necessarily mutually exclusive
mutually exclusive blocks of code
11
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?
12
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!
13
Problem 2 -- option 2
What variables will we need?
Period of a pendulum (almost!)
14
Problem 2 -- option 2
How to compose these operations and functions
together?
Period of a pendulum (almost!)
15
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
16
Problem 2 -- option 4
  • from years, days, hours, minutes, and seconds
    seconds

How many seconds are in 42 years?
17
Problem 2 -- option 5
Finding the max and min of four different ints.
problem outline
18
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
19
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
How many comparisons will this take (in the worst
case)?
20
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!)
Whats the effect of this H.p?
Use at least 4 variables
21
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!)
Whats wrong here?!
22
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...
23
Input Know your type!
H.in.nextWord() H.in.nextLine()
H.nw() H.nl()
return a String
including whitespace
H.in.nextChar()
H.nc() H.nanyc()
return a char
H.in.nextAnyChar()
including whitespace
H.in.nextInt()
H.ni()
returns an int
H.in.nextLong()
H.nlong()
returns a long
H.in.nextDouble()
H.nd()
returns a double
shortcuts !
24
use reasonable variable names
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

introductory comment
leave space before blocks of code
align curly braces that correspond
leave space between parts of code that do
different things
indent code evenly within the surrounding braces
Write a Comment
User Comments (0)
About PowerShow.com