CS 231: Lab #3

Title image Spring 2020


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

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

  1. 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.

  2. Write your constructors

    Write three different constructors for the 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 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.
  4. Write a clone function

    The clone function should return a new Cell with the same values as the existing Cell. First make a new Cell, then copy the data from the current Cell fields to the new Cell fields.

  5. 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.

  6. Test

    Test your Cell code using TestCell.java. Your output should look something like this

Cell Stack

Next, create a CellStack class that implements an array-based Stack that can hold Cell types. You may not use an ArrayList to implement your stack; build it with regular arrays.

  1. Make a CellStack class

    Implement a stack data structure that can hold items of type Cell. It should implement the following functions.

    • public CellStack() - initialize the stack to a default size.
    • public CellStack(int max) - initialize the stack to hold up to max elements.
    • public void push( Cell c ) - if there is space, push c onto 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 selements on the stack.
    • public boolean empty() - return true if the stack is empty.

    + (more detail)

    1. Create a new file CellStack.java. Start a class CellStack. The CellStack needs two fields. First, it needs an array of Cells. Second, 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, allocate an array of Cells of a size of your choice (e.g. 10), and set the top index to 0. The second constructor should take one argument, which is the initial size of the stack, allocate an array of the specified size, and set the top index to 0.
    3. Write the functions push, pop, and size, and empty.
      • public void push(Cell c) - if there is space for another item on the stack, push c onto the stack by assigning the Cell c to the top location, then increment the top location and return. If there is not sufficient 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, assign the top item from the stack to a local variable of type Cell. Then decrement the top index and return the Cell that was removed.
      • public int size() - return the number of elements on the stack.
      • public boolean empty() - return true if the stack is empty, otherwise false.
  2. 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 filename to the constructor
          // assign to a variable of type BufferedReader a new BufferedReader, passing the FileReader variable to the constructor
          // assign to a variable of type String line the result of calling the readLine method of your BufferedReader object.
          // start a while loop that loops while line isn't 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
          // 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.