Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.

Similar presentations


Presentation on theme: "CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions."— Presentation transcript:

1 CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions

2 CSE 201 – Elementary Computer Programming2 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 CSE 201 – Elementary Computer Programming3 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9 ss 11 bb 88 ss 33 c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 CSE 201 – Elementary Computer Programming4 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 CSE 201 – Elementary Computer Programming5 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 CSE 201 – Elementary Computer Programming6 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 CSE 201 – Elementary Computer Programming7 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); letter = userInput.charAt(counter); if ((letter == '&') || (letter == '@') || (letter == '$')) if ((letter == '&') || (letter == '@') || (letter == '$')) { numSpecialChars = numSpecialChars + 1; numSpecialChars = numSpecialChars + 1; }} System.out.println("Number of special characters is " + numSpecialChars );

8 CSE 201 – Elementary Computer Programming8 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; x = 5;x = 5; elseif( x != y) x = 7; x = 7;


Download ppt "CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions."

Similar presentations


Ads by Google