Below Program is a Demonstrate usage of Class, Objects, variables and Methods. Calculator Class Source File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package com.mypacakage.demo; class CalculatorClass { int val1; int val2; int result; String operation; //Call Below Function after Object creation with arguments //These function will set Result instance variable and display the result //Addition Function public void addition(int arg1, int arg2){ result = arg1+arg2; } //Subtraction Function public void subtraction(int arg1, int arg2){ result = arg1-arg2; } //Multiplication Function public void multiplication(int arg1, int arg2){ result = arg1*arg2; } //Division Function public void division(int arg1, int arg2){ result = arg1/arg2; } //Call any Operation //Function to Demonstrate that while Object is created from other Class Function, //we can set the Class instance variable val1 and val2 and just call the this function public void call_any_ops(char ops){ switch(ops){ case 'A': addition(val1,val2); break; case 'S' : subtraction(val1,val2); break; case 'D' : multiplication(val1,val2); break; case 'M' : division(val1,val2); break; } //Call Show Result Method showresult(); } public void showresult(){ System.out.println("Result of the Operation: " + result); } } |
CalculatorTest Class Source…