gpt4 book ai didi

java - 作业帮助、抽象/接口(interface)类

转载 作者:行者123 更新时间:2023-12-02 08:06:47 26 4
gpt4 key购买 nike

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

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

代码如下:

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

抽象状态:

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

农夫狼山羊卷心菜:

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

}

最佳答案

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

public AbstractState(State parent)

而不是

public AbstractState(State Parent)

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

关于java - 作业帮助、抽象/接口(interface)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8032333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com