Rotate your phone or change to desktop for better experience

Rotate your phone or change to desktop for better experience

Pgm 5 || Program to perform mathematical operations. Create a class called AddSub with methods to add and subtract. Create another class called MulDiv that extends from AddSub class to use the member data of the super class. MulDiv should have methods to multiply and divide A main function should access the methods and perform the mathematical operations.

 class addsub

{

int num1,num2;

addsub(int n1, int n2)

{

num1 = n1;

num2 = n2;

}

int add()

{ return num1+num2;

}

int subtract()

{ return num1-num2;

}

}

class multdiv extends addsub

{

public multdiv(int n1, int n2)

{

super(n1, n2);

}

int multiply()

{

return num1*num2;

}

float divide ()

{

return num2/num1;

}

public void display()

{

System.out.println("Number 1 :" + num1);

System.out.println("Number 2 :" + num2);

}

}

public class Pgm5

{

public static void main(String arg[])

{

addsub r1=new addsub(50,20);

int ad = r1.add();

int sb = r1.subtract();

System.out.println("Addition =" +ad);

System.out.println("Subtraction =" +sb);

multdiv r2 =new multdiv(4,20);

int ml = r2.multiply();

float dv =r2.divide();

System.out.println("Multiply =" +ml);

System.out.println("Division =" +dv);

}

}

Post a Comment

0 Comments