1 / 8

Extra Exercises

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.

marina
Download Presentation

Extra Exercises

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Extra Exercises Source: Suggested but not selected midterm questions

  2. 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.

  3. 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.

  4. 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')

  5. 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')

  6. 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; }

  7. 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 );

  8. 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;

More Related