public static void main(String[] args) { int inputNumber; // One of the integers input by the user. int sum; // The sum of the positive integers. int count; // The number of positive integers. double average; // The average of the positive integers. /* Initialize the summation and counting variables. */ sum = 0; count = 0; /* Read and process the user's input. */ TextIO.put("Enter your first positive integer: "); inputNumber = TextIO.getlnInt(); while (inputNumber != 0) { sum += inputNumber; // Add inputNumber to running sum. count++; // Count the input by adding 1 to count. TextIO.put("Enter your next positive integer, or 0 to end: "); inputNumber = TextIO.getlnInt(); } /* Display the result. */ if (count == 0) { TextIO.putln("You didn't enter any data!"); } else { average = ((double) sum) / count; TextIO.putln(); TextIO.putln("You entered " + count + " positive integers."); TextIO.putf("Their average is %1.3f.\n", average); } } // end main()
/** * Debug ME! Use the unit tests to reverse engineer how this method should work. Hint: Fix the * formatting (shift-cmd-F) to help debug the following code * * @param list * @param maximum */ public static void list(String[] list, int maximum) { int i; // for ( i = 0 ; i <= maximum; ); { TextIO.putln( "" + i + ". " + list[i]);} if (maximum > 0) { // show some of items for (i = 0; i < maximum; i++) TextIO.putln("" + (i + 1) + ". " + list[i]); } else { // show all items for (i = 0; i < list.length; i++) TextIO.putln("" + (i + 1) + ". " + list[i]); } }
public static void main(String[] args) { TextIO.putln("This program will print out 3N+1 sequences"); TextIO.putln("for starting values that you specify."); TextIO.putln(); int K; // Starting point for sequence, specified by the user. do { TextIO.putln("Enter a starting value;"); TextIO.put("To end the program, enter 0: "); K = TextIO.getInt(); // get starting value from user if (K > 0) // print sequence, but only if K is > 0 print3NSequence(K); } while (K > 0); // continue only if K > 0 } // end main
public static void main(String[] args) { int eggs; // Number of eggs, input by user. int gross; // How many gross in that number of eggs? int aboveGross; // How many eggs are left over, above an // integral number of gross? This number // can be computed as eggs % 144, and is // in the range 0 to 143. This number will // be divided into dozens and extras. int dozens; // How many dozens in aboveGross? int extras; // How many eggs are left over, above integral // numbers of gross and dozens? TextIO.put("How many eggs do you have? "); eggs = TextIO.getlnInt(); gross = eggs / 144; aboveGross = eggs % 144; dozens = aboveGross / 12; extras = aboveGross % 12; TextIO.put("Your number of eggs is "); TextIO.put(gross); TextIO.put(" gross, "); TextIO.put(dozens); TextIO.put(" dozen, and "); TextIO.put(extras); TextIO.putln(); } // end main()
/** * Example output if user enters 10: Max? 1 + 3 + 5 + 7 + 9 = 25 25 = 9 + 7 + 5 + 3 + 1 * * <p>Example output if user enters 11: Max? 1 + 3 + 5 + 7 + 9 + 11 = 36 36 = 11 + 9 + 7 + 5 + 3 + * 1 */ public static void main(String[] args) { TextIO.putln("Max?"); int max = TextIO.getlnInt(); int sum = 0; for (int i = 0; i <= max; i++) { if (i % 2 == 1) { sum = sum + i; if (i == max || i == max - 1) TextIO.put(i + " = "); else TextIO.put(i + " + "); } } TextIO.putln(sum); TextIO.put(sum + " = "); for (int j = max; j > 0; j--) { if (j % 2 == 1) { if (j == 1) TextIO.put(j); else TextIO.put(j + " + "); } } } // end of main method
/** @param args */ public static void main(String[] args) { // Formatted output double third = 1.0 / 3; TextIO.put("One third is "); TextIO.put(third); TextIO.putf(", or %.2f", third); TextIO.putln(" OK?"); // A program that prints out if a number is odd or even // However it lies for some values... boolean oops = false; int a = 0; while (a <= 10) { oops = a >= 4 && a <= 8; TextIO.put(a + " is "); if ((a % 2 == 1) || oops) TextIO.putln("Odd"); else TextIO.putln("Even"); a++; } }
// Lecture 7 example // Print 'You Win' if the first and last letter are the same public static void main(String[] args) { TextIO.put("Enter a string"); String s = TextIO.getln(); // ask the string to create a new string // change s to point to (hold the memory address of) // the newly created string s = s.toLowerCase(); char first = s.charAt(0); char last = s.charAt(s.length() - 1); if (first == last) TextIO.putln("You win!"); }
/** * print3NSequence prints a 3N+1 sequence to standard output, using startingValue as the initial * value of N. It also prints the number of terms in the sequence. The value of the parameter, * startingValue, must be a positive integer. */ static void print3NSequence(int startingValue) { int N; // One of the terms in the sequence. int count; // The number of terms found. int onLine; // The number of terms that have been output // so far on the current line. N = startingValue; // Start the sequence with startingValue; count = 1; // We have one term so far. TextIO.putln("The 3N+1 sequence starting from " + N); TextIO.putln(); TextIO.put(N, 8); // Print initial term, using 8 characters. onLine = 1; // There's now 1 term on current output line. while (N > 1) { N = nextN(N); // compute next term count++; // count this term if (onLine == 5) { // If current output line is full TextIO.putln(); // ...then output a carriage return onLine = 0; // ...and note that there are no terms // on the new line. } TextIO.putf("%8d", N); // Print this term in an 8-char column. onLine++; // Add 1 to the number of terms on this line. } TextIO.putln(); // end current line of output TextIO.putln(); // and then add a blank line TextIO.putln("There were " + count + " terms in the sequence."); } // end of Print3NSequence
public void Write_Line(Integer_Template.Integer i) { TextIO.putln(((Std_Integer_Realiz.Integer) i).val); }
public void Write_Line(Char_Str_Template.Char_Str s) { TextIO.putln(((Std_Char_Str_Realiz.Char_Str) s).val); }