java面向对象计算器

package classCode.practice6;
import java.util.Scanner;
/**
 *
* @ClassName: Calculator 
* @Description: 计算器类
* @author
*
 */
public class Calculator {
 public static void main(String[] args) {
//  Operation oper;
//  oper=OperationFactory.createOperate('+');
//  System.out.println("请输入_numberA:");
//  Scanner s = new Scanner(System.in);
//  oper.setNumberA(s.nextDouble());
//  System.out.println("请输入_numberB:");
//  oper.setNumberB(s.nextDouble());
//  double result = oper.getResult();
//  System.out.println("结果是:"+result);
  
//  
//  Operation oper;
//  Scanner s = new Scanner(System.in);
//  System.out.println("请输入运算符(+ - * /):");
//  String operChar = s.next();
//  switch(operChar){
//   case "+" : oper = new OperationAdd();
//   case "-" : oper = new OperationSub();
//   case "*" : oper = new OperationMul();
//   case "/" : oper = new OperationDiv();
//   default:
//   oper = new OperationAdd();
//  }
//  System.out.println("请输入_numberA:");
//  oper.setNumberA(s.nextDouble());
//  System.out.println("请输入_numberB:");
//  oper.setNumberB(s.nextDouble());
//  double result = oper.getResult();
//  System.out.println("计算结果是:"+result);
//  s.close();
//  
//  System.out.println("请输入_numberA:");
//  Scanner s = new Scanner(System.in);
//     int num1 = s.nextInt();
//  System.out.println("请输入_numberB:");
//  int num2 = s.nextInt();
//  System.out.println("请输入运算符(+ - * /):");
//  String oper = s.next();
//  switch(oper){
//  case "+" : System.out.println(num1+num2);
//  case "-" : System.out.println(num1-num2);
//  case "*" : System.out.println(num1*num2);
//  case "/" : System.out.println(num1/num2);
//  case "%" : System.out.println(num1%num2);
//  }
 }
}
--------------------------------------------------------------------
package classCode.practice6;
/**
 *
 * @ClassName: Operation 
 * @Description: 操作类的父类
 */
public class Operation {
    private double numberA = 0;
    private double numberB = 0;
   
    public double getResult(){
     double result =0;
     return result;
    }
 public double getNumberA() {
  return numberA;
 }
 public void setNumberA(double numberA) {
  this.numberA = numberA;
 }
 public double getNumberB() {
  return numberB;
 }
 public void setNumberB(double numberB) {
  this.numberB = numberB;
 } 
}
-----------------------------------------------------------
package classCode.practice6;
/**
 *
 * @ClassName: OperationAdd 
 * @Description: 加法操作类
 */
public class OperationAdd extends Operation{
 public double getResult(){
  double result =0;
  result =getNumberA()+getNumberB();
  return result;
 }
}
---------------------------------------------------------------------
package classCode.practice6;
/**
 *
 * @ClassName: OperationAdd 
 * @Description: 除法操作类
 */
public class OperationDiv extends Operation{
 public double getResult(){
  double result =0;
  result =getNumberA()/getNumberB();
  return result;
 }
}
------------------------------------------------------------
package classCode.practice6;
import java.util.Hashtable;
/**
 *
 * @ClassName: OperationFactory 
 * @Description: 操作工厂类
 */
public class OperationFactory {
    public static Operation createOperate(char operate){
     Operation oper = null;
     switch(operate){
     
     case '+':
      oper = new OperationAdd();
      break;
     case '-':
      oper = new OperationSub();
      break;
     case '*':
      oper = new OperationMul();
      break;
     case '/':
      oper = new OperationDiv();
      break;
     default:
      break;
     }
     return oper;
    }
}
-----------------------------------------------------
package classCode.practice6;
/**
 *
 * @ClassName: OperationMul 
 * @Description: 乘法操作类
 */
public class OperationMul extends Operation{
 public double getResult(){
  double result =0;
  result =getNumberA()*getNumberB();
  return result;
 }
}
------------------------------------------------------
package classCode.practice6;
/**
 *
 * @ClassName: OperationAdd 
 * @Description: 减法操作类
 */
public class OperationSub extends Operation{
 public double getResult(){
  double result =0;
  result =getNumberA()-getNumberB();
  return result;
 }
}

你可能感兴趣的:(基础)