WRITE A PROGRAM TO DEMONSTRATE THE POLYMORPHISM CONCEPT BY OVERLOADING THE ADD METHOD THAT ALLOWS ADDITION OF THE FOLLOWING NUMBERS:
-TWO INTEGER NUMBER
-TWO FRACTIONAL NUMBERS
-THREE INTEGER NUMBERS
ANSWER
public class addition {
public int add(int num1 , int num2) {
return num1 + num2;
}
public double add (double num1, double num2 ) {
return (double) num1 + num2;
}
public int add(int num1 , int num2 , int num3) {
return num1 + num2 + num3;
}
public static void main (string[] args) {
int a = 10, b = 15, c = 7;
double x = 4.5, y = 3.2;
addition obj = new addition ();
system.out.println (" the addition of two integer numbers is " + obj.add(a , b) );
system.out.println (" the addition of two fractional numbers is " + obj . add(x , y) ) ;
system.out.println (" the addition of three integer numbers is " + obj . add (a,b,c) ) ;
}
}
-TWO INTEGER NUMBER
-TWO FRACTIONAL NUMBERS
-THREE INTEGER NUMBERS
ANSWER
public class addition {
public int add(int num1 , int num2) {
return num1 + num2;
}
public double add (double num1, double num2 ) {
return (double) num1 + num2;
}
public int add(int num1 , int num2 , int num3) {
return num1 + num2 + num3;
}
public static void main (string[] args) {
int a = 10, b = 15, c = 7;
double x = 4.5, y = 3.2;
addition obj = new addition ();
system.out.println (" the addition of two integer numbers is " + obj.add(a , b) );
system.out.println (" the addition of two fractional numbers is " + obj . add(x , y) ) ;
system.out.println (" the addition of three integer numbers is " + obj . add (a,b,c) ) ;
}
}
No comments:
Post a Comment