midterm-review-problems

Midterm-review-probl - BASIC PROGRAMMING EXERCISES(CONDITIONAL STATEMENTS LOOPS METHODS AND ARRAYS COMP-202B Winter 2010 All Sections INSTRUCTIONS

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: BASIC PROGRAMMING EXERCISES (CONDITIONAL STATEMENTS, LOOPS, METHODS, AND ARRAYS) COMP-202B, Winter 2010, All Sections INSTRUCTIONS • Attempt each question on paper before trying to implement it in a Java program. • Attempt to implement the solution to a problem only once you think you have a solution written on paper. • Every loop MUST terminate as soon as it can; for example, if a question asks you to write a method which determines whether a given value occurs in a given array, the loop MUST terminate as soon as it finds a value in the given array which is equal to the given value, without looking at the rest of the values. However, you MUST NOT use the break statement to exit loops. • Each question asks you to write a class which defines one method. After completing each question, add a main() method to the class you wrote for that question. This main() method should ask the user to enter values for each of the parameters of the method you wrote for that question, and read these values from the keyboard. Then, have your main() method call the method you wrote for that question, pass the values the user entered as parameters to this method, and display the results this method returns. • To read an array of char from the keyboard, use keyboard.nextLine().toCharArray(); where keyboard is the name of the Scanner variable you initialized to read from the keyboard. Likewise, to read a single char from the keyboard, use keyboard.nextLine().charAt(0); where, again, keyboard is the name of the Scanner variable you initialized to read from the keyboard. PROBLEMS 1. Write a Java class called Factorial . This class defines a method called factorial() which takes as its only parameter an int called n , and returns an int representing the factorial of n . The factorial of an integer n , denoted n !, is defined as n · ( n- 1) · ( n- 2) · ... · 1 (but note that 0! is 1). You MAY assume that n is a non-negative integer. 2. Write a Java class called Fibonacci . This class defines a method called fibonacci() which takes as its only parameter an int called n , and returns an int representing the n th Fibonacci number. The 1 n th Fibonacci number, denoted f n , is defined as follows: f = 0 f 1 = 1 f n = f n- 1 + f n- 2 You MAY assume that n is a non-negative integer. 3. Write a Java class called PrimalityChecker . This class defines a method called isPrime() , which takes as its only parameter an int called n , and returns a boolean which is true if and only if n is a prime number, false otherwise. A prime number is an integer which cannot be divided evenly by any integer except 1 and itself. You MAY assume that n is a non-negative integer....
View Full Document

This note was uploaded on 10/16/2010 for the course COMP 202 taught by Professor Verbrugge during the Winter '07 term at McGill.

Page1 / 4

Midterm-review-probl - BASIC PROGRAMMING EXERCISES(CONDITIONAL STATEMENTS LOOPS METHODS AND ARRAYS COMP-202B Winter 2010 All Sections INSTRUCTIONS

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

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