Question 8



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=pxq 
p=(a+b)/c  
q=a+b+c
        

Solution:

,,
 
import java.util.Scanner;
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) 
{
System.out.println("Quotient: " + (m / n));
} 
else 
{
System.out.println("Sum: " + (2 * n + 3 * m));
}
}
double display(double a, double b, double c) 
{
double p = (a + b) / c;
double q = a + b + c;
double z = p * q;
return z;
}
public static void main(String[] args) 
{
OverloadDisplay obj = new OverloadDisplay();
Scanner sc = new Scanner(System.in);
obj.display();
System.out.print("Enter n and m for second display: ");
int n = sc.nextInt();
int m = sc.nextInt();
obj.display(n, m);
System.out.print("Enter a, b, c for third display: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double res =obj.display(a, b, c);
System.out.println("z="+res);
}
}
                                                                  


                                            



Reach Us

SERVICES

  • ACADEMIC
  • ON-LINE PREPARATION
  • FOUNDATION & CRASH COURSES

CONTACT

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+918081967119