Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step: 3

blur-text-image

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

MFDBS 89 2nd Symposium On Mathematical Fundamentals Of Database Systems Visegrad Hungary June 26 30 1989 Proceedings

Authors: Janos Demetrovics ,Bernhard Thalheim

1989th Edition

3540512519, 978-3540512516

More Books

Students also viewed these Databases questions

Question

Find the median of the density f (x) 12 exp(jxj) , x 2 R.

Answered: 1 week ago

Question

How digital evidence has changed from Windows Vista to Windows 10?

Answered: 1 week ago

Question

6. Identify seven types of hidden histories.

Answered: 1 week ago

Question

What is the relationship between humans and nature?

Answered: 1 week ago