2011-11-07 72 views
0

我是新來處理Java和我正在採取的課程是向我展示一些代碼,但是當我試圖運行它。由於從未設置父項,它返回空指針異常。家庭作業幫助,摘要/接口類

所以在一個抽象類中,我如何通過父項?這是基於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(); 

} 
+0

對我很好。 發佈異常錯誤消息和堆棧跟蹤。 –

+0

@DarylTeo堆棧跟蹤發佈!非常感謝你 –

回答

2

看起來像一個大/小寫問題,你需要AbstractState的構造改變

public AbstractState(State parent) 

,而不是

public AbstractState(State Parent) 

上線this.distance = parent.getDistance() +1;,你的構造是使用名稱爲parent的未初始化實例變量,而不是名稱Parent的輸入參數。

+0

哦哇。謝謝Kaleb!我寫了一段時間,但我只是注意到了這一點。哈哈!謝謝,似乎是一個愚蠢的錯誤! –

+0

另一個問題,應該如何使用求解器?你可以給我一個例子嗎? –