Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/21 Mon., 9/22 Problems

Similar presentations


Presentation on theme: "Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/21 Mon., 9/22 Problems"— Presentation transcript:

1 Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 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 dodds@cs.hmc.edu Submission problems? Grade Concerns?

2 Tutors available -- contact information

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

4 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

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 computer’s choice How do we make the real choice? Why is this value here?

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

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

9 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

10 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 { }

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

12 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! 01 0123 produces a double between 0 and 3 (excluding 3) produces an int between 0 and 2 (inclusive) 0123 produces an int between 1 and 3 (inclusive) 0123

13 Problem 2 -- option 2 Period of a pendulum (almost!) What variables will we need?

14 Problem 2 -- option 2 Period of a pendulum (almost!) How to compose these operations and functions together?

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 outline: Problem 2 -- option 5 Finding the max and min of four different int s.

18 /* * 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

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

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!”); } Use at least 4 variables What’s the effect of this H.p ?

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!”); } What’s 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.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

24 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


Download ppt "Homework 3 Due ( MT sections ) ( WTh sections ) at midnight Sun., 9/21 Mon., 9/22 Problems"

Similar presentations


Ads by Google