Week 12 28-04-2025
Week 12 28-04-2025
Aim
To provide students with a practical introduction to user-defined functions (static methods).
Objectives
After completing this week’s session, you should be able to:
Tasks
All tasks have been taken/adapted from Sedgwick.
1. Write a static method max3() that takes three int values as arguments and returns the value of
the largest one. Add an overloaded function that does the same thing with three double values.
2. Write a static method max() that takes a list of int values as arguments and returns the value of
the largest one. Add an overloaded function that does the same thing with a list of double values.
3. Write a static method odd() that takes three boolean inputs and returns true if an odd number
of inputs are true, and false otherwise.
4. Write a static method majority() that takes three boolean arguments and returns true if at
least two of the arguments have the value true, and false otherwise. Do not use an if-statement.
5. Write a static method eq() that takes two arrays of integers as arguments and returns true if
they contain the same number of elements and all corresponding pairs of elements are equal.
6. Write a static method areTriangular() that takes three double values as arguments and
returns true if they could be the sides of a triangle (none of them is greater than or equal to the
sum of the other two).
7. Write a program with three static methods. The first one creates an array of N random integers
(values between 1 and N). The second method should determine whether there are any
duplicates in the random array. You do not need to preserve the contents of the given array, but
do not use an extra array. The third one is the main method.
8. Rewrite the program in 7, this time making sure that the second method does not change the
order of elements in the random array.