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...
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....
Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), 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...
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,...
Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...
Overview: In this lab you are to write a Java program to prompt the user for an integer indicating a year and then print the calendar for that year. Objective: This lab's objective is to exercise you with the use of Java’s control statements. You are required to use exactly one while statement, one for statement and one switch statement. You will also practice on how to use some basic input methods of the Scanner class and some formatting techniques...