Java Console Implementation Calculator [Object oriented thinking-02]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.