家庭作业帮助,抽象/接口类

时间:2011-11-07 03:03:17

标签: java interface abstract river-crossing-puzzle

我是处理Java的新手,我正在考虑的课程是向我展示一些代码,但是当我尝试运行它时。它返回一个空指针异常,因为父节点从未设置过。

所以在抽象类中我如何传递Parent?这是基于AI搜索!谢谢!

下面是代码:

这是基于农民狼山羊白菜的问题。

AbstractState:

package hw1;

public abstract class AbstractState implements State
{
    public State parent = null;
    private double distance = 0;

    public AbstractState(){}
    public AbstractState(State Parent)
    {
        this.parent = parent;
        this.distance = parent.getDistance() +1;
    }

    public State getParent()
    {
        return parent;
    }

    public double getDistance()
    {
        return distance;
    }

}

国家

package hw1;
import java.util.Set;


public interface State 
{
    public Iterable<State> getPossibleMoves();
    public boolean isSolution();
    public double getHeuristic();
    public double getDistance();
    public State getParent();
}

FarmerWolfGoatCabbage:

package hw1;
import java.util.HashSet;
import java.util.Set;


public class FarmerWolfGoatState extends AbstractState 
{
    enum Side
    {
        EAST 
        {
            public Side getOpposite()
            {
                return WEST;
            }
        },
        WEST
        {
            public Side getOpposite()
            {
                return EAST;
            }
        };

        abstract public Side getOpposite();
    }

    private Side farmer = Side.EAST;
    private Side wolf = Side.EAST;
    private Side goat = Side.EAST;
    private Side cabbage = Side.EAST;

    public FarmerWolfGoatState()
    {}

    public FarmerWolfGoatState(FarmerWolfGoatState parent, Side farmer, Side wolf, Side goat, Side Cabbage)
    {
        super(parent);
        this.farmer = farmer;
        this.wolf = wolf;
        this.goat = goat;
        this.cabbage = cabbage;
    }

    @Override
    public Iterable<State> getPossibleMoves() {
        Set<State> moves = new HashSet<State>();

        if(farmer == wolf)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf.getOpposite()
                    ,goat,cabbage).addIfSafe(moves);

        if(farmer == goat)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf
                    ,goat.getOpposite(),cabbage).addIfSafe(moves);

        if(farmer == cabbage)
            new FarmerWolfGoatState(this,
                    farmer.getOpposite()
                    ,wolf
                    ,goat,cabbage.getOpposite()).addIfSafe(moves);

        new FarmerWolfGoatState(this, farmer.getOpposite(), wolf, goat, cabbage).addIfSafe(moves);

        return moves;

    }

    @Override
    public boolean isSolution() {
        //returns true if all of the them are on the west side and not the east.
        return farmer == Side.WEST && wolf == Side.WEST && goat==Side.WEST && cabbage == Side.WEST;

    }

    @Override
    public double getHeuristic() {
        // TODO Auto-generated method stub
        return 0;
    }

    public final void addIfSafe(Set<State> moves)
    {
        boolean unsafe = (farmer!= wolf && farmer != goat) || (farmer != goat && farmer != cabbage);

        if(!unsafe)
            moves.add(this);
    }

    public boolean equals(Object o)
    {
        if(o == null || !(o instanceof FarmerWolfGoatState))
            return false;
        FarmerWolfGoatState fwgs = (FarmerWolfGoatState)o;

        return farmer == fwgs.farmer &&
                wolf == fwgs.wolf &&
                cabbage == fwgs.cabbage &&
                goat == fwgs.goat;
    }

    public int hashCode()
    {
        return(farmer == Side.EAST ? 1 : 0) +
                (wolf == Side.EAST ? 2 : 0) +
                (cabbage == Side.EAST ? 4: 0)+
                (goat == Side.EAST ? 8 : 0);
    }

}

主要试图解决..

package hw1;

import hw1.FarmerWolfGoatState.Side;



public class Homework1 {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        FarmerWolfGoatState parentState = new FarmerWolfGoatState();
        FarmerWolfGoatState nextState = new FarmerWolfGoatState(parentState,Side.EAST,Side.EAST,Side.EAST,Side.WEST);

        while(!nextState.isSolution())
        {
            nextState.getPossibleMoves();
        }

    }


}

堆栈追踪:

Exception in thread "main" java.lang.NullPointerException
    at hw1.AbstractState.<init>(AbstractState.java:12)
    at hw1.FarmerWolfGoatState.<init>(FarmerWolfGoatState.java:38)
    at hw1.Homework1.main(Homework1.java:15)

我还有一个求解器,我应该使用它吗?

here it is.
package hw1;
import java.util.Stack;


public class DepthFirstSolver extends AbstractSolver
{
    private Stack<State> stack = new Stack<State>();

    @Override
    protected void addState(State s)
    {
        if(!stack.contains(s))
        {
            stack.push(s);
        }
    }

    @Override
    protected boolean hasElements()
    {
        return !stack.empty();
    }

    @Override
    protected State nextState()
    {
        return stack.pop();
    }
    @Override
    protected void clearOpen()
    {
        stack.clear();
    }

}


package hw1;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;




public abstract class AbstractSolver implements Solver 
{
    private Set<State> closed= new HashSet<State>();

    public List<State> solve(State initialState)
    {
        closed.clear();
        clearOpen();
        while(hasElements())
        {
            State s = nextState();
            if(s.isSolution())
                return findPath(s);
            closed.add(s);
            Iterable<State> moves = s.getPossibleMoves();
            for(State move : moves)
                if(!closed.contains(move))
                        addState(move);
        }
        return null;

    }
    public int getVisitedStateCount()
    {
        return closed.size();
    }

    private List<State> findPath(State solution)
    {
        LinkedList<State> path = new LinkedList<State>();
        while(solution!=null)
        {
            path.addFirst(solution);
            solution = solution.getParent();

        }
        return path;
    }

    protected abstract boolean hasElements();
    protected abstract State nextState();
    protected abstract void addState(State s);
    protected abstract void clearOpen();

}

1 个答案:

答案 0 :(得分:2)

看起来像一个大写/小写的问题,你需要将AbstractState的构造函数更改为

public AbstractState(State parent)

而不是

public AbstractState(State Parent)

this.distance = parent.getDistance() +1;行上,您的构造函数使用名为parent的未初始化实例变量,而不是名称Parent的输入参数。