Java: Complete the methods marked TODO. Will rate your answer! -------------------------------------------------------------------------------------------------- package search; import java.util.ArrayList; import java.util.List; /** * An abstraction over...

80.2K

Verified Solution

Question

Programming

Java: Complete the methods marked TODO. Will rate youranswer!

--------------------------------------------------------------------------------------------------

package search;

import java.util.ArrayList;
import java.util.List;

/**
* An abstraction over the idea of a search.
*
* @author liberato
*
* @param : generic type.
*/
public abstract class Searcher {
protected final SearchProblem searchProblem;
protected final List visited;
protected List solution;

/**
   * Instantiates a searcher.
   *
   * @param searchProblem
  *           the search problem for which this searcher will find and
  *           validate solutions
   */
public Searcher(SearchProblem searchProblem) {
    this.searchProblem = searchProblem;
    this.visited = new ArrayList();
}

/**
   * Finds and return a solution to the problem,consisting of a list of
   * states.
   * The list should start with the initial state of theunderlying problem.
   * Then, it should have one or more additional states.Each state should be
   * a successor of its predecessor. The last stateshould be a goal state of
   * the underlying problem.
   * If there is no solution, then this method shouldreturn an empty list.
   *
   * @return a solution to the problem (or an emptylist)
   */
public abstract List findSolution();

/**
   * Checks that a solution is valid.
   * A valid solution consists of a list of states. Thelist should start with
   * the initial state of the underlying problem. Then,it should have one or
   * more additional states. Each state should be asuccessor of its
   * predecessor. The last state should be a goal stateof the underlying
   * problem.
   *
   * @param solution : solution
   * @return true iff this solution is a validsolution
   * @throws NullPointerException
  *            if solution is null
   */
public final boolean isValidSolution(List solution){
        // TODO
        return false;
}
}


--------------------------------------------------------------------------------------------------------

package search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;

/**
* An implementation of a Searcher that performs an iterativesearch,
* storing the list of next states in a Stack. This results ina
* depth-first search.
*
*/
public class StackBasedDepthFirstSearcher extendsSearcher {

/**
   * StackBasedDepthFirstSearcher.
   * @param searchProblem : search problem
   */
public StackBasedDepthFirstSearcher(SearchProblemsearchProblem) {
    super(searchProblem);
}

@Override
public List findSolution() {
    // TODO
    return null;
}
}

----------------------------------------------------------------------------------------------------

package search;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
* An implementation of a Searcher that performs an iterativesearch,
* storing the list of next states in a Queue. This results ina
* breadth-first search.
*
*/
public class QueueBasedBreadthFirstSearcher extendsSearcher {

/**
   * QueueBasedBreadthFirstSearcher.
   * @param searchProblem : search problem
   */
public QueueBasedBreadthFirstSearcher(SearchProblemsearchProblem) {
    super(searchProblem);
}

@Override
public List findSolution() {
    // TODO
    return null;
}
}


---------------------------------------------------------------------------------

package mazes;

import java.util.List;

import search.Solver;

public class MazeDriver {

/**
   * Supproting main method.
   */
public static void main(String[] args) {
    MazeGenerator mg = new MazeGenerator(24, 8,0);
    Maze m = mg.generateDFS();
    System.out.println(m.toString());
    Solver s = newSolver(m);
    List r =s.solveWithRecursiveDFS();
    for (Cell cell : r) {
      System.out.println(cell);
    }
    System.out.println(r.size());
    // System.out.println(\"--------\");
    // List d =s.solveWithIterativeDFS();
    // for (Cell cell : d) {
    //   System.out.println(cell);
    // }
    // System.out.println(d.size());
    // System.out.println(\"--------\");
    // List q = s.solveWithBFS();
    // for (Cell cell : q) {
    //   System.out.println(cell);
    // }
    // System.out.println(q.size());
}
}


------------------------------------------------------------------------------------------------------------------

package puzzle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import search.SearchProblem;
import search.Solver;

/**
* A class to represent an instance of the eight-puzzle.
* The spaces in an 8-puzzle are indexed as follows:
* 0 | 1 | 2
* --+---+---
* 3 | 4 | 5
* --+---+---
* 6 | 7 | 8
* The puzzle contains the eight numbers 1-8, and an emptyspace.
* If we represent the empty space as 0, then the puzzle issolved
* when the values in the puzzle are as follows:
* 1 | 2 | 3
* --+---+---
* 4 | 5 | 6
* --+---+---
* 7 | 8 | 0
* That is, when the space at index 0 contains value 1, thespace
* at index 1 contains value 2, and so on.
* From any given state, you can swap the empty space with aspace
* adjacent to it (that is, above, below, left, or right ofit,
* without wrapping around).
* For example, if the empty space is at index 2, you may swap
* it with the value at index 1 or 5, but not any other index.
* Only half of all possible puzzle states are solvable! See:
* https://en.wikipedia.org/wiki/15_puzzle
* for details.
*

* @author liberato
*
*/
public class EightPuzzle implementsSearchProblem> {

/**
   * Creates a new instance of the 8 puzzle with thegiven starting values.
   * The values are indexed as described above, andshould contain exactly the
   * nine integers from 0 to 8.
   *
   * @param startingValues
  *           the starting values, 0 -- 8
   * @throws IllegalArgumentException
  *            if startingValues is invalid
   */
public EightPuzzle(List startingValues) {
}

@Override
public List getInitialState() {
    // TODO
    return null;
}

@Override
public List>getSuccessors(List currentState) {
    // TODO
    return null;
}


@Override
public boolean isGoal(List state) {
    // TODO
    return false;
}

/**
   * supporting man method.
   */
public static void main(String[] args) {
    EightPuzzle e = newEightPuzzle(Arrays.asList(new Integer[] { 1, 2, 3,
        4, 0, 6, 7, 5, 8}));

    List> r = newSolver>(e).solveWithBFS();
    for (List l : r) {
      System.out.println(l);
    }
}
}

Answer & Explanation Solved by verified expert
4.4 Ratings (926 Votes)
Code Implemented in JavaNote Comments are written for codeexplanationCodeFilename Searcherjavapackage searchimport javautilArrayListimport javautilList An abstraction over the idea of a search author liberato param public abstract class Searcher protected final SearchProblemsearchProblem protected final List visited protected List solution Instantiates a searcher param searchProblem the search problem for which this searcher will findand validate solutions public SearcherSearchProblem searchProblem thissearchProblem searchProblem thisvisited newArrayList Finds and return a solution to the problemconsisting of a list of states The list should start with the initial state of theunderlying problem Then it should have one or more additional statesEach state should be a successor of its predecessor The last stateshould be a goal state of the underlying problem If there is no solution then this method shouldreturn an empty list return a solution to the problem or an emptylist public abstract List findSolution Checks that a solution is valid A valid solution consists of a list of states Thelist should start with the initial state of the underlying problem    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students