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.
- Write a method which takes an
int as parameter and returns a boolean
value indicating whether the number is even.
- 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.
- 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.
- 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.
- 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.
- Write a method which takes an array of
doubles and returns the last element
of the array.
- Write a method which takes an array of
doubles and returns
the average of the elements in the array.
- Write a method which takes an array of
doubles and returns
a boolean value indicating whether all the elements in the array are positive.
- 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.
- 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.
- 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.
|