0% found this document useful (0 votes)
27 views17 pages

OVERLOAD

The document contains a series of programming questions and answers related to method overloading in Java. Each question requires the definition of a class with overloaded methods for various functionalities such as displaying patterns, calculating areas and volumes, and manipulating strings. The answers provide complete Java code implementations for each question, demonstrating the use of method overloading effectively.

Uploaded by

Ravi Hansda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views17 pages

OVERLOAD

The document contains a series of programming questions and answers related to method overloading in Java. Each question requires the definition of a class with overloaded methods for various functionalities such as displaying patterns, calculating areas and volumes, and manipulating strings. The answers provide complete Java code implementations for each question, demonstrating the use of method overloading effectively.

Uploaded by

Ravi Hansda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

OVERLOAD

QUESTION 1
Define a class to overload the method display() as follows:
void display(): To print the following format using nested
loop.
12121
12121
12121
void display (int n, int m) : To print the quotient of the
division of m and n if m is greater than n otherwise print
the sum of twice n and thrice m.
double display (double a, double b, double c) — to print
the value of z where
z=p×qz=p×q
p=a+bcp=ca+b
q=a+b+cq=a+b+c

ANSWER
public class OverloadDisplay
{
void display() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (j % 2 == 0) {
System.out.print("1 ");
} else {
System.out.print("2 ");
}
}
System.out.println();
}
}
void display(int n, int m) {
if (m > n) {
double q = m / n;
System.out.println("Quotient: " + q);
} else {
int sum = 2 * n + 3 * m;
System.out.println("Sum: " + sum);
}
}
double display(double a, double b, double c) {
double p = (a + b) / c;
double q = a + b + c;
double z = p * q;
System.out.println("Z = " + z);
return z;
}
}

QUESTION 2
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
CSA=πrlCSA=πrll=r2+h2
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
Answer
import java.util.Scanner;

public class OverloadPerform


{
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') {
int q = m / n;
System.out.println("Quotient: " + q);
} else if (ch == 'R') {
int r = m % n;
System.out.println("Remainder: " + r);
} else {
System.out.println("Invalid Character!");
}
}
public static void main(String[] args) {
OverloadPerform mo = new OverloadPerform();

// Calculating CSA of a cone


double csa = mo.perform(3.0, 4.0);
System.out.println("Curved Surface Area of Cone: "
+ csa);

// Generating pattern
mo.perform(4, 5);

// Printing quotient or remainder


mo.perform(20, 6, 'Q');
mo.perform(20, 6, 'R');
}
}

QUESTION 3
Define a class to overload the method display as follows:
void display( ): To print the following format using nested
loop
1
12
123
1234
12345
void display(int n): To print the square root of each digit
of the given number.
Example:
n = 4329
Output – 3.0
1.414213562
1.732050808
2.0
ANSWER
import java.util.Scanner;

public class MethodOverload


{
public void display()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(j + " ");
}
System.out.println();
}
}
public void display(int n)
{
while( n != 0)
{
int d = n % 10;
System.out.println(Math.sqrt(d));
n = n / 10;
}
}
public static void main(String args[])
{
MethodOverload obj = new MethodOverload();
Scanner in = new Scanner(System.in);
System.out.println("Pattern: ");
obj.display();
System.out.print("Enter a number: ");
int num = in.nextInt();
obj.display(num);
}
}

QUESTION 4
Define a class to overload the function print as follows:
void print() - to print the following format
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead
number. A lead number is the one whose sum of even
digits are equal to sum of odd digits.
e.g. 3669
odd digits sum = 3 + 9 = 12
even digits sum = 6 + 6 = 12
3669 is a lead number.
ANSWER
import java.util.Scanner;

public class MethodOverload


{
public void print()
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; j <= 4; j++)
{
System.out.print(i + " ");
}
System.out.println();
}
}
public void print(int n)
{
int d = 0;
int evenSum = 0;
int oddSum = 0;
while( n != 0)
{
d = n % 10;
if (d % 2 == 0)
evenSum += d;
else
oddSum += d;
n = n / 10;
}
if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Not a lead number");
}
public static void main(String args[])
{
MethodOverload obj = new MethodOverload();
Scanner in = new Scanner(System.in);
System.out.println("Pattern: ");
obj.print();
System.out.print("Enter a number: ");
int num = in.nextInt();
obj.print(num);
}
}

QUESTION 5
Design a class to overload a function series( ) as follows:
(a) void series (int x, int n) – To display the sum of the
series given below:
x1 + x2 + x3 + .......... xn terms
(b) void series (int p) – To display the following series:
0, 7, 26, 63 .......... p terms
(c) void series () – To display the sum of the series given
below:
1/2 + 1/3 + 1/4 + .......... 1/10
Answer
public class OverloadSeries
{
void series(int x, int n) {
long sum = 0;
for (int i = 1; i <= n; i++) {
sum += Math.pow(x, i);
}
System.out.println("Sum = " + sum);
}

void series(int p) {
for (int i = 1; i <= p; i++) {
int term = (int)(Math.pow(i, 3) - 1);
System.out.print(term + " ");
}
System.out.println();
}

void series() {
double sum = 0.0;
for (int i = 2; i <= 10; i++) {
sum += 1.0 / i;
}
System.out.println("Sum = " + sum);
}
}

QUESTION 6

Design a class to overload a function volume() as follows:


1. double volume (double R) – with radius (R) as an
argument, returns the volume of sphere using the
formula.
V = 4/3 x 22/7 x R3
2. double volume (double H, double R) – with height(H)
and radius(R) as the arguments, returns the volume
of a cylinder using the formula.
V = 22/7 x R2 x H
3. double volume (double L, double B, double H) – with
length(L), breadth(B) and Height(H) as the
arguments, returns the volume of a cuboid using the
formula.
V=LxBxH
Answer
public class Volume
{
double volume(double r) {
return (4 / 3.0) * (22 / 7.0) * r * r * r;
}
double volume(double h, double r) {
return (22 / 7.0) * r * r * h;
}
double volume(double l, double b, double h) {
return l * b * h;
}
public static void main(String args[]) {
Volume obj = new Volume();
System.out.println("Sphere Volume = " +
obj.volume(6));
System.out.println("Cylinder Volume = " +
obj.volume(5, 3.5));
System.out.println("Cuboid Volume = " +
obj.volume(7.5, 3.5, 2));
}
}

QUESTION 7
Using switch statement, write a menu driven program for
the following:
1. To find and display the sum of the series given
below:
S = x1 - x2 + x3 - x4 + x5 .......... - x20
(where x = 2)
2. To display the following series:
1 11 111 1111 11111
For an incorrect option, an appropriate error message
should be displayed.
Answer
import java.util.Scanner;
public class SeriesMenu
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("1. Sum of the series");
System.out.println("2. Display series");
System.out.print("Enter your choice: ");
int choice = in.nextInt();

switch (choice) {
case 1:
int sum = 0;
for (int i = 1; i <= 20; i++) {
int x = 2;
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum -= term;
else
sum += term;
}
System.out.println("Sum=" + sum);
break;
case 2:
int term = 1;
for (int i = 1; i <= 5; i++) {
System.out.print(term + " ");
term = term * 10 + 1;
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}

QUESTION 8
Design a class to overload a function check( ) as follows:
1. void check (String str , char ch ) — to find and print
the frequency of a character in a string.
Example:
Input:
str = "success"
ch = 's'
Output:
number of s present is = 3
2. void check(String s1) — to display only vowels from
string s1, after converting it to lower case.
Example:
Input:
s1 ="computer"
Output : o u e
Answer
public class Overload
{
void check (String str , char ch ) {
int count = 0;
int len = str.length();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (ch == c) {
count++;
}
}
System.out.println("Frequency of " + ch + " = " +
count);
}
void check(String s1) {
String s2 = s1.toLowerCase();
int len = s2.length();
System.out.println("Vowels:");
for (int i = 0; i < len; i++) {
char ch = s2.charAt(i);
if (ch == 'a' ||
ch == 'e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u')
System.out.print(ch + " ");
}
}
}

QUESTION 9
Design a class to overload a function sumSeries() as
follows:
(i) 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=x1−x2+x3−x4+x5... ... ... to n termss=1x−2x+3x−4x+5x...
... ... to n terms
(ii) void sumSeries(): to find and display the sum of the
following series:
s=1+(1×2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×20)s=1+(1×
2)+(1×2×3)+... ... ... +(1×2×3×4... ... ... ×20)
Answer
public class Overload
{
void sumSeries(int n, double x) {
double sum = 0;
for (int i = 1; i <= n; i++) {
double t = x / i;
if (i % 2 == 0)
sum -= t;
else
sum += t;
}
System.out.println("Sum = " + sum);
}
void sumSeries() {
long sum = 0, term = 1;
for (int i = 1; i <= 20; i++) {
term *= i;
sum += term;
}
System.out.println("Sum=" + sum);
}
}

QUESTION 10
Design a class to overload a function Joystring( ) as
follows:
1. void Joystring(String s, char ch1, char ch2) with one
string argument and two character arguments that
replaces the character argument ch1 with the
character argument ch2 in the given String s and
prints the new string.
Example:
Input value of s = "TECHNALAGY"
ch1 = 'A'
ch2 = 'O'
Output: "TECHNOLOGY"
2. void Joystring(String s) with one string argument
that prints the position of the first space and the last
space of the given String s.
Example:
Input value of s = "Cloud computing means Internet
based computing"
Output:
First index: 5
Last Index: 36
3. void Joystring(String s1, String s2) with two string
arguments that combines the two strings with a space
between them and prints the resultant string.
Example:
Input value of s1 = "COMMON WEALTH"
Input value of s2 = "GAMES"
Output: COMMON WEALTH GAMES
(Use library functions)
Answer
public class StringOverload
{
public void joystring(String s, char ch1, char ch2) {
String newStr = s.replace(ch1, ch2);
System.out.println(newStr);
}
public void joystring(String s) {
int f = s.indexOf(' ');
int l = s.lastIndexOf(' ');
System.out.println("First index: " + f);
System.out.println("Last index: " + l);
}
public void joystring(String s1, String s2) {
String newStr = s1.concat(" ").concat(s2);
System.out.println(newStr);
}
public static void main(String args[]) {
StringOverload obj = new StringOverload();
obj.joystring("TECHNALAGY", 'A', 'O');
obj.joystring("Cloud computing means Internet
based computing");
obj.joystring("COMMON WEALTH", "GAMES");
}
}

You might also like