Java Starts from scratch 17 (simple factory)

Source: Internet
Author: User

implementation of a simple factory

Implement a calculator: Require 2 numbers, and operators, to get results

Operation class
 PackageCom.pb.demo1; Public classOperation {Private DoubleNUM1; Private Doublenum2;  Public DoubleGetResult (DoubleNUM1,Doublenum2) {        DoubleResult=0; returnresult; }     Public DoublegetNum1 () {returnNUM1; }     Public voidSETNUM1 (DoubleNUM1) {         This. NUM1 =NUM1; }     Public DoublegetNum2 () {returnnum2; }     Public voidSETNUM2 (Doublenum2) {         This. num2 =num2; }    }
add, subtract, behave, divide, take the mold
 package   Com.pb.demo1;  /*   * addition  */ public  class  Addoperation extends   operation {@Override public  double  num1,  num2) {return  (Num1+num2); }}
 package   Com.pb.demo1;  /*   * subtract  */ public  class  Substruction extends   operation {@Override public  double  num1,  num2) { Span style= "color: #0000ff;"    >return  (Num1-num2); }    }
 package   Com.pb.demo1;  /*   good method  */ public  class  Multiplication extends   operation {@Override public  double  num1,  num2) {return  (Num1*num2); }    }
 package   Com.pb.demo1;  /*   Division  */ public  class  Division extends   operation {@Override  public  double  GetResult (double  NUM1, double   num2) { return  (Num1/num2); }    }
 Package Com.pb.demo1; /*  */Publicclassextends  operation    {    @Override  publicDouble GetResult (doubledouble  num2) {                return (num1%num2);    }    }
Factory class:
 PackageCom.pb.demo1;/** Simple Factory class*/ Public classFactory {//return type is class object     PublicOperation GetResult (String op) {operation Operation=NULL; //declares an object according to an operator and returns        Switch(OP) { Case"+": Operation=Newaddoperation ();  Break;  Case"-": Operation=Newsubstruction ();  Break;  Case"*": Operation=Newmultiplication ();  Break;  Case"/": Operation=NewDivision ();  Break;  Case"%": Operation=Newmodeoperation ();  Break; default: System.err.println ("The input operator is incorrect!");  Break; }        //returns the generated object        returnoperation; }}
Test class:
 PackageCom.pb.demo1;Importjava.util.InputMismatchException;ImportJava.util.Scanner;/** Calculator*/ Public classDemo1 { Public Static voidMain (string[] args) {//declaring a scanner type variableScanner input =NewScanner (system.in); //declaring a factory objectFactory fac=NewFactory (); Try{System.out.println ("Please enter the first integer:"); Doublenum1=input.nextdouble (); System.out.println ("Please enter a second integer:"); DoubleNum2=input.nextdouble (); System.out.println ("Please lose operator: + 、-、 *,/,%"); String op=Input.next (); //The returned object factory of the receiving factory generates objects based on the operatorOperation Operation=Fac.getresult (OP); //Receive calculation results            Doubleresult=Operation.getresult (NUM1, num2); //Show Calculation ResultsSystem.out.println (num1+ "and" +num2+ "Running results:" +result); }Catch(inputmismatchexception e) {System.err.println ("The input type is incorrect!"); }Catch(Exception e) {e.printstacktrace (); }    }}

Java Starts from scratch 17 (simple factory)

Related Article

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.