Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

help me fix my logic mistake in JAVA In the following JAVA class I had to implement a few methods import java.util. * ; /

help me fix my logic mistake in JAVA
In the following JAVA class I had to implement a few methods
import java.util.*;
/**
* Class that represents a maze with N*N junctions.
*
*/
public class Maze{
private final int N;
private Graph M; //Maze
public int startnode;
public Maze(int N, int startnode){
if (N <0) throw new IllegalArgumentException("Number of vertices in a row must be nonnegative");
this.N = N;
this.M= new Graph(N*N);
this.startnode= startnode;
buildMaze();
}
public Maze (In in){
this.M = new Graph(in);
this.N=(int) Math.sqrt(M.V());
this.startnode=0;
}
private void validateVertex(int v){
#implemented
int V = M.V();
if (v <0|| v >= V)
throw new IllegalArgumentException("vertex "+ v +" is not between 0 and "+(V -1));
}
/**
* Adds the undirected edge v-w to the graph M.
*
* @param v one vertex in the edge
* @param w the other vertex in the edge
* @throws IllegalArgumentException unless both {@code 0<= v < V} and {@code 0<= w < V}
*/
public void addEdge(int v, int w){
#implemented
validateVertex(v);
validateVertex(w);
if (!hasEdge(v, w) && v != w){
M.addEdge(v, w);
}
}
/**
* Returns true if there is an edge between 'v' and 'w'
* @param v one vertex
* @param w another vertex
* @return true or false
*/
public boolean hasEdge( int v, int w){
#implemented
if (v <0|| v >= M.V()|| w <0|| w >= M.V()){
return false;
}
if (v == w){
return true;
}
return M.adj(v).contains(w);
}
/**
* Builds a grid as a graph.
* @return Graph G -- Basic grid on which the Maze is built
*/
public Graph mazegrid(){
#implemented
Graph g = new Graph(N * N);
for (int y =0; y < N; y++){
for (int x =0; x < N; x++){
int current = x + y * N;
if (x < N -1){
int right = current +1;
g.addEdge(current, right);
}
if (y < N -1){
int down = current + N;
g.addEdge(current, down);
}
}
}
return g;
}
/**
* Builds a random maze as a graph.
* The maze is build with a randomized DFS as the Graph M.
*/
private void buildMaze(){
#implemented
Graph grid = mazegrid();
RandomDepthFirstPaths rdfs = new RandomDepthFirstPaths(grid, startnode);
rdfs.randomDFS(grid);
Set addedEdges = new HashSet<>();
for (int v =0; v < grid.V(); v++){
for (int w : grid.adj(v)){
if (rdfs.hasPathTo(w) && v!= w){
int edgeHash = Math.abs(Math.min(v, w)- Math.max(v, w))* grid.V()+ Math.max(v, w);
if (!addedEdges.contains(edgeHash)){
addEdge(v, w);
addedEdges.add(edgeHash);
}
}
}
}
}
/**
* Find a path from node v to w
* @param v start node
* @param w end node
* @return List -- a list of nodes on the path from v to w (both included) in the right order.
*/
public List findWay(int v, int w){
#implemented
RandomDepthFirstPaths rdfs = new RandomDepthFirstPaths(M, v);
rdfs.randomDFS(M);
return rdfs.pathTo(w);
}
/**
* @return Graph M
*/
public Graph M(){
return M;
}
public static void main(String[] args){
// FOR TESTING
}
}
I got a few jUnit tests given and I have an error with only one
@Test
void testBuildMazeRightEdgeCount() throws InvocationTargetException, NoSuchMethodException, IllegalAccessException, InterruptedException {
for (int i =0; i <10; i++){// since the maze is random better check it more than once
for (int size =2; size <10; size++){
int startnode = ThreadLocalRandom.current().nextInt(0, size * size -1);
MazeExt m = new MazeExt(size, startnode);
int edgesInMaze = m.getGraphPublic().E();
assertEquals(size * size -1, edgesInMaze);
}
}
}
the error message:
org.opentest4j.AssertionFailedError:
Expected :3
Actual :4
I'm certain the error is a logic mistake in the private void buildMaze() method, though I can't figure it out

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

Would you rather audit corporations or federal agencies?

Answered: 1 week ago