What is wrong with this?
I am trying to make a program that does simple binary operations, it is not working. I think I am missing some constructors, but don't know what they are. Any help would be appreciated.
//Expression.java
package Math;
publicabstractclass Expression{
publicabstract String toString();
publicabstractdouble evaluateMe();
publicstaticvoid main(String args[]){
Sum s1 =new Sum(new Number(7.123456789),new Number(11.3344));
Sum s2 =new Sum(s1,new Multiply(s1,new Number(3)));
System.out.println("s2 = " + s2 +" with value " + s2.evaluateMe());
}
}
//Number.java
package Math;
publicclass Numberextends Expression{
double num;
public String toString(){
return String.valueOf(num);
}
//return num.decimalFormat()
//return the number but to 2nd place
publicdouble evaluateMe(){
return num;
}
}
//Sum.java
package Math;
publicclass Sumextends Expression{
Expression left;
Expression right;
public String toString(){
return"(" + left.toString() +")" +"+" +"(" + right.toString() +")";
}
publicdouble evaluateMe(){
return left.evaluateMe() + right.evaluateMe();
}
}
//Product.java
package Math;
publicclass Productextends Expression{
Expression left;
Expression right;
public String toString(){
return"(" + left.toString() +")" +"*" +"(" + right.toString() +")";
}
publicdouble evaluateMe(){
return left.evaluateMe() * right.evaluateMe();
}
}
Once again Thanks
[4295 byte] By [
AdiVa] at [2007-11-27 11:14:45]

yes you are missing constructors (and a lot of other things). I'm sorry , but this code is too messed up for me to know where to begin to fix. Maybe someone else will have better luck. Please go through the Sun java tutorials and learn how to code (including constructors) from the ground up. Good luck!
Ok, sorry for the bad code, but I just started and need some help. I did go through the tutorials, but i guess I have to just read again. Thank you anyways, and I do hope someone here can help me out with this. But can you tell me what else I am missing?
Message was edited by:
AdiV
AdiVa at 2007-7-29 14:08:42 >

I think your biggest problem is that you think this is Perl.
You cant multiply 2 Strings together.
A CAS (computer algebra system) isnt THAT hard but it does
require definite programming competence.
Perhaps you should hold off on an attempt this early on?
well this is a project I had to submit and in the guidlines it uses those commands in Expression class to have the output display:
s2 = ((7.12 + 11.33) + ((7.12 + 11.33) * 3)) with value 73.831427156.
In fact this is exactly what it says:
In object oriented parlance the five types of binary operation expressions will each have two (sub) expressions, a left hand side and a right hand side (or in the case of a quotient a numerator and a denominator). Thus each binary operation class will have a constructor that takes two other Expression objects as arguments.
Numbers on the other hand will only store a single double. Thus their only constructor will take a double.
All expressions know how to answer two messages: the toString() method which will provide a String representation of the expression the object represents. On the other hand, the evaluateMe() method will cause the expression to compute its value and return it (as a double). There is only one twist to this, and that is the toString() of numbers should only print out to two decimal places (using a java.text.DecimalFormat object). To give you some idea of what is required, consider the following code:
Sum s1 = new Sum(new Number(7.123456789), new Number(11.3344));
Sum s2 = new Sum(s1, new Product(s1, new Number(3)));
System.out.println("s2 = " + s2 + " with value " + s2.evaluateMe());
This should, when executed print out:
s2 = ((7.12 + 11.33) + ((7.12 + 11.33) * 3)) with value 73.831427156
Try getting the Number and Sum classes working first. The other classes are only very minor variations on these.
The toString() method should hold no surprises really, except that in the Number version you'll have to format the return value a bit. But check out either the solutions to the first assignment or the description of the first project. The evaluateMe() method is also not very complex. In the Number case it should just return the stored value, while in the Sum case it will just ask its two subexpressions to evaluate themselves, and then return the resulting sum of those doubles.
AdiVa at 2007-7-29 14:08:42 >

Basically youre just building a binary tree.
The Numbers are leafs and the Expressions are Nodes that return a Number.
Yes, this was the point of the excerises, to work with hiearchies. But how do you propose to fix this? Should I delete one of the expressions?
AdiVa at 2007-7-29 14:08:42 >

Expression looks like it should be an interface. Not an abstract class.
Then AbstractExpression should implement expression and have the left and right expressions.
Then concrete implementations will extend AbstractExpression.
Well, instead of a Class for every different type of Operation,
I would have a field in Expression hold the Operation type.
expression{
int operation;
static final int ADD = 1, SUBTRACT = 2... etc
public double evaluate(){
switch(operation){
case ADD: return X + Y;
...
}
}
}
First though you need to fix your Expression interface which is borked. Having toString is fine but you need to be storing the actual numbers. As mentioned you can't do mathematical operations on a String. This is not a scripting language.
So you need to have real numbers. I don't know if you want to use primitives like floats or something like BigDecimal but if I were you I would figure that out first.
As others have mentioned but just to be clear you are a long way from anything good here. Just want you to have realistic expectations about how long this will take you.
> Well, instead of a Class for every different type of
> Operation,
> I would have a field in Expression hold the Operation
> type.
>
And then I will beat you with a stick.
For one thing if you really must do it that way, which IMO is not a good way, you should be using an enum
Thanks I will try to do what I can, and don't get me wrong I fully understand that I am not close to finsihing this. I just thought I could get some help here by asking. But I will take everyones advice into consideration and try to make this work.
AdiVa at 2007-7-29 14:08:42 >

HERE YOU GO!
YOU OWE ME BIG!
public class MathMachine{
public static void main(String[] args){
new MathMachine();
}
public MathMachine(){
Expression e1 = new Expression(Operation.ADD, 5, 3);
Expression e2 = new Expression(Operation.MULTIPLY, new Number(2), e1);
System.out.println("Expression: " + e2.toString() + " = " + e2.evaluate().getValue());
}
// EVALUATOR
public interface Evaluator{
public String toString();
public Number evaluate();
}
// NUMBER
public class Number implements Evaluator{
public Number(double number){ this.number = number; }
public String toString(){ return "" + number; }
public Number evaluate(){ return this; }
public double getValue(){ return number; }
double number;
}
// EXPRESSION
public class Expression implements Evaluator{
public Expression(int operation, double x, double y){
this.operation = operation;
this.x = new Number(x);
this.y = new Number(y);
}
public Expression(int operation, Evaluator x, Evaluator y){
this.operation = operation;
this.x = x;
this.y = y;
}
public String toString(){
return "(" + x.toString() + Operation.getOperatorString(operation) + y.toString() + ")";
}
public Number evaluate(){ return new Number(Operation.evaluate(this)); }
int operation;
Evaluator x;
Evaluator y;
}
// OPERATION
public static class Operation{
static final int ADD = 1;
static final int SUBTRACT = 2;
static final int MULTIPLY = 3;
static final int DIVIDE = 4;
public static String getOperatorString(int operator){
switch(operator){
case ADD: return "+";
case SUBTRACT: return "-";
case MULTIPLY: return "*";
case DIVIDE: return "/";
default: return "error";
}
}
public static double evaluate(Expression e){
switch(e.operation){
case ADD: return e.x.evaluate().getValue() + e.y.evaluate().getValue();
case SUBTRACT: return e.x.evaluate().getValue() - e.y.evaluate().getValue();
case MULTIPLY: return e.x.evaluate().getValue() * e.y.evaluate().getValue();
case DIVIDE: return e.x.evaluate().getValue() / e.y.evaluate().getValue();
default: return Double.NaN;
}
}
}
}
> HERE YOU GO!
> YOU OWE ME BIG!
Caveat emptor.
Well, you *do* get what you pay for...
~
Thanks a lot, altogh not keeping with the heirchacy that was required, I Realy really apperciate this, since now I can see how it works. Thanks a lot!
AdiVa at 2007-7-29 14:08:46 >

> Thanks a lot, altogh not keeping with the heirchacy
> that was required, I Realy really apperciate this,
> since now I can see how it works. Thanks a lot!
It's a really shitty implementation.
>> It's a really shitty implementation.
wtfe @sshole.
i took 10 mins to implement his 3 classes in a way that worked.
did you want me to write macsyma?
Not to sound rude, but at least he tried to help out the most out of everyone here.
AdiVa at 2007-7-29 14:08:46 >

> >> It's a really shitty implementation.
>
> wtfe @sshole.
> i took 10 mins to implement his 3 classes in a way
> that worked.
It's a shitty design. End of story.
If the OP doesn't know how to implement and needs examples then (a) he should look at the tutorials (b) he should look for design help with the guidance others including myself gave in this thread.
Giving the OP a quick and dirty and ultimatley shitty implementation is of no use to anyone.
> Not to sound rude, but at least he tried to help out
> the most out of everyone here.
That is your opinion which you are entitled to but is incorrect all the same. I am telling you it is a shitty implementation because it is. It does not teach you the right way to do this which is important.
If you want to just latch on to what TuringPest posted then go right ahead, nobody is stopping you, but to paraphrase yawmark "You get what you pay for" You paid nothing. You got nothing there.
My suggestions were good ones and offered things to think about. If you don't know Java then you should be going through the tutorials available on this site.
It will not do you any good in the long term to use TuringPests code. In fact it will do you harm.
Cotton, do you ever get tired of being full of noise?
Below is a copy and paste of every good suggestion you made.
(1) Expression (renamed evaulator because thats what it does) IS
an interface.
(2) AbstractExpression (now Expression) does exactly what you say.
(3) His assignment is to do a simple BinaryTree so its not necessary to
make Expression abstract and then make a concrete implementation for every single type of operation.
(4) It was borked. He did need numbers. And he was only 10 mins away from getting it to work.
(5) Instead of an Enum (J5 only) I used a class Operation.ADD
BECAUSE for his toString() method he needed each operation to
be able to print itself. The switch was the fastest way I could type it.
None of your godsent advice contradicts what I did in the example.
(1)
Expression looks like it should be an interface. Not an abstract class.
(2)
Then AbstractExpression should implement expression and have the left and right expressions.
(3)
Then concrete implementations will extend AbstractExpression.
(4)
First though you need to fix your Expression interface which is borked. Having toString is fine but you need to be storing the actual numbers. As mentioned you can't do mathematical operations on a String.
This is not a scripting language.
So you need to have real numbers. I don't know if you want to use primitives like floats or something like BigDecimal but if I were you I would figure that out first.
As others have mentioned but just to be clear you are a long way from anything good here. Just want you to have realistic expectations about how long this will take you.
(5)
And then I will beat you with a stick.
For one thing if you really must do it that way, which IMO is not a good way, you should be using an enum
