This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Lecture 4 A. Modification of Some Problems from the Last Lecture Question 1: Redo the following code which counts the number of digits in a whole number so that the while loop test is replaced by the more natural test number > 0 .This is more natural as it corresponds directly to the way we count things. Also be sure that you assign the number 0 to have one digit. Just consider the problem of eating all the cookies in a jar. If the jar is empty the boy gets to eat no cookies at all. The jar is nonempty if and only if the number of cookies in the jar satisfies the test number > 0 . While this condition holds true, the boy eats another cookie and increases the count of how many he has eaten by 1. Old Code: public static int countDigits(int number) { int numOfDigits = 1; while ( number/10 != 0 ) {numOfDigits++; number = number/10; // Lob off the last digit } //end of while loop block. return numOfDigits; } // end of method countDigits In the new code below, we also allow the user to enter the number whose digits are to be counted via the keyboard. New Code: Question1.java //Question1 import java.util.*; public class Question1 { public static void main(String args) {Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a whole number."); int number = keyboard.nextInt( ); int digits = CountDigits(number); System.out.printf("The number %d has %d digits.\n", number, digits); } // end of method main public static int CountDigits(int number) { int numOfDigits = ( number == 0 ) ? 1 : 0; while (number>0) {numOfDigits++; number = number/10; // Lob off the last digit } //end of while loop block. return numOfDigits; } // end of method countDigits } // end of class Question1 Question 2: Adjust Exercise 4.java from Lecture 3 so that it counts the number of sevens instead of the number of zeros. Old Code public class Exercise4 { public static void main(String args) { int n = 1020304050; int z = zeros(n); System.out.println("The original number is: " +n); System.out.println("The number of zeros is: " +z); } // end of main method public static int zeros( int n ) { if ( n == 0 ) return 0; else return ( ( n % 10 == 0 ) ? zeros( n/10 ) + 1 : zeros( n/10 ) ); } // end of zeros method }//end of class New Code: Question2.java //Question 2 import java.util.*; public class Question2 { public static void main(String args) {Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a whole number."); int n = keyboard.nextInt( ); int numSevens = countDigits(n); System.out.printf("The number %d contains %d sevens.\n", n, numSevens); } // end of method main public static int countDigits( int n )...
View
Full
Document
This note was uploaded on 01/17/2012 for the course ECE 203 taught by Professor Robincarr during the Fall '07 term at Drexel.
 Fall '07
 RobinCarr

Click to edit the document details