18S-Section-Solution

# 18S-Section-Solution - CS106A Handout 18S April 21st 2010...

This preview shows pages 1–2. Sign up to view the full content.

CS106A Handout 18S Spring 2010 April 21 st , 2010 Section Solution Discussion Problem 1 Solution: Ostentatious Numbers private boolean isOstentatiousNumber( int n) { int sum = 0; int product = 1; int original = n; while (n > 0) { int digit = n % 10; sum += digit; product *= digit; n /= 10; } return Math.abs(original – sum * product) <= 10; } Discussion Problem 2 Solution: Tipping Point Probability public class ExcessCoinFlips extends ConsoleProgram { public void run() { println("Here's a program that keeps flipping fair coins until the"); println("difference between the number of heads and tails equals " + COIN_FLIP_THRESHOLD + "."); println("----------------------------------------------------------"); int numHeads = 0; int numTails = 0; while (Math. abs (numHeads - numTails) <= COIN_FLIP_THRESHOLD ) { if ((numHeads + numTails) % 20 == 0) { println(); print(" "); } if (rgen.nextBoolean()) { print("H"); numHeads++; } else { print("T"); numTails++; } pause( DELAY_BETWEEN_FLIPS ); } println();

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

18S-Section-Solution - CS106A Handout 18S April 21st 2010...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online