13-Even-More-Idioms - CS106A Handout 13 Spring 2011 April...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS106A Handout 13 Spring 2011 April 11 th , 2011 Even More Idioms, Booleans, and Helper Methods Computing the Sum of Digits /** * File: DigitSum.java * ------------------- * Short program that extracts a number from the user, and * provided he/she bevahes and enters a positive number, the * program uses a while loop to add up all the digits to * compute the digit sum. */ import acm.program.*; public class DigitSum extends ConsoleProgram { public void run() { int number = readInt("Enter a number greater than 0: "); if (number <= 0) { println("Your response should have been positive."); println("You're no fun to play number games with."); return ; // end prematurely without continuing } int digitSum = 0; int n = number; while (n > 0) { int digit = n % 10; digitSum += digit; n /= 10; } println("The sum of the digits making up " + number + " is " + digitSum + "."); println("Thanks for playing. :)"); } } Why for and while ? Why both? Yes, both are looping constructs, and technically, whatever can be done with one can be done with the other. But programmers use for loops when it’s blatantly obvious before the looping even starts how many iterations there will be. The while loop is used when it’s not immediately clear how long looping will go on. If you don’t like the user’s response, you can return and end the program early. There are more graceful ways to grant the user a second (and third, and fourth) chance to respond properly, but this certainly works. Use a while loop to identify, incorporate, and discard of the ones digit until everything’s become a ones digit and been accounted for in the digit sum. Note the use of % and integer division. Prime Factorization Sum (Challenging!) /** * File: PrimeFactorizationSum.java * -------------------------------- * This program computes the sum of all of the prime * numbers making up the prime factorization of a number. * If a prime contributes to the prime factorization more * than once, then it contributes that number of times to * the sum. * * This is algorithmically a step up from where you've been so far. */ import acm.program.*; public class PrimeFactorizationSum extends ConsoleProgram { public void run() { print("Here we print the prime factorization sum "); println("of any positive number greater than 1."); int number; // declare here so available beyond while loop while ( true ) { number = readInt("Please enter a number greater than or equal to 1: "); if (number >= 1) break ; println("You entered " + number + ", but that's just too small."); } int n = number; int sum = 0; int factor = 2; while (n > 1) { if (n % factor == 0) { sum += factor; n /= factor; } else { factor++; } } println("The prime factorization sum of " + number + " is " + sum + "."); println("Neat, huh?"); println(); println("All done!"); } } This while loop looks like it’s doomed to cycle forever and ever. Fortunately, there’s a check within the body of the while loop that makes it possible to break out. This...
View Full Document

This note was uploaded on 05/28/2011 for the course CS 106A taught by Professor Sahami,m during the Spring '08 term at Stanford.

Page1 / 7

13-Even-More-Idioms - CS106A Handout 13 Spring 2011 April...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online