Stacks
The goal of this lab period is to build and test a stack and to learn how to open and parse files in Java.
Tasks
Cell Class
Write a simple Cell class, similar to the Cell class from last week. The Cell will represent one location in a Sudoku board.
- Setup
Create a working directory for today's lab. Then start a Java file called Cell.java and make a public class called Cell. The Cell class should have four fields to hold the row index, column index, value, and whether the value is locked. Use integers for the first three and a boolean for the last.
- Write your constructors
Write three different constructors for the Cell class.
public Cell()- initialize all values to 0 or false;public Cell(int row, int col, int value)- initialize the row, column, and value fields to the given parameter values. Set the locked field to false;public Cell(int row, int col, int value, boolean locked)- initialize all of the Cell fields given the parameters.
- Write the accessor and mutator functions
The following accessor and mutator functions manage the position of a cell (row, col), its value (number), and whether it is a locked value.
public int getRow()- return the Cell's row index.public int getCol()- return the Cell's column index.public int getValue()- return the Cell's value.public void setValue(int newval)- set the Cell's value.public boolean isLocked()- return the value of the locked field.public void setLocked(boolean lock)- set the Cell's locked field to the new value.
- Write a toString function
The toString function should generate and return a representating String. In this case, have it return a string with the Cell's numeric value.
- Test your Cell class with the following code.
Cell Stack
Next, create a CellStack class that implements a node-based stack. It should look pretty similar to last week's LinkedList, but with far fewer methods.
- Make a CellStack class
Implement a stack data structure that can hold items of type Cell. It should implement the following functions, where each function should have a O(1)-runtime. You'll probably want an inner Node class, a field for the head of the list, and a field for the size.
public CellStack()- initialize the stack's fields.public void push( Cell c )- push c onto the stack.public Cell peek()- return the top Cell on the stack.public Cell pop()- remove and return the top element from the stack; return null if the stack is empty.public int size()- return the number of elements in the stack.public boolean empty()- return true if the stack is empty.
- Test your stack with the following code.
Reading Files
The final part of the lab is to get practice reading data from a file.
You will be using the java classes FileReader and BufferedReader
- Create a new file Board.java. In it, create a new class Board. For now, it does not need any fields. You should import java.io.*.
- Write a read function using the following
template.
public boolean read(String filename) { try { // assign to a variable of type FileReader a new FileReader object, passing filename to the constructor FileReader fr = new FileReader(filename); // assign to a variable of type BufferedReader a new BufferedReader, passing the FileReader variable to the constructor BufferedReader br = new BufferedReader(fr); // assign to a variable of type String line the result of calling the readLine method of your BufferedReader object. String line = br.readLine(); // start a while loop that loops while line isn't null while(line != null){ // assign to an array of type String the result of calling split on the line with the argument "[ ]+" // print the String (line) // print the size of the String array (you can use .length) // assign to line the result of calling the readLine method of your BufferedReader object. } // call the close method of the BufferedReader br.close(); return true } catch(FileNotFoundException ex) { System.out.println("Board.read():: unable to open file " + filename ); } catch(IOException ex) { System.out.println("Board.read():: error reading file " + filename); } return false; } - Write a main function that creates a new Board and calls the read method. Use a command line argument as the filename you pass to the read method.
- Download test1.txt and test2.txt and make sure the output makes sense.
When you are done with the lab, go ahead and get started on the current project.