Question
Stacks using a ArrayStack Class and StackADT Interface This assignment has little coding and is more about understanding the typical actions of a stack, such
Stacks using a ArrayStack Class and StackADT Interface This assignment has little coding and is more about understanding the typical actions of a stack, such as push(), pop(), peek(), size(), isEmpty(), etc. This assignment has a ZIP file that was provided in Blackboard. Download the ZIP file and unzip the contents. Do not move any of the files around within the folder it unzips. Step 1: Reverse the Words You are to start with the ReverseWords.java file that was provided to you. It will need to use the ArrayStack class and StackADT interface discussed in the Lewis chapter you read this week. These are provided for you and have already been compiled. You are also provided other files need for this assignment. You are to use complete ReverseWords.java so that the user can type in a sentence and generate output that will display the sentences words in reverse order. To do this: Use the Scanner class to input a sentence from the user from the keyboard (assign to a string). Set up the sentence as a separate Scanner object so that you can read a word at a time (much like you have done when reading from a file). Place (push) each word on the stack. For example, if the sentence is: Dogs make good pets The stack would look like: pets good make Dogs After all the words from the sentence is on the stack, pop each word off the stack and display the words on the console on the same line. For the above example, the display would be: pets good make Dogs This part of the assignment demonstrates your understanding of a stack. The push(), pop(), and isEmpty() methods will be handy. Think about practical applications for a stack. You are to start with the ReverseWords.java file that was provided to you. It will need to use the ArrayStack class and StackADT interface discussed in the Lewis chapter you read this week. These are provided for you and have already been compiled. You are also provided other files need for this assignment Step 2: Use a Caesar Cipher on each word in the stack A simple encryption algorithm is a Caesar Cipher (Google the details if you are unfamiliar). Below is code for encrypting a String using a Caesar cipher. The string to be encrypted is called plainText and the amount of the shift for the cipher is stored in the integer constant called SHIFT. The encrypted string is called cipherText. String cipherText = ""; for (int index=0; index < plainText.length(); index++) cipherText = cipherText + (char)(plainText.charAt(index)+SHIFT); Create a static method that will encrypt a String. The method can use two parameters: (1) the plaintext and (2) the shift amount. You may choose the shift (use a number in the range 1 12). Modify your code so that each word is encrypted, using a Caesar cipher, before you place the word in the stack. Test your code. Now, the sentence should be displayed with each word encrypted and in reverse order from the original sentence. NOTE: The characters within a word are NOT in reverse order. Step 3: Add a loop to allow entry of multiple sentences The program should allow the user to enter as many sentences as they desir.
ReverseWords.java
/* --------------------------------
Fill in your information here
--------------------------------- */
import jsjf.ArrayStack;
import java.util.Scanner;
public class ReverseWords
{
/* This program should use the Scanner class to input a sentence from the user from the keyboard.
Each word from the sentence is placed on a stack.
Each word is popped of the stack to display the sentence with words in a reverse order.
*/
public static void main (String[] args)
{
ArrayStack stack = new ArrayStack();
}
}
ArrayStack.java
package jsjf;
import jsjf.exceptions.*; import java.util.Arrays;
/** * An array implementation of a stack in which the bottom of the * stack is fixed at index 0. * * @author Java Foundations * @version 4.0 */ public class ArrayStack
private int top; private T[] stack; /** * Creates an empty stack using the default capacity. */ public ArrayStack() { this(DEFAULT_CAPACITY); }
/** * Creates an empty stack using the specified capacity. * @param initialCapacity the initial size of the array */ public ArrayStack(int initialCapacity) { top = 0; stack = (T[])(new Object[initialCapacity]); }
/** * Adds the specified element to the top of this stack, expanding * the capacity of the array if necessary. * @param element generic element to be pushed onto stack */ public void push(T element) { if (size() == stack.length) expandCapacity();
stack[top] = element; top++; }
/** * Creates a new array to store the contents of this stack with * twice the capacity of the old one. */ private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); }
/** * Removes the element at the top of this stack and returns a * reference to it. * @return element removed from top of stack * @throws EmptyCollectionException if stack is empty */ public T pop() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack");
top--; T result = stack[top]; stack[top] = null;
return result; } /** * Returns a reference to the element at the top of this stack. * The element is not removed from the stack. * @return element on top of stack * @throws EmptyCollectionException if stack is empty */ public T peek() throws EmptyCollectionException { if (isEmpty()) throw new EmptyCollectionException("stack");
return stack[top-1]; }
/** * Returns true if this stack is empty and false otherwise. * @return true if this stack is empty */ public boolean isEmpty() { return (top == 0); } /** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size() { return top; }
/** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString() { String result = "";
for (int scan=0; scan < top; scan++) result = result + stack[scan] + " ";
return result; } }
StackADT.java
package jsjf;
/** * Defines the interface to a stack collection. * * @author Java Foundations * @version 4.0 */ public interface StackADT
/** * Returns without removing the top element of this stack. * @return the element on top of the stack */ public T peek(); /** * Returns true if this stack contains no elements. * @return true if the stack is empty */ public boolean isEmpty();
/** * Returns the number of elements in this stack. * @return the number of elements in the stack */ public int size();
/** * Returns a string representation of this stack. * @return a string representation of the stack */ public String toString(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started