Use stacks to implement a program in JAVA that will prompt the use to type a word, then print that word in reverse order of letters, then ask the user for another word, and so on. The program should terminate when the user enters "STOP" in upper case.
Your deliverables shall include:
Step-1)Define a method reverseWord . Iterate on the given word and push the letters in the word in a stack.
Step-2)Continue popping letters from stack till the stack is not empty and append those letters in a StringBuilder.
Step-3)Convert StringBuilder to String and return it.
To write testcases create a test class => In the test class create a method test_reverseWord()
Create a instance of ReverseWord Class and call reverseWord method using it and pass the word you wish to reverse as an argument to it.
Finally use assertEquals to match the response with expected result.
import java.util.Scanner;
import java.util.Stack;
public class Reverseword {
public Reverseword(){
}
public static String reverseWord(String word){
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<word.length();i++){
stack.push(word.charAt(i));
}
StringBuilder result = new StringBuilder();
while(!stack.empty()){
result.append(stack.pop());
}
return result.toString();
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
while(true) {
System.out.println("enter a word to get reversed : ");
String word = sc.next();
if (word.equals("STOP"))
System.exit(0);
else {
System.out.println("Reversed word is : " + reverseWord(word));
}
}
}
}
Testcases:
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ReversewordTest {
public Reverseword reverseword;
@Before
public void setUp() throws Exception {
reverseword = new Reverseword();
}
@Test
public void test_reverseWord() {
String value1 = this.reverseword.reverseWord("java");
Assert.assertEquals("avaj", value1);
String value2 = this.reverseword.reverseWord("HomeworkLib");
Assert.assertEquals("ggehc", value2);
}
}



If you have any doubt in understanding the code or running the code , just comment on the answer and i would be happy to help you out.
Use stacks to implement a program in JAVA that will prompt the use to type a...
JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...
JAVA Problem: Coffee do not use ArrayList or Case Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare ();...
I have to use java programs using netbeans. this
course is introduction to java programming so i have to write it in
a simple way using till chapter 6 (arrays) you can use (loops ,
methods , arrays)
You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...
Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...
C++: Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the Linked List....
JAVA Problem: Coffee Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
IN JAVA Overview In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide the user to decide between different forms...
Create a Java program that uses methods. In the main method, the program must prompt the user for a Social Security Number in the following format: DDD-DD-DDDD, where D is a digit. The program must call a method called “validate” that must receive the inputted SSN from the main method and decide whether the inputted number is valid and return the decision to the main method. Then, the main method must print the decision.
In Java, design and implement the class day that implements the day of the week in a program. The class day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of the type of day: Set the day. Print the day Return the day Return the next day Return the previous day. Calculate and return the day by adding certain days to the current day. For example,...