Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Calculator program java
    primarykey
    data
    text
    <p>I'm supposed to create a program which simulates a very simple calculator</p> <p>So I have been asked to implement an abstract class that represents binary (having 2 arguments) arithmetic expression. I've also made 4 subclasses - division, addition, etc. </p> <p>However, i need to use these classes to be "able to represent any arbitrary expression (i.e. an expression with an arbitrary number of terms), for example an expression like <code>(5.0+8.1)*(2.0)</code> or <code>((5.0+8.1)*(2.0))/12.5</code> as quoted by my crswork spec. </p> <p>In other words, my calculator must be able to use any arithmetic expression. </p> <p>Pls help. </p> <pre><code>public abstract class ArithmeticExpression { // base abstract superclass double left; double right; double result; public abstract void evaluate(); public abstract void display(); } // ------------------------------------ Addition subclassed from base class public class Addition extends ArithmeticExpression { public Addition(double x, double y) { this.left = x; this.right = y; } @Override public void evaluate() { result = left + right; } @Override public void display() { System.out.println ("The expression and result is: " + left + " + " + right + " = " + result); } } // ------------------------------------ Subtract subclassed from base class public class Subtract extends ArithmeticExpression { public Subtract(double x, double y) { this.left = x; this.right = y; } @Override public void evaluate() { result = left - right; } @Override public void display() { System.out.println ("The expression and result is: " + left + " - " + right + " = " + result); } } // ------------------------------------ Mulitplication subclassed from base // class public class Multiplication extends ArithmeticExpression { public Multiplication(double x, double y) { this.left = x; this.right = y; } @Override public void evaluate() { result = left * right; } @Override public void display() { System.out.println ("The expression and result is: " + left + " * " + right + " = " + result); } } // ------------------------------------ Division subclassed from base class public class Division extends ArithmeticExpression { public Division(double x, double y) { this.left = x; this.right = y; } @Override public void evaluate() { result = left / right; } @Override public void display() { System.out.println("The expression and result is: " + left + " / " + right + " = " + result); } } </code></pre> <p>this is my attampt at making evaluate method return value. Does it work?</p> <pre><code>public abstract ArithmeticExpression evaluate(); //------------------------------------ Addition subclassed from base class public class Addition extends ArithmeticExpression { public Addition(double x, double y) { this.left = x; this.right = y; } @Override public ArithmeticExpression evaluate() { result = left + right; return this; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload