SlideShare a Scribd company logo
Java Question : help needed In the program "Fill the Add statements Area" here area. Sample
Output in the picture
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 an asterisk.
This is the program for it
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;
}
}
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 ) {
this.width = width;
this.height = height;
this.maze = new char [this.width][this.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 = 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.
for(int x=0; x
for(int y=0; y
buffer.append(maze[x][y]);
buffer.append(" ");
return buffer.toString();
}
}
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() );
solver.solve();
System.out.println( "After solving (. shows solution, o shows visited)" );
System.out.println( maze );
}
}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 );// Remove new line after intscanner.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 Mazereturn 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 )));}}
Solution
import java.io.*;
class Maze
{
class MazeSquare
{
public boolean hasTopWall;
public boolean hasRightWall;
public boolean hasLeftWall;
public boolean hasBottomWall;
public MazeSquare()
{
hasTopWall = hasRightWall = hasLeftWall = hasBottomWall = false;
}
}
class MazeFormatException extends Exception
{
public final static int noError = 0;
public final static int badRowAndColumnCounts = 1;
public final static int wrongNumberOfEntriesInRow = 2;
public final static int badEntry = 3;
public int lineNumber;
public int problem;
public MazeFormatException( int line, int prob )
{
lineNumber = line;
problem = prob;
}
}
private int nRows;
private int nColumns;
MazeSquare[][] square;
public static void main( String[] args )
{
if( args.length != 1 )
{
System.err.println( "Usage: java Maze mazeFileName" );
System.exit( 1 );
}
Maze knosos = null;
try
{
knosos = new Maze( args[0] );
}
catch( FileNotFoundException e )
{
System.err.println( "Can't open " + args[0] + ". Check your spelling." );
System.exit( 1 );
}
catch( IOException e )
{
System.err.println( "Severe input error. I give up." );
System.exit( 1 );
}
catch( MazeFormatException e )
{
switch( e.problem )
{
case MazeFormatException.badRowAndColumnCounts:
System.err.println( args[0] + ", line " + e.lineNumber + ": row and column counts
expected" );
break;
case MazeFormatException.wrongNumberOfEntriesInRow:
System.err.println( args[0] + ", line " + e.lineNumber + ": wrong number of entries"
);
break;
case MazeFormatException.badEntry:
System.err.println( args[0] + ", line " + e.lineNumber + ": non-hexadecimal digit
detected" );
break;
default:
System.err.println( "This should never get printed." );
break;
}
System.exit( 1 );
}
knosos.print( System.out );
}
public Maze()
{
square = null;
nRows = nColumns = 0;
}
public Maze( String fileName ) throws FileNotFoundException, IOException,
MazeFormatException
{
BufferedReader in = null;
in = new BufferedReader( new FileReader( fileName ) );
load( in );
in.close();
}
public void load( BufferedReader in ) throws MazeFormatException
{
String line;
String[] tokens;
int lineNumber = 0;
try
{
line = in.readLine();
lineNumber++;
tokens = line.split( "s+" );
nRows = Integer.parseInt( tokens[0] );
nColumns = Integer.parseInt( tokens[1] );
if( nRows <= 0 || nColumns <= 0 )
throw new Exception();
}
catch( Exception e )
{
throw new MazeFormatException( lineNumber,
MazeFormatException.badRowAndColumnCounts );
}
// Allocate the 2D array of MazeSquares.
square = new MazeSquare[nRows][nColumns];
for( int i=0; i < nRows; i++ )
for( int j=0; j < nColumns; j++ )
square[i][j] = new MazeSquare();
for( int i=0; i < nRows; i++ )
{
try
{
line = in.readLine();
lineNumber++;
tokens = line.split( "s+" );
if( tokens.length != nColumns )
throw new Exception();
}
catch( Exception e )
{
throw new MazeFormatException( lineNumber,
MazeFormatException.wrongNumberOfEntriesInRow );
}
for( int j=0; j < nColumns; j++ )
{
int squareValue;
try
{
squareValue = Integer.parseInt( tokens[j], 16 );
}
catch( NumberFormatException e )
{
throw new MazeFormatException( lineNumber, MazeFormatException.badEntry );
}
// These are "bitwise AND" operations. We'll discuss them in class.
square[i][j].hasTopWall = ((squareValue & 1) != 0);
square[i][j].hasRightWall = ((squareValue & 2) != 0);
square[i][j].hasBottomWall = ((squareValue & 4) != 0);
square[i][j].hasLeftWall = ((squareValue & 8) != 0);
}
}
}
public void print( PrintStream out )
{
int i, j;
for( i=0; i < nRows; i++ )
{
out.print( '+' );
for( j=0; j < nColumns; j++ )
{
if( i > 0 && square[i][j].hasTopWall != square[i-1][j].hasBottomWall )
out.print( "xxx+" );
else if( square[i][j].hasTopWall )
out.print( "---+" );
else
out.print( " +" );
}
out.println();
if( nColumns > 0 && square[i][0].hasLeftWall )
out.print( '|' );
else
out.print( ' ' );
for( j=0; j < nColumns; j++ )
{
if( j < nColumns - 1 && square[i][j].hasRightWall != square[i][j+1].hasLeftWall )
out.print( " X" );
else if( square[i][j].hasRightWall )
out.print( " |" );
else
out.print( " " );
}
out.println();
}
if( nRows > 0 )
{
out.print( '+' );
for( j=0; j < nColumns; j++ )
{
if( square[nRows-1][j].hasBottomWall )
out.print( "---+" );
else
out.print( " +" );
}
out.println();
}
}
}

More Related Content

Similar to Java Question help needed In the program Fill the Add statements.pdf

Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdfTopic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
ambikacollection1084
 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdfImplement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
SIGMATAX1
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
Jose Perez
 
I need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdfI need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdf
arihantgiftgallery
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
Francis Johny
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
arcotstarsports
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
Tak Lee
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
amazing2001
 
Java Generics
Java GenericsJava Generics
Java Generics
jeslie
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
Rafael Winterhalter
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
ganisyedtrd
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
Intro C# Book
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
aromanets
 
Please copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docxPlease copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docx
cgraciela1
 
I have compilation errors that I'm struggling with in my code- please.pdf
I have compilation errors that I'm struggling with in my code- please.pdfI have compilation errors that I'm struggling with in my code- please.pdf
I have compilation errors that I'm struggling with in my code- please.pdf
ColinjHJParsonsa
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
Christian Baranowski
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
Hang Zhao
 

Similar to Java Question help needed In the program Fill the Add statements.pdf (20)

Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdfTopic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
Topic Use stacks to solve Maze. DO NOT USE RECURSION. And do not us.pdf
 
Implement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdfImplement the ADT stack by using an array stack to contain its entri.pdf
Implement the ADT stack by using an array stack to contain its entri.pdf
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
I need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdfI need help with this maze gui that I wrote in java, I am trying to .pdf
I need help with this maze gui that I wrote in java, I am trying to .pdf
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
-- USING UNITY TRYING TO CREATE A CLICK TO PATH- THAT YOU CLICK ON AND.pdf
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdfCreat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
 
Please copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docxPlease copy and paste the code and explain why it won't work- It is su.docx
Please copy and paste the code and explain why it won't work- It is su.docx
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
I have compilation errors that I'm struggling with in my code- please.pdf
I have compilation errors that I'm struggling with in my code- please.pdfI have compilation errors that I'm struggling with in my code- please.pdf
I have compilation errors that I'm struggling with in my code- please.pdf
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 

More from kamdinrossihoungma74

WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdfWHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
kamdinrossihoungma74
 
What advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdfWhat advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdf
kamdinrossihoungma74
 
What are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdfWhat are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdf
kamdinrossihoungma74
 
Using Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdfUsing Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdf
kamdinrossihoungma74
 
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdfUnlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
kamdinrossihoungma74
 
True or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdfTrue or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdf
kamdinrossihoungma74
 
To understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdfTo understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdf
kamdinrossihoungma74
 
Think about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdfThink about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdf
kamdinrossihoungma74
 
The probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdfThe probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdf
kamdinrossihoungma74
 
Sally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdfSally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdf
kamdinrossihoungma74
 
Short answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdfShort answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdf
kamdinrossihoungma74
 
Rectangle class inherits members of Shape class Has one method calle.pdf
Rectangle class inherits members of Shape class  Has one method calle.pdfRectangle class inherits members of Shape class  Has one method calle.pdf
Rectangle class inherits members of Shape class Has one method calle.pdf
kamdinrossihoungma74
 
Operating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdfOperating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdf
kamdinrossihoungma74
 
Of the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdfOf the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdf
kamdinrossihoungma74
 
Multiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdfMultiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdf
kamdinrossihoungma74
 
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdfPart 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
kamdinrossihoungma74
 
Lets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdfLets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdf
kamdinrossihoungma74
 
Let X be a random variable with cumulative distribution function F(a.pdf
Let X be a random variable with cumulative distribution function  F(a.pdfLet X be a random variable with cumulative distribution function  F(a.pdf
Let X be a random variable with cumulative distribution function F(a.pdf
kamdinrossihoungma74
 
John and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdfJohn and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdf
kamdinrossihoungma74
 
How can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdfHow can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdf
kamdinrossihoungma74
 

More from kamdinrossihoungma74 (20)

WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdfWHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
WHICH OF THE FOLLOWING LAB EXPERIMENTS DEMONSTRATIONS BEST REPRESEN.pdf
 
What advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdfWhat advantage is there in placing the two wires carrying an AC sign.pdf
What advantage is there in placing the two wires carrying an AC sign.pdf
 
What are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdfWhat are key benefits of enzymes What are key benefits of enzym.pdf
What are key benefits of enzymes What are key benefits of enzym.pdf
 
Using Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdfUsing Linux, while a directory may seem empty because it doesnt co.pdf
Using Linux, while a directory may seem empty because it doesnt co.pdf
 
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdfUnlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
Unlike carbohydrates, proteins, and nucleic acids, lipids are define.pdf
 
True or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdfTrue or False A characteristic of a variable on interval level is t.pdf
True or False A characteristic of a variable on interval level is t.pdf
 
To understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdfTo understand de Broglie waves and the calculation of wave properties.pdf
To understand de Broglie waves and the calculation of wave properties.pdf
 
Think about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdfThink about the similarities and differences that benign (non-cancer.pdf
Think about the similarities and differences that benign (non-cancer.pdf
 
The probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdfThe probability of getting a C in stat is .20. if students attend cl.pdf
The probability of getting a C in stat is .20. if students attend cl.pdf
 
Sally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdfSally wants to express her gene of interest in E. coli. She inserts .pdf
Sally wants to express her gene of interest in E. coli. She inserts .pdf
 
Short answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdfShort answers ·GUI elements Static methods and variables Overloading .pdf
Short answers ·GUI elements Static methods and variables Overloading .pdf
 
Rectangle class inherits members of Shape class Has one method calle.pdf
Rectangle class inherits members of Shape class  Has one method calle.pdfRectangle class inherits members of Shape class  Has one method calle.pdf
Rectangle class inherits members of Shape class Has one method calle.pdf
 
Operating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdfOperating leases have historically been controversial because there .pdf
Operating leases have historically been controversial because there .pdf
 
Of the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdfOf the ECG traces shown on the back of this page, which one represe.pdf
Of the ECG traces shown on the back of this page, which one represe.pdf
 
Multiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdfMultiple choice of secant functionsI know D isnt the correct ans.pdf
Multiple choice of secant functionsI know D isnt the correct ans.pdf
 
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdfPart 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
Part 1 - Written AnswersDownload the GridWriter.zip file and exami.pdf
 
Lets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdfLets suppose that a species of mosquito has two different types of.pdf
Lets suppose that a species of mosquito has two different types of.pdf
 
Let X be a random variable with cumulative distribution function F(a.pdf
Let X be a random variable with cumulative distribution function  F(a.pdfLet X be a random variable with cumulative distribution function  F(a.pdf
Let X be a random variable with cumulative distribution function F(a.pdf
 
John and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdfJohn and Mike have been friends since school but havent seen eachoth.pdf
John and Mike have been friends since school but havent seen eachoth.pdf
 
How can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdfHow can you tell if a URL or Domain is open or closed to the public.pdf
How can you tell if a URL or Domain is open or closed to the public.pdf
 

Recently uploaded

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 

Recently uploaded (20)

Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 

Java Question help needed In the program Fill the Add statements.pdf

  • 1. Java Question : help needed In the program "Fill the Add statements Area" here area. Sample Output in the picture 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 an asterisk. This is the program for it 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; } }
  • 2. 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 ) { this.width = width; this.height = height; this.maze = new char [this.width][this.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
  • 3. } /** * 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
  • 4. * 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. for(int x=0; x for(int y=0; y buffer.append(maze[x][y]); buffer.append(" "); return buffer.toString(); } } 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 );
  • 5. 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 ); } }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 );// Remove new line after intscanner.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 Mazereturn 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 )));}} Solution import java.io.*; class Maze { class MazeSquare { public boolean hasTopWall; public boolean hasRightWall; public boolean hasLeftWall; public boolean hasBottomWall; public MazeSquare() { hasTopWall = hasRightWall = hasLeftWall = hasBottomWall = false;
  • 6. } } class MazeFormatException extends Exception { public final static int noError = 0; public final static int badRowAndColumnCounts = 1; public final static int wrongNumberOfEntriesInRow = 2; public final static int badEntry = 3; public int lineNumber; public int problem; public MazeFormatException( int line, int prob ) { lineNumber = line; problem = prob; } } private int nRows; private int nColumns; MazeSquare[][] square; public static void main( String[] args ) { if( args.length != 1 ) { System.err.println( "Usage: java Maze mazeFileName" ); System.exit( 1 ); } Maze knosos = null; try { knosos = new Maze( args[0] ); } catch( FileNotFoundException e ) { System.err.println( "Can't open " + args[0] + ". Check your spelling." ); System.exit( 1 ); }
  • 7. catch( IOException e ) { System.err.println( "Severe input error. I give up." ); System.exit( 1 ); } catch( MazeFormatException e ) { switch( e.problem ) { case MazeFormatException.badRowAndColumnCounts: System.err.println( args[0] + ", line " + e.lineNumber + ": row and column counts expected" ); break; case MazeFormatException.wrongNumberOfEntriesInRow: System.err.println( args[0] + ", line " + e.lineNumber + ": wrong number of entries" ); break; case MazeFormatException.badEntry: System.err.println( args[0] + ", line " + e.lineNumber + ": non-hexadecimal digit detected" ); break; default: System.err.println( "This should never get printed." ); break; } System.exit( 1 ); } knosos.print( System.out ); } public Maze() { square = null; nRows = nColumns = 0; } public Maze( String fileName ) throws FileNotFoundException, IOException, MazeFormatException
  • 8. { BufferedReader in = null; in = new BufferedReader( new FileReader( fileName ) ); load( in ); in.close(); } public void load( BufferedReader in ) throws MazeFormatException { String line; String[] tokens; int lineNumber = 0; try { line = in.readLine(); lineNumber++; tokens = line.split( "s+" ); nRows = Integer.parseInt( tokens[0] ); nColumns = Integer.parseInt( tokens[1] ); if( nRows <= 0 || nColumns <= 0 ) throw new Exception(); } catch( Exception e ) { throw new MazeFormatException( lineNumber, MazeFormatException.badRowAndColumnCounts ); } // Allocate the 2D array of MazeSquares. square = new MazeSquare[nRows][nColumns]; for( int i=0; i < nRows; i++ ) for( int j=0; j < nColumns; j++ ) square[i][j] = new MazeSquare(); for( int i=0; i < nRows; i++ ) { try { line = in.readLine();
  • 9. lineNumber++; tokens = line.split( "s+" ); if( tokens.length != nColumns ) throw new Exception(); } catch( Exception e ) { throw new MazeFormatException( lineNumber, MazeFormatException.wrongNumberOfEntriesInRow ); } for( int j=0; j < nColumns; j++ ) { int squareValue; try { squareValue = Integer.parseInt( tokens[j], 16 ); } catch( NumberFormatException e ) { throw new MazeFormatException( lineNumber, MazeFormatException.badEntry ); } // These are "bitwise AND" operations. We'll discuss them in class. square[i][j].hasTopWall = ((squareValue & 1) != 0); square[i][j].hasRightWall = ((squareValue & 2) != 0); square[i][j].hasBottomWall = ((squareValue & 4) != 0); square[i][j].hasLeftWall = ((squareValue & 8) != 0); } } } public void print( PrintStream out ) { int i, j; for( i=0; i < nRows; i++ ) { out.print( '+' );
  • 10. for( j=0; j < nColumns; j++ ) { if( i > 0 && square[i][j].hasTopWall != square[i-1][j].hasBottomWall ) out.print( "xxx+" ); else if( square[i][j].hasTopWall ) out.print( "---+" ); else out.print( " +" ); } out.println(); if( nColumns > 0 && square[i][0].hasLeftWall ) out.print( '|' ); else out.print( ' ' ); for( j=0; j < nColumns; j++ ) { if( j < nColumns - 1 && square[i][j].hasRightWall != square[i][j+1].hasLeftWall ) out.print( " X" ); else if( square[i][j].hasRightWall ) out.print( " |" ); else out.print( " " ); } out.println(); } if( nRows > 0 ) { out.print( '+' ); for( j=0; j < nColumns; j++ ) { if( square[nRows-1][j].hasBottomWall ) out.print( "---+" ); else out.print( " +" ); } out.println();
  • 11. } } }