0% found this document useful (0 votes)
3 views4 pages

Function Overloading Programs

The document provides Java programs demonstrating function overloading for various geometric calculations, including area, perimeter, and volume of different shapes. It also includes methods for displaying characters based on conditions, checking divisibility, comparing values, and checking word patterns. Each class contains overloaded methods tailored for specific operations, showcasing the versatility of function overloading in Java.

Uploaded by

graciemendoza140
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)
3 views4 pages

Function Overloading Programs

The document provides Java programs demonstrating function overloading for various geometric calculations, including area, perimeter, and volume of different shapes. It also includes methods for displaying characters based on conditions, checking divisibility, comparing values, and checking word patterns. Each class contains overloaded methods tailored for specific operations, showcasing the versatility of function overloading in Java.

Uploaded by

graciemendoza140
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

Java Programs using Function Overloading

15. Class Area: Compute Area of Parallelogram, Rhombus, Trapezium

public class Area {


double area(double base, double height) {
return base * height; // Parallelogram
}

double area(double d1, double d2, char rhombus) {


return 0.5 * d1 * d2; // Rhombus
}

double area(double a, double b, double h) {


return 0.5 * (a + b) * h; // Trapezium
}
}

16. Class Perimeter: Compute Perimeter of Square, Rectangle, Circle

public class Perimeter {


int perimeter(int s) {
return 4 * s; // Square
}

int perimeter(int l, int b) {


return 2 * (l + b); // Rectangle
}

double perimeter(double r) {
return 2 * 3.1416 * r; // Circle
}
}

17. Class OverloadingDisplay: Display Uppercase, Vowels, etc.

public class OverloadingDisplay {


void display(String str, int p) {
if (p == 1) {
for (int i = 0; i < [Link](); i++)
if ([Link]([Link](i)))
[Link]([Link](i));
} else {
for (int i = 0; i < [Link](); i++)

1
if ([Link]([Link](i)))
[Link]([Link](i));
}
}

void display(String str, char chr) {


if (chr == 'v') {
for (int i = 0; i < [Link](); i++)
if ("aeiouAEIOU".indexOf([Link](i)) != -1)
[Link]([Link](i));
} else {
for (int i = 0; i < [Link](); i++)
if ([Link]([Link](i)))
[Link]([Link](i));
}
}
}

18. Class OverloadCalculate: Check Divisibility and Greater Value

public class OverloadCalculate {


void calculate(int m, char ch) {
if (ch == 's') {
[Link](m % 7 == 0 ? "Divisible by 7" :
"Not divisible by 7");
} else {
[Link](m % 10 == 7 ? "Last digit is 7" : "Last digit is
not 7");
}
}

void calculate(int a, int b, char ch) {


[Link]("Greater: " + (a > b ? a : b));
}
}

21. Class CompareOverload: Compare Ints, Chars, Strings

public class CompareOverload {


void compare(int a, int b) {
[Link](a > b ? a : b);
}

void compare(char a, char b) {


[Link](a > b ? a : b);

2
}

void compare(String a, String b) {


[Link]([Link]() > [Link]() ? a : b);
}
}

23. Class PolygonOverload: Print Shapes

public class PolygonOverload {


void polygon(int n, char ch) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
[Link](ch);
}
[Link]();
}
}

void polygon(int x, int y) {


for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
[Link](j == 0 ? '@' : '0');
}
[Link]();
}
}

void polygon() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
[Link]('*');
}
[Link]();
}
}
}

25. Class DisplayCheck: Check Word Patterns

public class DisplayCheck {


void display(String str, char ch) {
if ([Link](0) == ch && [Link]([Link]() - 1) == ch) {
[Link]("Special Word");
} else {

3
[Link]("No special word");
}
}

void display(String str1, String str2) {


[Link]([Link](str2) ? "Equal" : "Not Equal");
}

void display(String str, int n) {


if (n >= 1 && n <= [Link]())
[Link]("Character: " + [Link](n - 1));
else
[Link]("Invalid position");
}
}

27. Class VolumeOverload: Sphere, Cylinder, Cuboid Volume

public class VolumeOverload {


double volume(double r) {
return (4.0 / 3) * (22.0 / 7) * r * r * r; // Sphere
}

double volume(double h, double r) {


return (22.0 / 7) * r * r * h; // Cylinder
}

double volume(double l, double b, double h) {


return l * b * h; // Cuboid
}
}

End of Function Overloading Programs

You might also like