Extra Exercises Source: Suggested but not selected midterm questions CSE 201 – Elementary Computer Programming 1 Checking if string has digit Given a string variable, inputName, check to see if it contains any numbers. If it contains any numbers, output an error message. The boolean Character.isDigit(char) method can be used to check a character variable to see if it is a digit. CSE 201 – Elementary Computer Programming 2 Reverse Guessing Game Write a program which plays the random number game, the user chooses the secret number between 1 and 10, the computer will make "guesses" by choosing random numbers. User will enter ‘b’ is secret number is bigger than guess, and ‘s’ if secret number is smaller than guess, and ‘c’ when correct. Sample: The code 9 int randomNumber = (int)(10 * s Math.random ()) + 1; 1 will generate a random number b between 1 and 10 and assign the 8 value to the variable s randomNumber. 3 c Computer guessed the secret number in 4 guesses CSE 201 – Elementary Computer Programming 3 Value of expressions Given the following variable declarations int i = 10, j = 3, k = -1; boolean b = true; String str = "Elephant"; char c = 'e'; (A) evaluate expressions (a)-(f): a. (i < j) || b b. str.charAt(j) c. str.length() == 7 d. str.substring(0,4) e. str.indexOf(c) f. str.indexOf('c') CSE 201 – Elementary Computer Programming 4 Errors and Types Given the following variable declarations int i = 10, j = 3, k = -1; boolean b = true; String str = "Elephant"; char c = 'e'; determine which of the following statements (g)-(j) will produce an error. If a statement does produce an error, what kind? Briefly explain in one or two sentences why it produces an error. a. (i < j) || b b. str.charAt(j) c. str.length() == 7 d. str.substring(0,4) e. str.indexOf(c) f. str.indexOf('c') CSE 201 – Elementary Computer Programming 5 Code Writing Fix the code so that it works according to the supplied documentation (in comments) /* Calculates sum of all of the even * numbers from 0 to limit inclusive. * Assumes limit is initialized above * to a positive integer. */ int i = 0; int sum = 0; while (i != limit) { sum = sum + i; i+=2; } CSE 201 – Elementary Computer Programming 6 Debug According to Description //This program counts the number of special characters //(&, @, and $) in a string input. Scanner in = new Scanner(System.in); String userInput; System.out.print("Please enter a string: "); userInput = in.nextLine(); int stringSize = userInput.length(); int counter = 0, numSpecialChars = 0; char letter; while (counter < stringSize) { letter = userInput.charAt(counter); if ((letter == '&') || (letter == '@') || (letter == '$')) { numSpecialChars = numSpecialChars + 1; } } System.out.println("Number of special characters is " + numSpecialChars ); CSE 201 – Elementary Computer Programming 7 If else Are the two set of statements equivalent. Explain your answer in detail to get any credit. You can assume x and y are integers input by the user . if( x = = y) if( x = = y) x = 5; x = 5; else if( x != y) x = 7; x = 7; CSE 201 – Elementary Computer Programming 8