![](https://simplycoding.in/wp-content/uploads/2021/06/Functions-or-methods-Programs-1.jpg)
Functions or methods Programs
- Categories Functions in Java, Java, Functions, Functions
1 . Specify the class Check with following overloaded functions:
- void compare (int x, int y): to compare and print smallest from x, y. If both the numbers are same then print suitable message.
- void compare(char, char): to compare the numeric value of the two characters with higher numeric values. If both characters are same then print any one of them.
public class Check
{
void compare(int x, int y)
{
if (x<y)
System.out.println("Smallest from "+x+ " and "+y+" is= "+x);
else if(y<x)
System.out.println("Smallest from "+x+ " and "+y+ " is= "+y);
else
System.out.println("Both the numbers "+x+ " and "+y+ " are same");
}
void compare(char c1, char c2)
{
if ( (int)c1 > (int)c2 )
System.out.println("Largest character from "+c1+ " and "+c2+" is= "+c1);
else if((int)c2 > (int)c1)
System.out.println("Largest character from "+c1+ " and "+c2+ " is= "+c2);
else
System.out.println("Both the characters "+c1+ " and "+c2+ " are same and value is= "+c1);
}
}
2 . Design a class distance along with a function double distanc() with three parameters u, time and acc of double type tofind and return the value of d from following relation:
d= ut+ 1/2 at^2
Also write a main() function to input velocity(v), time(t) and acceleration (a) and by invoking function distance(), print the value of d including v, t and a.
the program should be properly documented.
import java.util.Scanner;
public class distance
{
public static double distanc(double u, double t, double acc)
{
double d= (u*t)+(0.5 *acc*(t*t));
return d;
}
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
double v,t,a,result;
System.out.println("Enter the following data");
System.out.print("Enter velocity(v)=");
v=sc.nextDouble();
System.out.print("Enter time(t)= ");
t= sc.nextDouble();
System.out.print("Enter acceleration (a)= ");
a= sc.nextDouble();
result= (distanc(v,t,a));
System.out.println("The results are: ");
System.out.println("velocity (v)= "+v);
System.out.println("Time (t)= "+t);
System.out.println("Acceleration (a)= "+a);
System.out.println("Final result (d)= "+result);
}
}
3 . Create a class factorial with a data member ‘num’ and a function void getFact(factorial ob): to find the factorial of the integer stored in the parameterized object ob(i.e using call by reference method).
Also, write a main function to create an object and input an integer. Assign the integer to the object and invoke. The function getFact() by the current object, find and print the factorial. For example:
Input: n=5 Output: the factorial of 5 is = 120
import java.util.Scanner;
public class fatorial
{
int num;
void getFact(fatorial ob)
{
int n=ob.num;
int fac=1;
for(int x=1; x <=n;x++)
{
fac= fac*x;
}
ob.num= fac;
}
public void main()
{
Scanner sc= new Scanner(System.in);
fatorial obj = new fatorial();
System.out.println("Input number: ");
int v= sc.nextInt();
obj.num=v;
getFact(obj);
System.out.print("Output: ");
System.out.println("The factorial of "+v+ " is= "+obj.num);
}
}
4 . Create a function void PrintHCF(int x, int y): to find and print HCF/GCD of x and y using Euclid’s method (or remainder method). Also, write a main function to input the two integers and print their HCF.
Hint:
- Decide the grater from two numbers.
- Divide the greater number by smaller number, if remainder is zero, print divisor (Smaller number) as HCF, otherwise
- Take divisor as number and remainder as divisor, and repeat steps 2 and 3
import java.util.Scanner;
public class FunctionExp1
{
void PrintHCF(int x, int y)
{
int r, hcf=0;
int a=x,b=y;
if(x<y)
{
int t=x;
x=y;
y=t;
}
do {
r= x%y;
if(r == 0)
{
hcf=y;
break;
}
x=y;
y=r;
}
while(r!=0);
System.out.println("The HCF of "+a+" and "+b+" = "+hcf);
}
public void main()
{
int N, M;
Scanner sc= new Scanner (System.in);
System.out.print("Input first number: ");
N= sc.nextInt();
System.out.print("Input Second number: ");
M= sc.nextInt();
PrintHCF(N,M);
}
}
5 . Design a function void Interest(long accn, double principal, double rate, int time): to take account number(accn), principal amount(principal), rate of interest (rate) and time period (time). Calculate and print all the details of account holder including simple interest (SI) using the formula:
SI=(principal * rate*time)/100;
public class FunctionExp2
{
void Interest(long accn, double principal, double rate, int time)
{
double SI = 0;
SI=(principal*rate*time)/100.0;
System.out.println("Account number= "+accn);
System.out.println("Principal amount= "+principal);
System.out.println("Rate of interest= "+rate);
System.out.println("Time period= "+time);
System.out.println("Simple Interest= "+SI);
}
}
6 . Create a function int FindLCM(int x, int y): to find and return LCM of x and y. also, write a function accept() to input the two integers and print their LCM by invoking the above function.
import java.io.*;
public class FunctionExp3
{
int FindLCM(int x, int y)
{
int lcm=0, product=0;
product= x*y;
lcm= product;
for(int n=1; n<=product; n++)
{
if(n%x ==0 && n%y ==0 && n<lcm)
{
lcm=n;
break;
}
}
return lcm;
}
public void accept() throws IOException
{
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input first number: ");
int N= Integer.parseInt(in.readLine());
System.out.print("Input second number: ");
int M= Integer.parseInt(in.readLine());
int LCM= FindLCM(N,M);
System.out.println("The LCM od "+N+ " and "+M+ " = "+LCM);
}
}
7 . Design the following functions:
- int cubeCal(int y): to find and return the cube of argument y.
- void printNumbers(): by making use of function cubeCal() find and print Armstrong numbers between 10 to 500. (If sum of cubes of all the digits of a number, then the number is Armstrong.)
Example: n=153= 1^3 + 5^3 + 3^3 = 1+ 125 + 27 = 153, so 153 is Armstrong number.
public class FunctionExp4
{
public int cubeCal(int y)
{
return(y*y*y);
}
public void printNumbers()
{
int num, temp, s,d,c;
for(num=10; num<=500; num++)
{
s=0;
temp= num;
while(temp>0)
{
d= temp%10;
c= cubeCal(d);
s= s+c;
temp= temp/10;
}
if (s==num)
System.out.println(num+"is an Armstrong number");
}
}
}
8 . Specify the class OverloadedEx1 with following overloaded functions:
- int findMax(int n1, int n2): to decide and return largest from n1, n2. if both the numbers are same then return 0.
- int findMax(int num1, int num2, int num3): To decide and return largest from num1,num2,num3.
if all the numbers are same then return 0.
Write main() function to input three integers x,y and z, and by invoking above function print largest from x,y and also largest from x,y,z. use function overloading concept.
import java.util.Scanner;
public class OverloadedEx1
{
public int findMax( int n1, int n2) //1st overloaded function
{
if(n1 > n2)
return n1;
else if(n2 > n1)
return n2;
else
return 0;
}
public int findMax( int num1, int num2, int num3)//2nd overloaded fun
{
if(num1 > num2 && num1 > num3)
return num1;
else if(num2 > num1 && num2 > num3)
return num2;
else if(num3 > num1 && num3 > num1)
return num3;
else
return 0;
}
public void main()
{
int x, y, z;
Scanner obj = new Scanner(System.in);
System.out.print("Enter the first number : ");
x = obj.nextInt();
System.out.print("Enter the second number : ");
y = obj.nextInt( );
System.out.print("Enter the third number ");
z = obj. nextInt( );
int g1 = findMax( x, y); // fun call by passing 2 parameters
int g2 = findMax( x, y, z ); // fun call by passing 3 parameters
System.out.println("The results are : ");
if(g1==0 || g2==0)
System.out.println("The numbers are same ");
if( g1!= 0)
System.out.println("Largest from "+ x +" and "+ y +" is = " + g1 );
if( g2!= 0)
System.out.println("Largest from "+ x + ","+y +" and "+ z + " is = " + g2 );
}
}
9 . Design a class to overload a function polygon() as follows:
- void polygon(int n, char c): With one integer argument and one character type argument that draws a filled square of side ‘n’ using character stored in ‘c’.
- void polygon(int x, int y): with two integer arguments that draws a filled rectangle of length x’ and breadth ‘y’, using the symbol ‘@’.
- void polygon( ): With no argument that draws a filled triangle.
Sample example for each function is given below:
i. Input n = 2, c =’O’ | ii. Input x = 2, y =5 | iii. |
Output: | Output: | Output: |
o o o o | @ @ @ @ @ @ @ @ @ @ | * * * * * * |
public class OverloadedEx2
{
void polygon(int n, char c)//overloaded fun no.1 print desired pattern
{
for( int j= 1; j<= n; j++ )
{
for( int k = 1; k<= n; k++ )
{
System.out.print( c );
}
System.out.println( );
}
}//end of the 1nd overloaded function polygon()
void polygon(int x, int y)//overloaded fun no. 2 to print pattern
{
for( int j = 1;j<= x; j++)
{
for( int k = 1; k<= y; k++)
{
System.out.print("@");
}
System.out.println( );
}
}//end of the 2nd overloaded function polygon()
void polygon()//overloaded function no. 3 to print the desired pattern
{
for(int j = 1; j <= 3; j++)
{
for(int k = 1; k <= j; k++)
{
System.out.print("*");
}
System.out.println();
}
}//end of the 3rd overloaded function polygon()
}
10 . Design a class to overload a function series() as follows :
- double series( double n ) with one double argument and returns the sum of the series.
sum = 1/1 + 1/2 + 1/3 + … + 1/n - double series( double a, double n ) with two double arguments and returns the sum of the series.
sum = 1/a^2 + 4/5^5 + 7/a^8 1/a^11.. to n terms.
- double series( double n ) with one double argument and returns the sum of the series.
public class OverloadedEx3
{
public double series( double n ) //1st overloaded fun with one parameter n
{
double sum = 0.0;
for(double y = 1.0; y <= n; y++)
{
double term = 1.0 / y;
sum = sum + term;
}
return sum;
}
public double series( double a, double n )//2nd overloaded function with two parameters a, n
{
double sum = 0.0;
double numerator = 1.0, denominator = 2.0;
for( int i = 1;i <= n; i++ )
{
double term = numerator / (Math.pow( a, denominator) );
sum += term;
numerator += 3.0;
denominator += 3.0;
}
return sum;
}
}
11 . Design a class to overload a function area() as follows:
- double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using he formula:
area= SquareRoot(s(s-a)(s-b)(s-c)), where s= (a+b+c)/2 - double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area= 1/2 height (a+b) - double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
area= 1/2 (diagonal1 x diagonal2)
- double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using he formula:
public class OverloadedEx4
{
double area(double a, double b, double c)
{
double s= (a+b+c)/2;
double Area = Math.sqrt(s * (s-a) * (s-b) * (s-c));
return Area;
}
double area(int a, int b, int height)
{
return ((double)(height * (a+b))/2);
}
double area(double diagonal1, double diagonal2)
{
double Area = (diagonal1 * diagonal2);
return Area;
}
public void main()
{
double Area_triangle= area(15.0,12.0,8.0);
double Area_trapezium= area(12,4,6);
double Area_rhombus= area(9.0, 26.0);
System.out.println("Area of scalene triangle = "+Area_triangle);
System.out.println("Area of scalene trapezium = "+Area_trapezium);
System.out.println("Area of scalene rhombus = "+Area_rhombus);
}
}
12 . Write a class to design the given function to perform the related tasks:
- double volume1( double side )- to find and return volume of cube using (side*side*side).
- double volume2( double l, double b, double h)- to find and return volume of cuboid using:(length*breadth*height).
- double volume3( double r )- to find and return volume of cylinder using: ((4/3)πr^3)
public class OverloadedEx6
{
public double volume1(double s)
{
return (s*s*s);
}
public double volume2(double l, double b, double h)
{
return (l*b*h);
}
public double volume3(double r)
{
return ((4/3)*3.14*r);
}
}
13 . Write a class to make overloaded function Volume() to perform the following:
- to find and return Volume of a cube using (side*side*side)
- to find and return Volume of a cuboid using (length*breadth*height)
- to find and return Volume of a cylinder using ((4/3)πr^3)
public class OverloadedEx7
{
public int volume(int s)
{
int cube= s*s*s;
return cube;
}
public int volume(int l, int b, int h)
{
int cuboid= l*b*h;
return cuboid ;
}
public double volume(double r)
{
double cylinder= (4/3)*3.14*r;
return cylinder ;
}
}
15 . Design a class to overload a function area() as follows:
- double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula area= √(s(s- a)(s-b)(s- c) ) where s = a+b+c
- double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula: area = height (a+ b)
- double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula: area =(diagonal1 x diagonal2)
public class OverloadedEx8
{
public double area(double a, double b, double c)
{
double s= (a+b+c)/2;
return (Math.sqrt(s*(s-a)*(s-b)*(s-c)));
}
public double area(int a, int b, int height)
{
return ( (1.0/2)*height*(a+b) ) ;
}
public double area(double diagonal1, double diagonal2)
{
return ( (1.0/2)* diagonal1 * diagonal2 ) ;
}
}
16 . Design a class to overload a function compare() as follows :
- void compare( int, int ): to compare two integer values and print the Smallest of the two.
- void compare( char, char ) : to compare the numeric value of two characters and print the character with lower numeric value.
public class OverloadedEx9
{
public void compare(int a, int b)
{
int max = Math.min(a, b);
System.out.println("From " +a+ " and "+b+ " Smallest number is: "+max);
}
public void compare(char a, char b)
{
char max = (char) Math.min(a, b);
System.out.println("From " +a+ " and "+b+ " Lowest numeric value is: "+max);
}
}
17 . Design a class to overload a function SumSeries() as follows:
- void SumSeries(int n, double x): with one integer argument and one double argument to find and display the sum of the series given below:
s= x^1 -x^2 +x^3 -x^4 … to x^n terms - void SumSeries() to find and display the sum of the following series:
S= 1 + (1 x 2) + (1 x 2 x 3) + (1 x 2 x 3 x 4) + … 1x 2 x 3 x 4 x 5 …X 20)
- void SumSeries(int n, double x): with one integer argument and one double argument to find and display the sum of the series given below:
public class OverloadedEx10
{
public void SumSeries(int n, double x)
{
double sum= 0;
for(int i=1; i<=n; i++)
{
if(i%2==0)
sum= sum- Math.pow(x,i);
else
sum= sum+ Math.pow(x,i);
}
System.out.println(" x^1 -x^2 +x^3 -x^4 ... to x^n = "+sum);
}
public void SumSeries()
{
int i,s=1;
for(i=1;i<=20;i++)
{
s=s*i;
}
System.out.println("1 + (1 x 2) + (1 x 2 x 3)...(1 x 2 x 3 x ...x 20)= "+s);
}
}
18 . Design a class sample for the following functions to print cash memo for a product purchased from a shop:
- void product(): create this function with product code(integer), unit price of product(float) and quantity of product(float) as parameters or arguments. calculate total cost of the product, discount as 12% on total cost and net price to be paid for the product.
- void run(): to invoke function product() by passing required values to this function and print all the data members including total, discount and net price of product with suitable message.
public class Sample
{
public void product(int code, float price, float quantity)
{
float t;
double dis, Nprice;
t= price*quantity;
dis= (t*12.0)/100.0;
Nprice= t-dis;
System.out.println("\n Cash Memo\n");
System.out.println("Product code ="+code);
System.out.println("Price ="+price);
System.out.println("Quantity ="+quantity);
System.out.println("Total ="+t);
System.out.println("Discount ="+dis);
System.out.println("Net Price ="+Nprice);
}
public void run()
{
product(2360,65,97);
}
}
19 . design a class swapValues with a function swapper() with two parameters A and B. Interchange or swap values of A and B print the interchanged values. Also, write a function show() to input two integers and swap them by calling or invoking the function swapper(). print the integers before and after interchange.
import java.util.Scanner;
public class SwapValues
{
public void swapper(int A, int B)
{
int tempVar;
tempVar= A;
A=B;
B=tempVar;
System.out.println("Number after interchange "+A+ "\t" +B);
}
public void show()
{
Scanner sc= new Scanner(System.in);
System.out.print("Enter first Number= ");
int x= sc.nextInt();
System.out.print("Enter Second Number=");
int y= sc.nextInt();
System.out.println("Output :");
System.out.println("Numbers before interchange "+x+"\t"+y);
swapper(x,y);
}
}
20 . Design a class swapValues1 with method Swapper(swapValues1 obj) which receives two integers by reference (or swapValues1 object ‘obj’).
Interchange values stored in the object ‘obj’. Also, write a main() function to input the two integers and swap or interchange them by calling or invoking the function swapper(swapVales1 obj), by reference. print numbers before and after interchange.
import java.util.Scanner;
public class SwapValues1
{
int a,b;
public void swapper(SwapValues1 obj)
{
int tempVar;
tempVar= obj.a;
obj.a=obj.b;
obj.b=tempVar;
}
public void main()
{
Scanner sc= new Scanner(System.in);
SwapValues1 sv= new SwapValues1();
System.out.print("Enter first Number= ");
a= sc.nextInt();
System.out.print("Enter Second Number=");
sv.b= sc.nextInt();
System.out.println("Output:");
System.out.println("Numbers before interchange= "+sv.a + "\t"+ sv.b);
swapper(sv);
System.out.println("Numbers after interchange= "+sv.a+ "\t"+ sv.b);
}
}
You may also like
![Single Linked List Programs in Java Single Linked List Programs in Java](https://simplycoding.in/wp-content/uploads/2021/07/Single-Linked-List-Programs-in-Java-300x200.png)
Single Linked List Programs in Java
![Implementing Stack using Array in Java Implementing Stack using Array in Java](https://simplycoding.in/wp-content/uploads/2021/08/Implementing-Stack-using-Array-in-Java-300x200.png)
Implementing Stack using Array in Java
![Constructor Programs Constructor Programs](https://simplycoding.in/wp-content/uploads/2021/06/Constructor-Programs-300x200.jpg)