CS 231: Lab #3

Title image Spring 2018


The goal of this lab period is to build and test a stack and to learn how to open and parse files in Java.


Cell Class

First, write a simple Cell class, similar to the Cell class from last week. The Cell will represent one location in a Sudoku board.

  1. 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 whold the row index, column index, value, and whether the value is locked. Use integers for the first three and a boolean for the last.
  2. Write three different constructors for your Cell class.
    • public Cell(): initialize all valus 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.
  3. Write the following accessor functions.
    • 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 if the Cell is locked.
    • public void setLocked(boolean lock): set the Cell's locked value.
  4. Write a clone function that returns a new Cell with the same values as the existing Cell.
  5. Write a toString function that generates a string that describes the Cell's fields.

Cell Stack

Next, create a CellStack class that implements an array-based Stack that can hold Cell types.

  1. Create a new file CellStack.java. Start a class CellStack. The CellStack needs three fields. First, it needs an array of Cells. Second, it needs an integer indicating the maximum number of items that will fit on the stack. Third, it needs an integer indicating the next free location on the stack.
  2. Write two different constructors for your stack. The first should take no arguments, initialize the maximum size to 10, allocate an array of 10 Cells and set the top index to 0. The second constructor should take one argument, which is the initial size of the stack. Set the maximum size to the parameter, allocate an array of the specified size, and set the top index to 0.
  3. Write the functions push, pop, and size.
    • public void push(Cell c): if there is space for another item on the stack, push c onto the stack and return. If there is not space, allocate a new Cell array that is twice as big as the prior array, copy all of the elements from the old array to the new array, replace the old array, then push c onto the stack and return.
    • public Cell pop(): if the stack is not empty, pop the top item off the stack and return it.
    • public int size(): return the size of the stack.
    • public boolean empty(): return true if the stack is empty, otherwise false.
  4. Test your stack with the following code.

Reading Files

The final part of the lab is to get practice reading data from a file.

  1. 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.*.
  2. 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 in the filename to the constructor
          // assign to a variable of type BufferedReader a new BufferedReader passing in the FileReader to the constructor
          // start an infinite loop (e.g. while(true) or for(;;)
              // assign to a String variable the result of calling the readLine method of your BufferedReader object.
              // if the String is null, break out of the loop
              // assign to a String array variable the result of calling split on the String with the argument "[ ]+"
              // print the String
              // print the size of the String array
          // call the close method of the BufferedReader
          // 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;
  3. 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.
  4. 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.