Exam 2 review
Version 0

 

This lab consists of extra problems relating to methods and arrays to be done in preparation for the exam at the end of week 10.

Note: You will almost certainly not be able to complete all the problems during lab time. You should keep working on them on your own, and seek help from the tutors or professors if you need it. Any one of these would be a reasonable exam question.

The first five problems ask you to write methods which do simple numerical manipulations. It is easiest to build a single class containing all of these methods (added and tested one-by-one) and single main method which gets input from the user to be used in testing the other methods.

The remaining problems require writing methods which manipulate arrays. Again, it is easiest to combine them into a single program. The test arrays may either be input by the user or be given their contents in an initialization step. You might want to use the test program from last week's lab as a shell for this program.

For each of the array problems, make sure that all the methods will work no matter how many elements are in the array you pass to them.

  1. Write a method which takes an int as parameter and returns a boolean value indicating whether the number is even.

  2. Write a method which takes two ints as parameters and returns a boolean value indicating whether the first number is a factor of the second number.

  3. Write a method which takes an int and returns a boolean value indicating whether the given number is prime or not. This method should call the previous one to do some of its work.

  4. Write a method which takes an int and returns as an int the next larger prime number, or the number itself if it is prime. This method should call the previous one to do some of its work.

  5. Write a method which takes two ints as parameters and returns a boolean indicating whether they are relatively prime. This method should call the Euclid's Method method written in the lab for week 07 to do the bulk of its work (two numbers are relatively prime if their greatest common factor is ...). If you have not already written that method, write it first.

  6. Write a method which takes an array of doubles and returns the last element of the array.

  7. Write a method which takes an array of doubles and returns the average of the elements in the array.

  8. Write a method which takes an array of doubles and returns a boolean value indicating whether all the elements in the array are positive.

  9. Write a method which takes a double and an array of doubles and returns a boolean value indicating whether the given number occurs as one of the elements of the array.

  10. Write a method which takes two arrays of doubles and returns a boolean indicating whether the average of the elements in the first is greater than the average of the elements in the second. This should call the method written earlier to compute the averages.

  11. Write a method which takes an array of doubles and returns a boolean value indicating whether more than half the elements of the array are greater than the average of the elements of the array.


This page copyright ©1998 by Joshua S. Hodas. It was built with Frontier on a Macintosh . Last rebuilt on Tue, Oct 20, 1998 at 9:12:33 PM.
http://www.cs.hmc.edu/~hodas/courses/cs5/week_10/lab.html