0% found this document useful (0 votes)
8 views1 page

Week 12 28-04-2025

The document outlines a lab session for COM121 in 2025, focusing on user-defined functions (static methods). Students are tasked with writing various static methods to perform operations such as finding maximum values, checking boolean conditions, and validating triangle sides. The session aims to enhance students' programming skills by implementing multiple static methods in their programs.

Uploaded by

zomahaco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Week 12 28-04-2025

The document outlines a lab session for COM121 in 2025, focusing on user-defined functions (static methods). Students are tasked with writing various static methods to perform operations such as finding maximum values, checking boolean conditions, and validating triangle sides. The session aims to enhance students' programming skills by implementing multiple static methods in their programs.

Uploaded by

zomahaco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

COM121 2025 Lab Session 9 (week 11)

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:

1. Write programs composed of more than one static methods

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.

You might also like