Question: Sentence word reverser Design and implement a class (that includes a main method) that reads a sentence from the user and prints the sentence out

Sentence word reverser Design and implement a class (that includes a main method) that reads a sentence from the user and prints the sentence out with the words in the SAME order, but with the characters of each word backwards. Use ArrayStack to reverse the characters of each word in the sentence. Hint! Using two Scanners for this will make getting and splitting the string easier. Use one to get the initial sentence from the input stream (using .nextLine()). Then use another that doesnt scan the input stream, but rather scans the string (using .next()) to get each word. The constructor to Scanner can be passed System.in for the input stream or a string instead. See the following session: Enter a sentence: Hello nice to meet you Reversing each word: olleH ecin ot teem uoy

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

PLEASE USE THIS ALREADY GIVEN CLASSES / INTERFACE

public class EmptyCollectionException extends Exception { private static final long serialVersionUID = 358083002087971606L;

public EmptyCollectionException() { super(); } public EmptyCollectionException(String msg) { super(msg); } }

import java.util.Arrays;

public class ArrayStack implements StackADT //We agree to supply all functions that we say exist StackADT Functions correlate to ArrayStack { private final static int DEFAULT_CAPACITY = 100; private int top; private T[] stack; public ArrayStack() { this(DEFAULT_CAPACITY); } @SuppressWarnings("unchecked") public ArrayStack(int initialCapacity) { top = 0; stack = (T[]) new Object [initialCapacity]; //Have to cast Object Array to T array in order for placeholder to work. Java just works like that. } public void push( T element) { if(size() == stack.length) expandCapacity(); stack [top] = element; ++top; } public T pop() throws EmptyCollectionException { if(isEmpty()) throw new EmptyCollectionException(); --top; T result = stack[top]; stack [top] = null; // Top before that ends up being empty; Makes it clean return result; } public T peek() throws EmptyCollectionException { if(isEmpty()) throw new EmptyCollectionException(); return stack[top -1]; } public int size() { return top; } public boolean isEmpty() { // Even shorter return top ==0; // Short // boolean a = top == 0; // return a; // long way // if(top==0) // return true; // else // return false; } public void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); //Would values stay the same but only place in memory changes? Hexa decimal #'s? } public String toString() { String output = "ArrayStack-> [ "; for(int i =top-1; i >=0 ; i--) { output+= stack[i].toString() + " "; } output+= "]"; return output; } }

public interface StackADT //Provide layout of what we want something to do w/o supplying the way it gets done = interface { public void push( T element) ; public T pop () throws EmptyCollectionException; public T peek() throws EmptyCollectionException; public boolean isEmpty(); public int size(); }

public class SentenceWordReverser {

public static void main(String[] args) { // TODO Auto-generated method stub

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!