Design a sub-class of SimpleCalculator called Calculator using psudoecode that contains public methods named multiply(), and divide() and that defines private fields called num1 and num2. Each of the methods accepts two numeric arguments and returns a number that represents the result of the appropriate Calculator operation. Design a two-arg constructor Calculator() that sets the two values num1 and num2. Design a main() method that prompts the user for two numbers and reads them in. Then creates an instance of the Calculator class setting the input values via the Calculator constructor. The main() method then tries each of the four arithmetic methods. Assume that an CalculatorException is created and thrown if the user attempts to divide by 0. Display an appropriate message when an exception is caught. If no exception, display the results from each arithmetic method call.
1 answer
Public Static num add(num x, num y)
num sum
sum= x+y
Return sum
Public Static num sub(num x, num y)
num subtract
subtract= x- y
Return subtract
Endclass
Class Calculator inheritsFrom SimpleCalculator
// Declarations
Private num num1
Private num num2
Public Static num multiply(num x, num y)
num multiply
multiply= x*y
Return multiply
Public Static num divide(num x, num y)
Num divide
divide= x/y
Return divide
Public Calculator(num x, num y)
num1=x
num2=y
Public num add()
Return(num1+num2)
Public num subtract()
Return (num1-num2)
Public num multiply()
Return (num1*num2)
Public num divide()
Return (num1/num2)
Public static void main ()
// Declaration
Num a
Calculator c1= new Calculator(numx, num y)
a=c1.add(6,9)
Output “ the sum is” + a
a=c1.multiply(2,10)
Output” the result of multiplication” + a
a=c1.subtract(6,9)
Output “ the difference is” + a
try
a=c1.divide(5,0)
a=c1.divide()
endtry
catch( ArithmeticException mistake)
output “cannot divide by 0”
endcatch
Output “ the result of division is” + a
return
endmain
endclass