{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


13-Even-More-Idioms - CS106A Handout 13 April 11th 2011...

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full Document Right Arrow Icon
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.");
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}