C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Method Overloading - Pattern & Series - Java Programs

Design a class to overload a method called pattSeries() as follows:

(a) void pattSeries() – To generate and display the pattern given below:
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

(b) void pattSeries(int n) – To find and display the sum of the series given below:


Write a main() method to create an object of the class and call the above member methods.

class Overload{ public void pattSeries(){ for(int i = 5; i >= 1; i--){ for(int j = i; j >= 1; j--) System.out.print(j + " "); System.out.println(); } } public void pattSeries(int n){ double sum = 0.0; for(int i = 1; i <= n; i++) sum += i / (i + 2.0); System.out.println("Sum = " + sum); } public static void main(String[] args){ Overload obj = new Overload(); obj.pattSeries(); obj.pattSeries(5); } }

Output

 
 OUTPUT 1: 
5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 

Sum = 2.814285714285714