{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

midterm-review-problems

midterm-review-problems - BASIC PROGRAMMING...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
n th Fibonacci number, denoted f n , is defined as follows: f 0 = 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern