ImportJava.util.*; Public classtest01{//Test Class Public Static voidmain (String [] args) {finaltest (); } Public Static voidfinaltest () { while(true){
Instantiate an object of a computed class, set the value by the set method, get the value operationtest p=Newoperationtest (); P.SETNUM1 (NewScanner (system.in). nextdouble ()); String s=NewScanner (system.in). Next (); P.setnum2 (NewScanner (system.in). nextdouble ()); Test (P.getnum1 (), s,p.getnum2 ()); } } Public Static voidTest (DoubleNum1,string E,Doublenum2) { FinalString a = "+", b= "-", c= "*", d= "/"; Switch(e) { CaseA:system.out.println (num1+e+num2+ "=" +Operationadd.getresult ()); Break; CaseB:system.out.println (num1+e+num2+ "=" +Operationsub.getresult ()); Break; CaseC:system.out.println (num1+e+num2+ "=" +Operationmul.getresult ()); Break; CaseD:system.out.println (num1+e+num2+ "=" +Operationdiv.getresult ()); Break; }}}//the parent class of the compute classclassoperationtest{ Public Static DoubleNUM1; Public Static Doublenum2; Get Set Method Publicoperationtest () {} Public DoublegetNum1 () {returnNUM1; } Public DoublegetNum2 () {returnnum2; } Public voidSETNUM1 (DoubleNUM1) { This. NUM1 =NUM1; } Public voidSETNUM2 (Doublenum2) { This. num2 =num2; } Public Static DoubleGetResult () {DoubleResult =0; returnresult; }}//class that computes addition inherits from the calculated classclassOperationaddextendsoperationtest{ Public Static DoubleGetResult () {returnnum1+num2; }}//classes that compute subtraction inherit from a calculated classclassOperationsubextendsoperationtest{ Public Static DoubleGetResult () {returnNum1-num2; }}//classes that compute multiplication inherit from the calculated classclassOperationmulextendsoperationtest{ Public Static DoubleGetResult () {returnNum1*num2; }}//classes that compute division inherit from a calculated classclassOperationdivextendsoperationtest{ Public Static DoubleGetResult () {returnnum1/num2; }}
Java Console Implementation Calculator [Object oriented thinking-02]