Sample_Comp1130_Midterm_wAns

Sample_Comp1130_Midterm_wAns - COMP113Midterm #2Winter 2010

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP113—Midterm #2—Winter 2010 Ver. A ANSWER KEY Page 1 of 11 COMP 113 – Computer Programming I Computing Science Department School of Advanced Technologies and Mathematics Version A ANSWER KEY COURSE: COMP 113 INSTRUCTOR: R.J. Lippelt SEMESTER: Winter 2010 DATE: 19 March 2010 Student Name: _______________________________ Student Number: _______________________________ Part 1 Write Methods: ___________ / 28 Part 2 Fix Syntax Errors & Produce Output: ___________ / 32 Part 3 Methods, Main, and Class definitions: ___________ / 30 Total : ___________ / 90 Please place all solutions in the space provided MIDTERM EXAMINATION 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP113—Midterm #2—Winter 2010 Ver. A ANSWER KEY Page 2 of 11 1. Write a method called: guessMe(). guessMe() accepts one parameter. The user is then prompted to guess the value of the parameter. In the first prompt, guessMe() displays a range of values within which the parameter value lies. This range of values is always 0 – 100. If the user guesses a number that is higher than the parameter value, guessMe() responds with: “Too High”. If the user guesses a number that is lower than the parameter value, guessMe() responds with: “Too Low”. This continues until the user guesses the correct parameter value. Once the correct parameter value has been guessed by the user, guessMe() returns the number of guesses it took the user to guess the parameter value back to the calling program. The parameter value does not allow decimal values. [Use the Scanner class to obtain the user’s input.] Here is an example of typical guessMe() session. guessMe() was invoked as: int guesses = guessMe(12); Computer: Try to guess my number. It is in the range 0 - 100 User : 10 Computer: Too Low User : 18 Computer: Too High User : 15 Computer: Too High User : 11 Computer: Too Low User : 12 Computer: Yes, that’s it! It took you 5 attempts. Answer : public int guessMe(int valueToGuess) { boolean stillGuessing = true; int nextGuess = 0; guessCount = 0; System.out.println(“Try to guess my value. It is in the range: 0 – 100”); Scanner keyboard = new Scanner(System.in); while (stillGuessing) { nextGuess = keyboard.nextInt(); If (nextGuess > valueToGuess) { System.out.println(“Too High”); guessCount++; } Else if (nextGuess < valueToGuess) { System.out.println(“Too Low”); guessCount++; } Else { System.out.println(“Yes, that’s it! It took you “ + guessCount + “ attempts.”); stillGuessing = false; } } return guessCount; } _____ / 16
Background image of page 2
COMP113—Midterm #2—Winter 2010 Ver. A ANSWER KEY Page 3 of 11 Question #1 Continued
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP113—Midterm #2—Winter 2010 Ver. A ANSWER KEY Page 4 of 11 2. Part ‘A’
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

Sample_Comp1130_Midterm_wAns - COMP113Midterm #2Winter 2010

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online