1. Define a class Math_1 which has twomember functions: plus() and subtract() .
ID: 3611257 • Letter: 1
Question
1. Define a classMath_1 which has twomember functions: plus()and subtract(). Both take twointeger arguments. The functions calculate the sum and differenceof the two integers, respectively. Then declare a classMath_2 to inheritMath_1, and Math_2 also has two member functionsmultiply() and divide(). Similarly, bothtake two integer arguments; and these functions will calculate theproduct and division results of the two integers.
Declare a Math_2 type variable in main(), and twointeger variables. Assign integers using cin and prompt the user tomake one of the four choices: 1, 2, 3, or 4; which means the fourmember functions defined above (plus, subtract, multiply, anddivide), respectively. Then call the related member function tocalculate and output the result.
Explanation / Answer
Class Math1 public class Math1 { public static int plus(int num1, int num2){ int result = num1 + num2; return result; } public static int subtract(int num1, intnum2){ int result = num1 - num2; return result; } } Class Math2 public class Math2 extends Math1{ public static int multiply(int num1, intnum2){ int result = num1 * num2; return result; } public static int divide(int num1, intnum2){ int result = num1 / num2; return result; } public static void main(String args[]){ int n = 4, m = 2; int result; //Math2 object = newMath2(); result = plus(n,m); System.out.println(result); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.