0

This is supposed to have a Processor, Expression, Sum, Number and Product class. Processor execute Expression and expression should execute Sum and Product maybe even number. I'm not too sure about that.

This goes to Number class Expression e1 = new Number(2.0);

        public Number(double operand) {
        Double code = new Double(operand);
           code.toString();
            System.out.println("double : " + code);
           return;

This passes fine. I get the number

public class TestSOP  {
public static void main(String[] args) {
    Processor proc = new Processor();
    Expression e1 = new Number(2.0);
    Expression e2 = new Number(3.1);
    Expression e3 = new Number(-5.0);
    Sum s1 = new Sum(e1, e2, e3);
    System.out.println("s1 - " + proc.execute(s1));
    Product p1 = new Product(s1, e3);
    System.out.println("s1 - " + proc.execute(s1));
    // etc.
}

}

The problem is this line Sum s1 = new Sum(e1, e2, e3); the Sum has a constructor that has to pass the expression. I have tried many different ways after passing the operand to get it into a double but all I get is SOP.Number@566776ad. I'm not sure what to do or try next Bellow is the Sum Class.

public  class Sum implements Expression   { 
 public Sum(Expression ...operand ) {
SOP.Number.Numbers(operand); }
Voz zzz
  • 25
  • 4
  • `SOP.Number` does **not** override `toString`. Fix that and your problem will go away. – Elliott Frisch Feb 21 '20 at 06:31
  • Are you talking about operand.toString(); because I still get [LSOP.Expression;@6d06d69cs1 – Voz zzz Feb 21 '20 at 06:37
  • You're printing an object of type 'Numbers'. This class has no toString so therefore it uses Object.toString. Implement a toString method or create a new method which returns the double and prints that. Be aware of calculating with double values, see https://stackoverflow.com/questions/3413448/double-vs-bigdecimal – R.Groote Feb 21 '20 at 06:37

1 Answers1

1

Firstly, System.out.println() calls passed in object's toString() function to convert the object to a string representation, so you don't need to convert it beforehand. Also, if you do so, you would need to assign it to a variable:

String printCode = code.toString(); // redundant
System.out.println("double : " + printCode);

Double overrides toString() method (see https://docs.oracle.com/javase/8/docs/api/?java/lang/Double.html), hence your number is printed, while Number only inherits it from Object class (see https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html).

You could write your own class which extends Number class, and override toString() method.

Sabina Orazem
  • 477
  • 4
  • 12