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

Board Method Overloading Imp Practice Program

The document contains multiple Java programs demonstrating concepts such as nested loops, method overloading, and class definitions. Program 1 prints Floyd's triangle and calculates a series sum, Program 2 calculates areas of different shapes using overloaded methods, Program 3 finds the largest element in an array, and performs string manipulations, while Program 4 calculates the curved surface area of a cone and performs division operations. Program 5 displays a pattern of letters, checks for 'scary' numbers, and calculates squares or square roots based on input.

Uploaded by

asha.bluebirds
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 views5 pages

Board Method Overloading Imp Practice Program

The document contains multiple Java programs demonstrating concepts such as nested loops, method overloading, and class definitions. Program 1 prints Floyd's triangle and calculates a series sum, Program 2 calculates areas of different shapes using overloaded methods, Program 3 finds the largest element in an array, and performs string manipulations, while Program 4 calculates the curved surface area of a cone and performs division operations. Program 5 displays a pattern of letters, checks for 'scary' numbers, and calculates squares or square roots based on input.

Uploaded by

asha.bluebirds
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/ 5

Program 1

/* (a) Define a class to print the Floyds triangle of given rows using nested loops only.
Example:
A Floyd triangle of 6 rows is
1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21*/

class floyd
{
public static void main(String args[])
{
int x=1;
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(x+++" ");
}
System.out.println();
}
}
}
/*(b) Define a class to print the sum of the following series:
1+(1+2)+(1+2+3)+..........(1+2+3……20) */
class sumseries
{
public static void main(String args[])
{
int s=0,sum=0;
for(int i=1;i<=20;i++)
{
for(int j=1;j<=i;i++)
{
s=s+i;
}
sum=sum+s;
}
System. out. println("Sum="+sum);
}
}
Program 2

Rahul was asked to create a class with methods to calculate the area of a triangle (½ b
h ), the area of a square ( s x s), the area of a trapezium (½ h(a+b)), the area of a
rectangle (l x b). He created a class by giving the same name to all the methods as area.
Name the element of Java used in the program. Define a class with the four methods
with the same name. */
class polygon
{
void area(double b, double h)
{
System.out.println("Area of triangle= "+1.0/2*b*h);
}
void area(double s)
{ System.out. println("Area of square= "+s*s);
}
void area(double h, double a, double b)
{
System.out. println(" Area of trapezium= "+1.0/2*h*(a+b));
}
void area(int l, int b)
{
System. out.println(" Area of rectangle= "+l*b);
}
public static void main(String args[])
{
polygon obj=new polygon();
obj.area(10.0,20.0);
obj.area(10.0);
obj.area(10.0,20.0,30.0);
obj.area(10,20);
}
}
Program 3

/* Define a class to overload the method that performs as follows :


void perform(int x[]) - to display the largest element of the array.
Example:
{4, 5, 16, 6, 12, 9}
Largest: 16
void perform (String s, char ch)
If ch is ‘F’ print the first 5 characters, if ch is ‘L’ print the last 5characters
Example: s= “CARPENTER”
ch = F
Output: CARPE
ch = L
Output: ENTER
void perform(int n) : to print the Product of the first and last digits of the number
Example: if n = 45326
Output: 4x6=24*/
class overload
{
void perform(int x[])
{
int max=x[0];
for (int i=0;i<x.length;i++)
{
if (x[i]>max)
{
x[i]=max;
}
}
System. out. println(max);
}
void perform(String s, char ch)
{
int len=s.length();
if (ch=='F'||ch=='f')
System.out.println(s.substring(0,5));
if (ch=='L'||ch=='l')
System. out. println(s.substring(len-5));
}
void perform(int n)
{
int last=n%10,rem=0;
while(n>0)
{
rem=n%10;
n=n/10;
}
int first=rem;
System.out.println("product of first and last digit="+(first*last));
}
public static void main(String args[])
{
int x[]={1,3,2,4,7,5,6,8,6,4};
overload obj=new overload();
obj.perform(x);
obj.perform("computer application",'F');
obj.perform(125);
}
}
Program 4
/*
Define a class to overload the method perform() as follows:
double perform(double r, double h) – to calculate and return the value of curved surface area of cone.
double perform (double r, double h) to calculate and return the value of
Curved surface area of cone
𝐶𝐶𝐶𝐶𝐶𝐶 = 𝜋𝜋𝜋𝜋𝜋𝜋 𝑙𝑙 = √𝑟𝑟2 + ℎ2

void perform(int r, int c) – use nested for loop to generate the following format:
r = 4, c = 5
Output:
12345
12345
12345
12345
void perform(int m, int n, char ch) – to print the quotient of the division of m and n if ch is Q
else print the remainder of the division of m and n if ch is R.
*/
class Overload{
double perform(double r, double h)
{
double l = Math.sqrt(r * r + h * h);
double csa = Math.PI * r * l;
return csa;
}
void perform(int r, int c)
{
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
void perform(int m, int n, char ch){
if(ch == 'Q'|| ch=='q')
System.out.println("Quotient: " + (m / n));
else if(ch == 'R'|| ch=='r')
System.out.println("Remainder: " + (m % n));
}
public static void main(String args[])
{
Overload obj = new Overload();
obj.perform(4,5);
obj.perform(600,20,'Q');
System.out.println("curved surface area of cone = "+obj.perform(10.0,20.0));
}
}
Program 5
/*void display():To print the following format using nested loop
A
BC
DEF
GHIJ
KLMNO
void display(int n): To check whether the given number is a scary number or not
A scary number is a number which has 13 in the number
double display(int n,char ch):display square of the number if ch==s otherwise display square root of the number
*/

public class overload


{
void display()
{
int alpha=65;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print((char)alpha++);
}

System.out.println();
}
}
void display(int n)
{
int rem=0,temp=0,n1=n;

while(n>0)
{
rem=n%100;
if(rem==13)
{
temp=1;
break;
}
n=n/10;
}

if(temp==1)
System.out.println(n1+"is a scary number");
else
System.out.println(n1+"is not scary number");
}
double display(int n,char ch)
{
if(ch=='s'||ch=='S')
return(n*n);
else
return Math.sqrt(n);
}
public static void main(String args[])
{
overload obj=new overload();
obj.display();
obj.display(4137);
System.out.println(obj.display(125,'r'));
}
}

You might also like