Laboratory Two Discussion

If you have questions or comments about this laboratory, please post them here.Remember, this conversation should adhere to the Computer Science Department's Honor Code; it is acceptable to conduct high level conversations, but it is not acceptable to share source code! Remember, post your questions here instead of sending them to me via email! Greg


IntegerWrapper

We've drawn up a picture based on studying the "Reflect on the Visitor Design Pattern" and "The Visitor Design Pattern" web pages. We're now adding code in but we're not sure where to put the iterator. According to the "Reflect" web site it should go in the PostfixOperatorEvaluatorVisitor class.

As we have our code right now, we have PostfixEvaluator main's making an array and calling evaluate. Evaluate calls PostfixOperatorEvaluatorVisitor's vistArrayList function which iterates through the arraylist, calling Object's accept on each iteration. Accept then calls the proper visit method inside PostfixOperatorEvaluatorVisitor - each visit pushes its result onto the stack. It seems a little strange to have an evaluate function that just calls another function but we'll see how things go once we get it to compile and run.

(All inside the PostfixOperatorEvaluatorVisitor:)
We're now stuck on two compile errors: "non-static variable this cannot be referenced from a static context - o.accept(this);" and "cannot resolve symbol - method accept - class java.lang.Object - o.accept(this);" Hmm...We fixed the first but it still doesn't know what accept is.

We've now changed our errors...We made Operator implement Visitable and then we casted the o in "o.accept(this)" to visitable as done on the Reflect page. Our new errors are as follows: "cannot implement accept(PosfixOperatorEvaluatorVisitor) in Viistable; attempting to assign weaker acces priviledges; was public - public abstract class Operator implements Visitable( " and "non-static variable this cannot be referenced from a static context - ((Visitable)o).accept(this);" Hmm indeed...

Is there some idea that we're missing here? It seems that we can't get our visitor to find the accept method.
Dan Fiedler Richard Geary

Note: I ended up resolving the above question and a number of others after talking with a number of groups. If you are still finding this laboratory assignment to be difficult, I will be able to accept visitors tommorow after class. Alternatively, you can ask me questions at the ACM Picnic this evening! Greg

I coded up a little example so that people can see the big picture of the Visitor design pattern without getting caught up in the details association with evaluating postfix expressions. In this simple example, all my code does is "visit" an
Object
and print out the name of the class that it visited. I hope that this clears things up for people that are still a little confused. Greg

public class IntegerWrapper implements Visitable
{
public void accept(Visitor v)
{
v.visit(this);
}
}


public interface Visitable
{
public void accept(Visitor v);
}


public interface Visitor
{
public void visit(IntegerWrapper o);
}


public class ValueVisitor implements Visitor
{
public void visit(IntegerWrapper o)
{
System.out.println("Found an integer!");
}
}


import java.util.;

public class ValueVisitorDriver
{
public static void main(String[] args)
{
ArrayList a = new ArrayList();

IntegerWrapper iw = new IntegerWrapper();
a.add(iw);

ValueVisitor v = new ValueVisitor();

Iterator i = a.iterator();
while( i.hasNext() )
{
Object o = i.next();
((Visitable)o).accept(v);
}
}
}


ArrayList

What should happen if a null is pushed on the stack and is multiplied with an int? An error? or should the null be taken in as 0?

When would a null be pushed onto the stack? I cannot conceive of a situation in which this would occur. Which visit method would modify the internal stack by placing a null onto the stack? Please provide more details if you have not resolved this issue. Greg

Also we tested divide by zero. In our DivideOp class we try and catch the error it gives, but when we test a number being divided by zero in JUnit it returns a value of infinity. How do we go about testing this?
John Pacino Matt Rummel

If you cannot test for this, that is acceptable. As long as you feel confident that your system is working correctly, I would not worry about this error condition. Greg

ArrayList

We are having some problems with testing our operation methods in our test cases. The problem is we give the test case an int but the testcase wants a VisitiableInt. Anyone have any idea how we can fix this? How can we make the int a VisitableInt?

Noosh Moussavi, Brenda Gruber, Chris Howell

Remember, whenever you are doing development, it is important to remeber that you can only place
Integer
s inside of an
VisitableInteger
. This means that if you want to represent the 'int' 3, you must place it inside of an . Essentially, this means that you need to have an int (or, potentially, an ) field inside of your class! Greg



Link to this Page