/* * CSE 142 review exercises. We emphasized the following "toolkit" * of topics that are useful in solving programming problems: * print/println * for loop * while loop * Scanner * if/else if/else * Random * arrays * parameters * returns * cumulative sum * fencepost loop * boolean zen * class basics and inheritance (see Sloth.java) */ import java.util.*; import java.io.*; public class ReviewSession142 { public static void main(String[] args) throws FileNotFoundException { // cumulativeSum cumulativeSum(); // collapseSpaces collapseSpaces("poem.txt"); // playWar Random rand = new Random(); playWar(rand); // toUpperTarget String[] words = new String[] {"hello","my","name","is","my"}; System.out.println(Arrays.toString(words)); toUpperTarget(words, "my"); System.out.println(Arrays.toString(words)); // toCamelCase String s1 = "heLLo"; //Hello String s2 = "OMNOMNOM"; //Omnomnom String s3 = "guava"; String s4 = ""; System.out.println(toCamelCase(s1)); System.out.println(toCamelCase(s2)); System.out.println(toCamelCase(s3)); System.out.println(toCamelCase(s4)); } /* * Write a class that prompts the user for a number repeatedly. * When the user types -1, the program stops prompting for a * number and prints the sum of all numbers that have been * typed (excluding the -1). * * This method demonstrates the following tools: * while loop * Scanner on the console * cumulative sums * printing */ public static void cumulativeSum() { Scanner console = new Scanner(System.in); int input = 0; int sum = 0; while (input != -1) { sum += input; // this must come BEFORE the println, otherwise -1 is added System.out.print("Next number?"); input = console.nextInt(); } System.out.println("Sum = " + sum); } /* * Write a method collapseSpaces that takes a file name * and prints out the contents of the file to the console. * All white space within a line, however, will be reduced * to one space. Line breaks are preserved. * * Example: * It was the best of times, * it was the worst of times, * it was the age of wisdom, * * it was the age of foolishness, * it was the epoch of belief, * it was the epoch * of incredulity, * * Should become: * It was the best of times, * it was the worst of times, * it was the age of wisdom, * * it was the age of foolishness, * it was the epoch of belief, * it was the epoch * of incredulity, * * This method demonstrates the following tools: * while loop * Scanner on a file (file input) * line-based and token-based processing * printing */ public static void collapseSpaces(String fileName) throws FileNotFoundException { Scanner input = new Scanner(new File(fileName)); while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); while (lineScan.hasNext()) { String word = lineScan.next(); System.out.print(word + " "); } System.out.println(); } } /* * Write a method playWar that plays a game of war. * It chooses a random number between 2 and 14 for * both player 1 and player 2 and then reports which * player won (i.e. had a higher choice of number). * Options are: "Player 1 won!" * "Player 2 won!" * "Players tied!" * * This method demonstrates the following tools: * Random * if/else structure * use of helper method to reduce redundancy */ public static void playWar(Random rand) { int playerOne = generate(rand); int playerTwo = generate(rand); // Always use proper if/else if/else construction; // "if/else if/else if" would be incorrect. if (playerOne > playerTwo) { System.out.println("Player 1 won!"); } else if (playerTwo > playerOne) { System.out.println("Player 2 won!"); } else { System.out.println("Players tied!"); } } /* * Generates a random integer between 2 and 14. */ public static int generate(Random rand) { return rand.nextInt(13) + 2; } /* Write a method toUpperTarget that takes an array * of strings and a target string and changes any * strings in the array that are the same as the * target to be all upper case. * * For example, if arr1 stores * {"hello","my","name","is","my"} * and we call toUpperTarget(arr1, "my"), then arr1 will store * {"hello","MY","name","is","MY"} * * This method demonstrates the following tools: * for loop * array traversal * string equality using .equals() * reference semantics (array as a parameter not returned) */ public static void toUpperTarget(String[] sentence, String target) { for (int i = 0; i < sentence.length; i++) { if (sentence[i].equals(target)) { sentence[i] = sentence[i].toUpperCase(); } } // We don't need to return the sentence array because arrays // are passed by reference, meaning that changing it here // changes it anywhere. } /* * Write a method toCamelCase that takes a string and * transforms it into camel case (first letter * capitalized, the rest lower case). * * This method demonstrates the following tools: * string operations * checking edge cases (string of length 0) */ public static String toCamelCase(String word) { if (word.length() > 0) { return Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase(); } return word; } }