SOURCE CODE FILES FOR THE Structured EXAMPLES ============================================================== /** * Project Doubling1 * Class Doubling * * Print the powers of 2 that * are less than or equal to 100. */ public class Doubling { public static void main(String[] args) { int x = 1; // Keep printing numbers while x // is less than or equal to 100. // Double number at the end of each // pass through the while loop. while(x <= 100) { System.out.print(x + " "); x *= 2; } // Go to a new line for next run. System.out.println(); } } // Output: 1 2 4 8 16 32 64 ============================================================== /** * Project DaysOfWeek * Class DaysOfWeek * * Input a daycode and print the corresponding day * of the week. Use if..else statements to find * the day of the week from the day code. */ import javax.swing.JOptionPane; public class DaysOfWeek { public static void main(String[] args) { int daycode; // Show input dialog. daycode = Integer.parseInt( JOptionPane.showInputDialog("Enter a day code."); // Obtain day of week for given daycode. if(daycode == 1) System.out.println("Sunday"); else if(daycode == 2) System.out.println("Monday"); else if(daycode == 3) System.out.println("Tuesday"); else if(daycode == 4) System.out.println("Wednesday"); else if(daycode == 5) System.out.println("Thursday"); else if(daycode == 6) System.out.println("Friday"); else if(daycode == 7) System.out.println("Saturday"); else System.out.println("Bad day code"); } } // Sample session. The 3 is entered by the user. Enter a day code: 3 Tuesday ============================================================== /** * Project SumTo100 * Class SumTo100 * * Compute the sum of the numbers from * 1 to TARGET, where TARGET is an int * constant. */ public class SumTo100 { public static void main(String[] args) { int i, sum = 0; final int TARGET = 100; for(i = 1; i <= TARGET; i++) sum += i; System.out.println("The sum of the first " + TARGET + " numbers is " + sum + "."); } } // Output: The sum of the first 100 numbers is 5050. ================================================================ /** * Project Doubling2 * Class Doubling * Print the powers of 2 that * are less than or equal to 100. */ public class Doubling { public static void main(String[] args) { // This for loop is equivalent to // while loop in the Doubling1 project. for(int x = 1; x <= 100; x *= 2) System.out.print(x + " "); // Go to a new line for next run. System.out.println(); } } // Output: 1 2 4 8 16 32 64 ============================================================== /** * Project IWillNot * Class Repeater * Repeat a sentence multiple times. */ import javax.swing.JOptionPane; public class Repeater { public static void main(String[] args) { String sentenceToRepeat; int timesToRepeat, i; sentenceToRepeat = JOptionPane.showInputDialog( "Enter sentence to repeat."); timesToRepeat = Integer.parseInt( JOptionPane.showInputDialog("Enter times to repeat.")); for(i = 1; i <= timesToRepeat; i++) System.out.println(i + ". " + sentenceToRepeat); } } // Sample session. The user enters "I will not talk in class." and 10 in the input dialogs. Enter sentence to repeat: I will not talk in class. Enter times to repeat: 10 1. I will not talk in class. 2. I will not talk in class. 3. I will not talk in class. 4. I will not talk in class. 5. I will not talk in class. 6. I will not talk in class. 7. I will not talk in class. 8. I will not talk in class. 9. I will not talk in class. 10. I will not talk in class. ==============================================================