0% found this document useful (0 votes)
5 views9 pages

Exp 4 - Methods

The document contains Java programming exercises focused on implementing methods and method overloading. It includes programs for addition of numbers, calculating the sum of three numbers with conditions, finding the sum of digits, calculating factorials, demonstrating static methods, and showcasing method overloading. Each program is provided with code examples and explanations for user input and output.

Uploaded by

latkaradit
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)
5 views9 pages

Exp 4 - Methods

The document contains Java programming exercises focused on implementing methods and method overloading. It includes programs for addition of numbers, calculating the sum of three numbers with conditions, finding the sum of digits, calculating factorials, demonstrating static methods, and showcasing method overloading. Each program is provided with code examples and explanations for user input and output.

Uploaded by

latkaradit
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

Experiment No.

4
Name:- Adit Arvind Latkar

Class:- SY-C Roll No.:- 62 PRN:- 242141200

Aim: Implement Java programs using methods and method overloading.

Questions:

1. Write a program to find addition of two numbers using a return type method and a
non return type method.
import [Link].*;

class Return
{
int add(int a, int b) {
return a + b;
}

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

int a, b;

[Link]("Enter first number: ");


a = [Link]([Link]());

[Link]("Enter second number: ");


b = [Link]([Link]());

Return c = new Return();


int result = [Link](a, b);

[Link]("Sum = " + result);


}
}
import [Link].*;

class Nonreturn {
void add(int a, int b) {
int sum = a + b;
[Link]("Sum = " + sum);
}

public static void main(String args[]) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

int a, b;

[Link]("Enter first number: ");


a = [Link]([Link]());

[Link]("Enter second number: ");


b = [Link]([Link]());

Nonreturn c = new Nonreturn();


[Link](a, b);
}
}
2. Write a program to calculate sum of three user entered numbers, if the values are
equal, then return thrice of their sum using a method.
import [Link].*;

class Three
{
int add(int a, int b, int c) {
if(a == b && b == c)
{
return (a + b + c) * 3;
}
else{
return (a + b + c);
}
}

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

int a, b, c;

[Link]("Enter three numbers: ");


a = [Link]([Link]());
b = [Link]([Link]());
c = [Link]([Link]());

Three t = new Three();


int result = [Link](a, b, c);

[Link]("Result = " + result);


}
}

3. Write a program to find sum of digits of a user entered number using a method with
parameter list.
import [Link].*;

class Sum
{
int sum(int n)
{
int s = 0;
while(n != 0)
{
s = s + n % 10;
n = n / 10;
}
return s;
}

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

int num;
[Link]("Enter number: ");
num = [Link]([Link]());

Sum s = new Sum();


int result = [Link](num);

[Link]("Sum of digits = " + result);


}
}

4. Write a program to find factorial of a number using a method.


import [Link].*;

class Factorials
{
int fact(int n)
{
int f = 1;
for(int i = 1; i <= n; i++)
{
f = f * i;
}
return f;
}

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

int num;
[Link]("Enter number: ");
num = [Link]([Link]());

Factorials c = new Factorials();


int result = [Link](num);

[Link]("Factorial = " + result);


}
}

5. Write a program to demonstrate static method and variables.


class Dataa
{
int a = 10;
int b = 20;
static int c = 30;
static int d = 40;

void show()
{
[Link](a);
[Link](b);
[Link](c);
[Link](d);
}

static void display()


{
Dataa d2 = new Dataa ();
[Link](d2.a);
[Link](d2.b);
[Link](c);
[Link](d);
}
}

class Demo
{
public static void main(String args[])
{
Dataa d1 = new Dataa();
[Link]();
[Link]();
}
}
6. Write a program to demonstrate method overloading.

class Overloading
{
int add(int a, int b)
{
return a + b;
}

int add(int a, int b, int c)


{
return a + b + c;
}

double add(double a, double b)


{
return a + b;
}

public static void main(String args[]) throws IOException


{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

Overloading d = new Overloading();


int x = [Link](5, 10);
int y = [Link](2, 3, 4);
double z = [Link](2.5, 3.5);

[Link]("Sum1 = " + x);


[Link]("Sum2 = " + y);
[Link]("Sum3 = " + z);
}
}

You might also like