java question: \"Fill the add statement area\"
Project is to work with stacks.
package p2;
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y ) {
this.x = x;
this.y = y;
}
public String toString() {
return \"(\" + this.x + \",\" + this.y + \")\";
}
@Override
public boolean equals( Object object ) {
if( object == null ) {
return false;
}
if( ! Coordinate.class.isAssignableFrom( object.getClass() )) {
return false;
}
final Coordinate other = (Coordinate) object;
return this.x == other.x && this.y == other.y;
}
}
package p2;
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y ) {
this.x = x;
this.y = y;
}
public String toString() {
return \"(\" + this.x + \",\" + this.y + \")\";
}
@Override
public boolean equals( Object object ) {
if( object == null ) {
return false;
}
if( ! Coordinate.class.isAssignableFrom( object.getClass() )) {
return false;
}
final Coordinate other = (Coordinate) object;
return this.x == other.x && this.y == other.y;
}
}
package p2;
import java.util.Vector;
public class Maze {
private char[][] maze;
private int height;
private int width;
/**
* Create a new Maze of the specified height and width, initializing every
* location as empty, with a \' \'.
**/
public Maze( int width, int height ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as blocked,
* marking it with a \'X\'
**/
public void setBlocked( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as having been visited,
* marking it with a \'*\'
**/
public void setVisited( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as part of the path solution,
* marking it with a \'.\'
**/
public void setPath( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns the character at the locatio specified by the Coordinate
**/
public char at( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns a Coordinate array containing all Coordinates that are clear around
* the specified coordinate.
**/
public Coordinate[] clearAround( Coordinate coord ) {
Vector vector = new Vector();
// ADD STATEMENTS HERE
// Look at each of the locations around the specified Coordinate, and add it
// to the vector if it is clear (i.e. a space)
return vector.toArray( new Coordinate[0] );
}
/**
* Returns a Coordinate that provides the entrance location in this maze.
**/
public Coordinate start() {
return new Coordinate( 0, 1 );
}
/**
* Returns a Coordinate that provides the exit location from this maze.
**/
public Coordinate end() {
// ADD STATEMENTS HERE
}
/**
* The toString() method is responsible for creating a String representation
* of the Maze. See the project specification for sample output. Note that
* the String representation adds numbers across the top and side of the Maze
* to show the Coordinates of each cell in the maze.
**/
public String toString() {
StringBuilder buffer =.
java question Fill the add statement areaProject is to wo.pdf
1. java question: "Fill the add statement area"
Project is to work with stacks.
package p2;
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y ) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
@Override
public boolean equals( Object object ) {
if( object == null ) {
return false;
}
if( ! Coordinate.class.isAssignableFrom( object.getClass() )) {
return false;
2. }
final Coordinate other = (Coordinate) object;
return this.x == other.x && this.y == other.y;
}
}
package p2;
public class Coordinate {
public int x;
public int y;
public Coordinate( int x, int y ) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
@Override
public boolean equals( Object object ) {
if( object == null ) {
return false;
}
if( ! Coordinate.class.isAssignableFrom( object.getClass() )) {
3. return false;
}
final Coordinate other = (Coordinate) object;
return this.x == other.x && this.y == other.y;
}
}
package p2;
import java.util.Vector;
public class Maze {
private char[][] maze;
private int height;
private int width;
/**
* Create a new Maze of the specified height and width, initializing every
* location as empty, with a ' '.
**/
public Maze( int width, int height ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as blocked,
4. * marking it with a 'X'
**/
public void setBlocked( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as having been visited,
* marking it with a '*'
**/
public void setVisited( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as part of the path solution,
* marking it with a '.'
**/
public void setPath( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns the character at the locatio specified by the Coordinate
**/
public char at( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns a Coordinate array containing all Coordinates that are clear around
* the specified coordinate.
5. **/
public Coordinate[] clearAround( Coordinate coord ) {
Vector vector = new Vector();
// ADD STATEMENTS HERE
// Look at each of the locations around the specified Coordinate, and add it
// to the vector if it is clear (i.e. a space)
return vector.toArray( new Coordinate[0] );
}
/**
* Returns a Coordinate that provides the entrance location in this maze.
**/
public Coordinate start() {
return new Coordinate( 0, 1 );
}
/**
* Returns a Coordinate that provides the exit location from this maze.
**/
public Coordinate end() {
// ADD STATEMENTS HERE
}
/**
* The toString() method is responsible for creating a String representation
* of the Maze. See the project specification for sample output. Note that
* the String representation adds numbers across the top and side of the Maze
* to show the Coordinates of each cell in the maze.
**/
6. public String toString() {
StringBuilder buffer = new StringBuilder();
// ADD STATEMENTS HERE
// First, print out the column headings
// Next, print out each row in the maze - note the spaces between
// cells to facilitate reading. Each row should include its row number.
return buffer.toString();
}
}
package p2;
import java.util.Vector;
public class Maze {
private char[][] maze;
private int height;
private int width;
/**
* Create a new Maze of the specified height and width, initializing every
* location as empty, with a ' '.
**/
public Maze( int width, int height ) {
// ADD STATEMENTS HERE
}
7. /**
* Mutator to allow us to set the specified Coordinate as blocked,
* marking it with a 'X'
**/
public void setBlocked( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as having been visited,
* marking it with a '*'
**/
public void setVisited( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Mutator to allow us to set the specified Coordinate as part of the path solution,
* marking it with a '.'
**/
public void setPath( Coordinate coord ) {
// ADD STATEMENTS HERE
}
/**
* Returns the character at the locatio specified by the Coordinate
**/
public char at( Coordinate coord ) {
// ADD STATEMENTS HERE
}
8. /**
* Returns a Coordinate array containing all Coordinates that are clear around
* the specified coordinate.
**/
public Coordinate[] clearAround( Coordinate coord ) {
Vector vector = new Vector();
// ADD STATEMENTS HERE
// Look at each of the locations around the specified Coordinate, and add it
// to the vector if it is clear (i.e. a space)
return vector.toArray( new Coordinate[0] );
}
/**
* Returns a Coordinate that provides the entrance location in this maze.
**/
public Coordinate start() {
return new Coordinate( 0, 1 );
}
/**
* Returns a Coordinate that provides the exit location from this maze.
**/
public Coordinate end() {
// ADD STATEMENTS HERE
}
/**
* The toString() method is responsible for creating a String representation
9. * of the Maze. See the project specification for sample output. Note that
* the String representation adds numbers across the top and side of the Maze
* to show the Coordinates of each cell in the maze.
**/
public String toString() {
StringBuilder buffer = new StringBuilder();
// ADD STATEMENTS HERE
// First, print out the column headings
// Next, print out each row in the maze - note the spaces between
// cells to facilitate reading. Each row should include its row number.
return buffer.toString();
}
}
package p2;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class MazeReader {
private String fileName;
private Maze maze;
public MazeReader( String fileName ) {
this.fileName = fileName;
this.maze = null;
10. }
public Maze open() throws FileNotFoundException {
Scanner scanner = new Scanner( new File( this.fileName ));
int width = scanner.nextInt();
int height = scanner.nextInt();
this.maze = new Maze( width, height );
// Remove new line after int
scanner.nextLine();
// ADD STATEMENTS HERE
// You will need to read in each line using the Scanner, and provide
// the row number and the line to the addLine method to add it to the Maze
return this.maze;
}
private void addLine( int row, String line ) {
// ADD STATEMENTS HERE
}
public static void main( String[] args ) throws FileNotFoundException {
MazeReader reader = new MazeReader( "sampleMaze.txt" );
Maze maze = reader.open();
11. System.out.println( maze );
System.out.println( maze.at( new Coordinate( 0, 0 )));
System.out.println( maze.at( new Coordinate( 0, 1 )));
}
}
package p2;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class MazeReader {
private String fileName;
private Maze maze;
public MazeReader( String fileName ) {
this.fileName = fileName;
this.maze = null;
}
public Maze open() throws FileNotFoundException {
Scanner scanner = new Scanner( new File( this.fileName ));
int width = scanner.nextInt();
int height = scanner.nextInt();
this.maze = new Maze( width, height );
12. // Remove new line after int
scanner.nextLine();
// ADD STATEMENTS HERE
// You will need to read in each line using the Scanner, and provide
// the row number and the line to the addLine method to add it to the Maze
return this.maze;
}
private void addLine( int row, String line ) {
// ADD STATEMENTS HERE
}
public static void main( String[] args ) throws FileNotFoundException {
MazeReader reader = new MazeReader( "sampleMaze.txt" );
Maze maze = reader.open();
System.out.println( maze );
System.out.println( maze.at( new Coordinate( 0, 0 )));
System.out.println( maze.at( new Coordinate( 0, 1 )));
}
}
package p2;
13. import java.io.FileNotFoundException;
public class MazeSolver {
private Maze maze;
private LinkedStack path;
public MazeSolver( Maze maze ) {
// ADD STATEMENTS HERE
}
public void solve() {
// ADD STATEMENTS HERE
// Add the starting Coordinate to the maze, and while the Stack has
// entries, and the top of the Stack is not the end, continue searching
// for the path
}
public static void main( String[] args ) throws FileNotFoundException {
MazeReader reader = new MazeReader( "sampleMaze.txt" );
Maze maze = reader.open();
MazeSolver solver = new MazeSolver( maze );
System.out.println( "Before solving" );
System.out.println( maze );
System.out.println( "Start is " + maze.start() );
System.out.println( "End is " + maze.end() );
solver.solve();
System.out.println( "After solving (. shows solution, o shows visited)" );
System.out.println( maze );
14. }
}
package p2;
import java.io.FileNotFoundException;
public class MazeSolver {
private Maze maze;
private LinkedStack path;
public MazeSolver( Maze maze ) {
// ADD STATEMENTS HERE
}
public void solve() {
// ADD STATEMENTS HERE
// Add the starting Coordinate to the maze, and while the Stack has
// entries, and the top of the Stack is not the end, continue searching
// for the path
}
public static void main( String[] args ) throws FileNotFoundException {
MazeReader reader = new MazeReader( "sampleMaze.txt" );
Maze maze = reader.open();
MazeSolver solver = new MazeSolver( maze );
System.out.println( "Before solving" );
System.out.println( maze );
System.out.println( "Start is " + maze.start() );
System.out.println( "End is " + maze.end() );
15. solver.solve();
System.out.println( "After solving (. shows solution, o shows visited)" );
System.out.println( maze );
}
}
Consider a maze made up of a rectangular array of squares. The maze will contain a character
(either +, -, or |) to represent a blocked square, and to form the walls of the maze. Mazes will
have only one entrance at the Coordinate (0, 1), with only one exit in the lower right hand corner
of the maze.
Beginning at the entrance to the maze, find a path to the exit at the bottom right of the maze. You
may only move up, down, left, and right. Each square in the maze can be in one of four states:
clear (space), blocked (X), path (.), or visited (*). Initially, after the maze has been read in from
the file, each square will be either clear or blocked. If a square lies on a successful path, mark it
with a period. If you visit a square but it does not lead to a successful path, mark it as visited
with asterisk.
Consider a maze made up of a rectangular array of squares. The maze will contain a character
(either +, -, or |) to represent a blocked square, and to form the walls of the maze. Mazes will
have only one entrance at the Coordinate (0, 1), with only one exit in the lower right hand corner
of the maze.
Beginning at the entrance to the maze, find a path to the exit at the bottom right of the maze. You
may only move up, down, left, and right. Each square in the maze can be in one of four states:
clear (space), blocked (X), path (.), or visited (*). Initially, after the maze has been read in from
the file, each square will be either clear or blocked. If a square lies on a successful path, mark it
with a period. If you visit a square but it does not lead to a successful path, mark it as visited
with asterisk.
Solution
Got with two methods which are mainly required to run the main program:-
public Maze(int width,int height{
if(Maze.maze == null) {
16. throw new IllegalStateException("Error in setting the size");
}
if(width < 0 || width >= maze.length) {
throw new IllegalArgumentException("Row is Invalid.");
}
if(height < 0 || height >= maze[0].length) {
throw new IllegalArgumentException("Column in Invalid");
}
this.width = width;
this.height = height;
widthMinus1 = (width - 1);
heightMinus1 = (height - 1);
}
We are using the boolean array visited[][] which will set the value to true or false , if the path has
been
traversed already.
private boolean[][] visited = true;
public void solve(char[][]maze)
{
int x = maze[0].length;
int y = maze.length-2;
int d = -1;
boolean ok = false;
recursivesolution(maze,x,y,d);
}
// Defined for solving it recursively
public void recursivesolution(char[][]maze,int x, int y, int d)
{
boolean bool = false;
for (int j = 0;j<4 &&!bool;j++)
if (j!= d)
switch (j)
{
// Check for up right down and left
17. case 0:
if (maze[y-1][x] == ' ')
bool = recursivesolution(maze, x, y - 2, 2);
break;
case 1:
if (maze[y][x+1] == ' ')
bool = recursivesolution(maze, x + 2, y, 3);
break;
case 2:
if (maze[y+1][x] == ' ')
bool = recursivesolution(maze, x, y + 2, 0);
break;
case 3:
if (maze[y][x-1] == ' ')
bool = recursivesolution(maze, x - 2, y, 1);
break;
}
if (x == 1 && y == 1)
bool = true;
}