Java Reference
In-Depth Information
public IfCommand(
Term term, Command body, Command elseBody)
{
this.term = term;
this.body = body;
this.elseBody = elseBody;
}
public void execute(Context c)
{
if (term.eval(c) != null)
{
body.execute(c);
}
else
{
elseBody.execute(c);
}
}
}
CHALLENGE 25.1
Write a program that creates and uses an interpreter to shut down all the machines
that MachineLine controls, except for the unload buffer.
Now suppose that you want to unload all the material from a machine. The HasMaterial
class takes another term—usually a machine—as a constructor argument. A HasMaterial
term returns null if its term has no material and otherwise returns the term's value:
package com.oozinoz.robot.interpreter;
import com.oozinoz.machine.*;
public class HasMaterial extends Term
{
protected Term term;
public HasMaterial(Term term)
{
this.term = term;
}
public Machine eval(Context c)
{
Machine m = term.eval(c);
return m.hasMaterial() ? m : null;
}
}
To unload a machine and to keep unloading it until it's empty, you need a WhileCommand
class. Suppose that you call up the machine line company and ask for this class. While you
wait for a patch or the next release, you can also temporarily create the class yourself. The
code for WhileCommand should be similar to the code in IfCommand.java .
Search WWH ::




Custom Search