Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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, pushconto 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.

-

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

    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.

    3. public boolean read(String filename) {

    4. try {

    5. // assign to a variable of type FileReader a new FileReader object, passing filename to the constructor

    6. // assign to a variable of type BufferedReader a new BufferedReader, passing the FileReader variable to the constructor

    7.

    8. // assign to a variable of type String line the result of calling the readLine method of your BufferedReader object.

    9. // start a while loop that loops while line isn't null

    10. // assign to an array of type String the result of calling split on the line with the argument \"[ ]+\"

    11. // print the String (line)

    12. // print the size of the String array (you can use .length)

    13. // assign to line the result of calling the readLine method of your BufferedReader object.

    14. // call the close method of the BufferedReader

    15. // return true

    16. }

    17. catch(FileNotFoundException ex) {

    18. System.out.println(\"Board.read():: unable to open file \" + filename );

    19. }

    20. catch(IOException ex) {

    21. System.out.println(\"Board.read():: error reading file \" + filename);

    22. }

    23.

    24. return false;

    }

    1. 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.
    2. Download test1.txt and test2.txt and make sure the output makes sense.

    Step by Step Solution

    There are 3 Steps involved in it

    Step: 1

    blur-text-image

    Get Instant Access to Expert-Tailored Solutions

    See step-by-step solutions with expert insights and AI powered tools for academic success

    Step: 2

    blur-text-image_2

    Step: 3

    blur-text-image_3

    Ace Your Homework with AI

    Get the answers you need in no time with our AI-driven, step-by-step assistance

    Get Started

    Recommended Textbook for

    Building Java Programs A Back To Basics Approach

    Authors: Stuart Reges, Marty Stepp

    5th Edition

    013547194X, 978-0135471944

    More Books

    Students also viewed these Programming questions

    Question

    What are your research interests?

    Answered: 1 week ago