简体   繁体   English

简单计算器的实现

[英]implementation of simple calculator

Create a program which simulates a very simple calculator 创建一个模拟非常简单的计算器的程序

So I have been asked to implement an abstract class that represents binary (having 2 arguments) arithmetic expression 因此,我被要求实现一个代表二进制(具有2个参数)算术表达式的抽象类

abstract class ArithmeticExpression {

    double binary1;
    double binary2;

    public abstract void evaluate ();

    public abstract void display ();

}

so then I created sub classes add, multiply, subtract, and divide. 因此,我创建了子类加,乘,减和除。 In subtract I have: 在减去我有:

public class subtract extends ArithmeticExpression {
    // private double result;
    double binary1;
    double binary2;

    public subtract (double x, double y) {
        this.binary1 = x;
        this.binary2 = y;
    }

    public  void evaluate () {

        System.out.println("Expression: " + getsubX() + " - " + getsubY() + " = ");

        // return result;
    }

    public  void display () {

    }

    public double getsubX() {

    }

    public double getsubY() {

    }

Using the classes I should be able to represent any arbitrary expression, with no hard coding. 使用这些类,我应该能够表示任何任意表达式,而无需进行硬编码。

It is also said evaluate should return the result as double and display method should print the expression out in string. 也有人说,评估应将结果返回为double值,并且显示方法应将表达式以字符串形式打印出来。 Am I on the right track? 我在正确的轨道上吗? What am I missing here? 我在这里想念什么? The part I do not understand how it is able to represent any expression? 我不了解的部分如何能够表示任何表达式?

If you want to evaluate any expession inserted in form of 如果要评估以以下形式插入的任何支出

4 + 3 * ( 4 + 5)

You either need to create binary tree or stack and fill those values and operators in. 您需要创建二叉树或堆栈并填充这些值和运算符。

What I quite dont understand is your so called binary represented in double. 我不太了解的是您所谓的以双精度表示的二进制文件。 If you want to have binary calc, you should use unsigned int or long (or any other type, that is not floating point) 如果要使用二进制计算,则应使用unsigned int或long(或任何其他类型,不是浮点数)

Using your abstract ArithmeticExpression, here's what the Subtract class should look like. 使用抽象的ArithmeticExpression,这是Subtract类的外观。 Java classes start with a capital letter. Java类以大写字母开头。

public class Subtract extends ArithmeticExpression {
    double  result;

    public Subtract(double x, double y) {
        this.binary1 = x;
        this.binary2 = y;
    }

    @Override
    public void evaluate() {
        result = binary1 - binary2;
    }

    @Override
    public void display() {
        System.out.println("Expression: " + binary1 + 
                " - " + binary2 + " = " + result);
    }

}

You don't have to re-declare binary1 and binary2. 您不必重新声明binary1和binary2。 They are instantiated in the abstract ArithmeticExpression class. 它们在抽象的ArithmeticExpression类中实例化。

You do have to provide a double for the result. 您必须为结果提供双倍。 This should have been done in the abstract ArithmeticExpression class. 这应该在抽象的ArithmeticExpression类中完成。

The evaluate() method is for evaluation. validate()方法用于评估。

The display() method is for display. display()方法用于显示。

You don't have to define any other methods in your Subtract concrete class. 您无需在Subtract具体类中定义任何其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM