OVERLOAD
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;
// Generating pattern
mo.perform(4, 5);
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;
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;
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
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");
}
}