Answered step by step
Verified Expert Solution
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 NN junctions.
public class Maze
private final int N;
private Graph M; Maze
public int startnode;
public Mazeint N int startnode
if N throw new IllegalArgumentExceptionNumber of vertices in a row must be nonnegative";
this.N N;
this.M new GraphNN;
this.startnode startnode;
buildMaze;
public Maze In in
this.M new Graphin;
this.Nint Math.sqrtMV;
this.startnode;
private void validateVertexint v
#implemented
int V MV;
if v v V
throw new IllegalArgumentExceptionvertex v is not between and V ;
Adds the undirected edge vw to the graph M
@param v one vertex in the edge
@param w the other vertex in the edge
@throws IllegalArgumentException unless both @code v V and @code w V
public void addEdgeint v int w
#implemented
validateVertexv;
validateVertexw;
if hasEdgev w && v w
MaddEdgev 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 v MV w w MV
return false;
if v w
return true;
return Madjvcontainsw;
Builds a grid as a graph.
@return Graph G Basic grid on which the Maze is built
public Graph mazegrid
#implemented
Graph g new GraphN N;
for int y ; y N; y
for int x ; x N; x
int current x y N;
if x N
int right current ;
gaddEdgecurrent right;
if y N
int down current N;
gaddEdgecurrent 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 RandomDepthFirstPathsgrid startnode;
rdfsrandomDFSgrid;
Set addedEdges new HashSet;
for int v ; v grid.V; v
for int w : grid.adjv
if rdfshasPathTow && v w
int edgeHash Math.absMathminv w Math.maxv w grid.V Math.maxv w;
if addedEdges.containsedgeHash
addEdgev w;
addedEdges.addedgeHash;
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 findWayint v int w
#implemented
RandomDepthFirstPaths rdfs new RandomDepthFirstPathsM v;
rdfsrandomDFSM;
return rdfspathTow;
@return Graph M
public Graph M
return M;
public static void mainString 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 ; i ; i since the maze is random better check it more than once
for int size ; size ; size
int startnode ThreadLocalRandom.currentnextInt size size ;
MazeExt m new MazeExtsize startnode;
int edgesInMaze mgetGraphPublicE;
assertEqualssize size edgesInMaze;
the error message:
org.opentestjAssertionFailedError:
Expected :
Actual :
Im 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started