80 likes | 257 Views
Extra Exercises. Source: Suggested but not selected midterm questions. 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.
E N D
Extra Exercises Source: Suggested but not selected midterm questions
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.
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: • 9 • s • 1 • b • 8 • s • 3 • c • Computer guessed the secret number in 4 guesses The code int randomNumber = (int)(10 * Math.random ()) + 1; will generate a random number between 1 and 10 and assign the value to the variable randomNumber.
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')
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')
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; }
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 );
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;