Module1-CC103
Module1-CC103
User-defined Functions
1.1 Introduction
1
In the improved example, the add method abstracts the logic of addition, making the
code more readable and reusable.
1. Input Validation:
Abstracting input validation logic into a reusable function.
3. Mathematical Computations:
Common operations like computing averages or maximum values.
2
In this example:
In Java, function declarations are commonly used in interface definitions and abstract
classes, where only the method signature is provided, and subclasses or implementing
classes provide the implementation.
In this example:
3
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
3. Method with Parameters and a Return Value
public int addNumbers(int num1, int num2) {
return num1 + num2;
}
1. Same Method Name: All overloaded methods share the same name.
2. Different Parameter Lists: Methods must differ in one or more of the following
ways:
o Number of parameters.
o Type of parameters.
o Order of parameters.
3. Return Type: The return type does not differentiate overloaded methods.
Changing only the return type without changing the parameter list will result in a
compilation error.
4
In this example:
The square method is overloaded to handle both int and double types.
The multiply method is overloaded to handle both int and double
parameters.
System.out.println("Square of 5: " +
operations.square(5)); // Calls square(int)
System.out.println("Square of 5.5: " +
operations.square(5.5)); // Calls square(double)
System.out.println("Product of 3 and 4: " +
operations.multiply(3, 4)); // Calls multiply(int, int)
System.out.println("Product of 3.5 and 4.5: " +
operations.multiply(3.5, 4.5)); // Calls multiply(double,
double)
}
}
5
1.5 Learning Activities
1.5.1 Multiple Choice:
Directions: Read Each Question Carefully. Ensure you understand the question before
selecting an answer.
5. What is the correct way to declare a function that takes two integers and
returns their product in Java?
A. int multiply(int a, int b);
B. public int multiply(int a, int b) { return a * b; }
C. void multiply(int a, int b)
D. function multiply(int a, int b) { return a * b; }
6
C. public double calculate(int a) and public double calculate(int a, int b)
D. public void print(int a) and public void print(int a)
1. Problem: Create a program that calculates the area and perimeter of a rectangle using
separate functions for each operation. Use the algorithm provided below in writing your
program.
Algorithm:
1. Start.
2. Input the length and width of the rectangle.
3. Call the calculateArea(length, width) function.
4. Call the calculatePerimeter(length, width) function.
5. Display the area and perimeter.
6. End.
Algorithm:
1. Start.
2. Define multiple sum functions with different parameters.
3. Call sum(int a, int b) and store the result.
4. Call sum(double a, double b) and store the result.
5. Call sum(int a, int b, int c) and store the result.
6. Display the results.
7. End.
3. Problem: Write a program that converts temperature between Celsius and Fahrenheit
using two functions: one for converting from Celsius to Fahrenheit, and the other for
converting from Fahrenheit to Celsius.
Requirements:
7
Define a function fahrenheitToCelsius(double fahrenheit) to convert
Fahrenheit to Celsius.
Take input from the user for the temperature value and unit (Celsius or Fahrenheit).
Display the converted temperature.
Example Input/Output:
4. Problem: Write a program that calculates the area of different shapes using function
overloading. Implement functions for:
Requirements:
Overload the area function with different parameters for different shapes.
The program should allow the user to choose the shape and input the necessary
values.
Example Input/Output:
Choose a shape:
1. Circle
2. Rectangle
3. Triangle
Enter your choice: 1
Enter the radius of the circle: 5
1.6 References
Ebook - Farrel, J, Java Programming, Course Technology, Cengage Learning
• https://www.javatpoint.com/java-function
• https://www.geeksforgeeks.org/function-overloading-in-programming/
• https://www.dremendo.com/java-programming-tutorial/java-function-overloading