Unformatted text preview:

University of Maryland College Park Dept of Computer Science CMSC131 Fall 2017 Midterm I Last Name PRINT First Name PRINT University Directory ID e g umcpturtle I pledge on my honor that I have not given or received any unauthorized assistance on this examination Your signature Instructions This exam is a closed book and closed notes exam Total point value is 200 points The exam is a 50 minutes exam Please use a pencil to complete the exam WRITE NEATLY There are four problems in the exam You don t need to use meaningful variable names however we expect good indentation 1 2 3 4 Total Grader Use Only Problem 1 Problem 2 Problem 3 Problem 4 Total 20 50 60 70 200 1 2 Problem 1 Implement the method sum that has the prototype below The method computes the sum of the parameter values only if the three values are different otherwise it will return 0 For example sum 2 3 1 will return 6 but sum 2 2 1 sum 3 2 3 and sum 3 3 3 will return 0 public static int sum int a int b int c 3 Problem 2 Implement the method harmonicNumber that has the prototype below The method computes the n th harmonic number which is the sum of the reciprocals of the first n natural numbers HarmonicNumber n 1 1 2 1 3 1 4 1 n For example HarmonicNumber 2 is 1 5 public static double harmonicNumber int n 4 Problem 3 Implement the method drawDiagram that has the prototype below The method will display a triangle where the height corresponds to size and the first character in each row is preceded by an increasing number of spaces The number of rows corresponds to the size parameter The following are examples of diagrams the method will generate For a size parameter of 2 For a size parameter of 3 For a size parameter of 4 Your solution must handle different size values not just 2 3 and 4 public static void drawDiagram int size 5 Problem 4 Complete the guess number program that appears on the next page The program will read an integer value from the user and verify whether that value is the one the program selected valueToGuess The program will keep asking the user for a value as long as the user have not guessed the expected value valueToGuess and the number of allowed attempts 5 has not been exceeded If the value entered by the user is the expected value the program will print the message You won and end If the value is larger than valueToGuess the program will print Too high and Too low if smaller To read a value print the message Enter value The following is an example of running the program you are expected to write remember your program must work for other values Underlined text represents input provided by the user Enter value 6 Too Low Enter value 10 Too high Enter value 8 You won 6 public static void main String args Scanner scanner new Scanner System in Random random new Random int endOfRange 10 Generates random value between 1 and endOfRange int valueToGuess random nextInt endOfRange 1 7


View Full Document

UMD CMSC 131 - Midterm I

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Midterm I
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Midterm I and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Midterm I and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?