Lab29a (1)
Lab29a (1)
Assignment Purpose:
Demonstrate knowledge of Java's Stack class. For this assignment you will write
a program with the standard Stack class. Without knowledge or even concern
for the implementation of this stack class, write the specified "Maze" program.
Write a program that randomly generates a maze, and a random starting position. The program then
needs to find a path out off the maze, if it is possible, to exit the maze, using a backtrack algorithm.
Backtracking is an excellent example of the importance of the stack ADT.
The maze is solved using the Stack class. The Stack object needs to stores coordinate values of
every visited location. The interface information of the Stack class is provided below.
// Lab29ast.java
// This is the student version of the Lab29a assignment.
// Completing this file, as is, is the 100 point version.
// For 80 points you will be given the <getMove> code.
import java.util.*;
class Maze
{
private char mat[][]; // 2d character array that stores the maze display
private Coord currentMove; // object that stores current maze position
private Stack visitStack; // stack that stores location that have been visited
class Coord
// Coord is a class that stores a single maze location.
{
private int rPos;
private int cPos;
public Coord (int r, int c) { rPos = r; cPos = c; }
public boolean isFree() { return (rPos == 0 && cPos == 0); }
public void setPos(int r, int c) { rPos+= r; cPos+= c; }
}
void displayMaze()
// displays the current maze configuration
{
System.out.println("\nRANDOM MAZE DISPLAY\n");
for (int r = 0; r < 12; r++)
{
for (int c = 0; c < 12; c++)
System.out.print(mat[r][c] + " ");
System.out.println();
}
System.out.println();
pause();
}
O X X X X X X X X X X X
X O O X O O X O X O O X
X O X X X X X O O O X X
X X X O O O X X X X X X
X O O O X O O X O X X X
X X O X O O X O O X X X
X X O X O X O X X X O .
X X O O O X O O X O O X
X O O X O X O O O O X X
X X X O X X X X X O X X
X O O X X O O X X X X X
X X X X X X X X X X X X
O X X X X X X X X X X X
X O O X O O X O X O O X
X O X X X X X O O O X X
X X X . . . X X X X X X
X . . . X . . X . X X X
X X . X . . X . . X X X
X X . X . X . X X X . .
X X . . . X . . X . . X
X . . X . X . . . . X X
X X X . X X X X X . X X
X . . X X O O X X X X X
X X X X X X X X X X X X
O X X X X X X X X X X X
X O O X O O X X X X X X
X O O X O X O X O O O X
X X X O O X X O O O X X
X X X X O O O O O X O X
X X X X O O O X X X X X
X O X O X X O X O X O X
X X X X X O O X X X O X
X X X O O X X O O X O X
X O X X O O O X O O O X
X X X X X O O X O X O .
X X X X X X X X X X X X
. X X X X X X X X X X X
X . . X . . X X X X X X
X . . X . X . X . . . X
X X X . . X X . . . X X
X X X X . . . . . X . X
X X X X . . . X X X X X
X O X O X X . X O X . X
X X X X X . O X X X . X
X X X O . X X . . X . X
X O X X . . . X . . . X
X X X X X . . X . X . .
X X X X X X X X X X X X
. X X X X X X X X X X X
X . . X . . X X X X X X
X . . X . X . X . . . X
X X X . . X X . . . X X
X X X X . . . . . X . X
X X X X . . . X X X X X
X O X O X X . X O X . X
X X X X X . O X X X . X
Exposure Java 2008 Chapter Lab 29a Page 5 07-26-08
X X X O . X X . . X . X
X O X X . . . X . . . X
X X X X X . . X . X . .
X X X X X X X X X X X X
Assignment Purpose:
Demonstrate knowledge of implementing required stack methods.
Write a MyStack class, which implements the standard stack methods. You will be provided with a
student test program that checks to see if your class works correctly. This is one of the few
assignments with a single, 100-point version.
You will need to implement the following methods listed below. Required parameters, if any, and
data types are intentional not shown.
MyStack
// Constructor
isEmpty
// Returns true if data is empty, false otherwise
push
// Adds variable to the top of the stack
pop
// Returns and removes the top element from the stack
peek
// Returns the top element from the stack without removal
Exposure Java 2008 Chapter Lab 29a Page 6 07-26-08
Lab29b 80 Point Version One Required Output
Lab29b 80 Point Version