0

我是处理 Java 的新手,我正在学习的课程是向我展示一些代码,但是当我尝试运行它时。由于从未设置父级,它返回一个空指针异常。

那么在抽象类中我如何传递父类?这是基于人工智能搜索!谢谢!!

继承人的代码:

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

抽象状态:

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();

}
4

1 回答 1

2

貌似是大写/小写问题,需要将AbstractState的构造函数改成

public AbstractState(State parent)

代替

public AbstractState(State Parent)

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

于 2011-11-07T03:15:26.020 回答