Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 20, Problem 1AW
Program Plan Intro
Stack:
- A stack is a linear data structure.
- It follows the order FILO (first in last out) LIFO or (last-in-first-out) for performing operations on it.
- The two main operations performed on a stack are,
- Push() – Add item to the stack.
- Pop() – Remove item from the stack.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Use a stack to reverse the words of a sentence. Keep reading words until you have a word that ends in a period, adding them onto a stack. When you have a word with a period, pop the words off and print them. Stop when there are no more words in the input. For example, you should turn the inputMary had a little lamb. Its fleece was white as snow.intoLamb little a had mary. Snow as white was fleece its.Pay attention to capitalization and the placement of the period.
This is what I have so far:
import java.util.Scanner;import java.util.Stack;
/** Class for reversing the order of a sentence.*/public class SentenceReverser{ /** Reverses the given sentence. @param sentence Sentence to be reversed. @return Reversed sentence. */ public static String reverse(String sentence) { String reversed = ""; Scanner scanner = new Scanner(sentence); Stack<String> stack = new Stack<String>();
// put your work below // ... return…
Implement a class called ReverseWords that uses a stack to output a set of elements input by the user in reverse order.
Use a stack to reverse the words of a sentence. Keep reading words until you have a word that ends in a period, adding them onto a stack. When you have a word with a period, pop the words off and print them. Stop when there are no more words in the input.
Chapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Ch. 20.3 - Prob. 20.1CPCh. 20.3 - Prob. 20.2CPCh. 20.3 - Prob. 20.4CPCh. 20.3 - Prob. 20.5CPCh. 20.6 - Prob. 20.6CPCh. 20.6 - Prob. 20.7CPCh. 20.6 - Prob. 20.8CPCh. 20.6 - Prob. 20.9CPCh. 20 - Prob. 1MCCh. 20 - Prob. 2MC
Ch. 20 - Prob. 3MCCh. 20 - The concept of seniority, which some employers use...Ch. 20 - Prob. 5MCCh. 20 - Prob. 6MCCh. 20 - Prob. 8TFCh. 20 - Prob. 9TFCh. 20 - Prob. 10TFCh. 20 - Prob. 1FTECh. 20 - Prob. 2FTECh. 20 - Prob. 3FTECh. 20 - Prob. 4FTECh. 20 - Prob. 5FTECh. 20 - Prob. 1AWCh. 20 - Prob. 2AWCh. 20 - Suppose that you have two stacks but no queues....Ch. 20 - Prob. 1SACh. 20 - Prob. 2SACh. 20 - Prob. 3SACh. 20 - Prob. 4SACh. 20 - Prob. 5SACh. 20 - Prob. 6SA
Knowledge Booster
Similar questions
- Write a non-recursive Java method for printing all permutations of the numbers {1,2, ..., n}Hint: use an explicit stackpublic class QueueByStack<E> {Stack<E> a = new Stack<>();Stack<E> b = new Stack<>(); public int size() {//write code here} public boolean isEmpty() {//write code here} public void eneque(E e) {//write code here} public E deque() {//write code here} public E first() {//write code here} @Overridepublic String toString() {//write code here}}arrow_forwardThe implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forwardWrite a method which takes as input a singly linked list and print the first duplicatenumber. Please answer it in Java coed.arrow_forward
- Write a program that reads a list of words from standard input and prints all twoword compound words in the list. For example, if after, thought, and afterthought are in the list, then afterthought is a compound word.arrow_forwardWrite a Method Called makePalindrome() that will return a linked list with a palindrome of the calling list. Example [1,2,3] must return [1,2,3,3,2,1] and [8,9,1,4] must return [8,9,1,4,4,1,9,8].arrow_forwardPlease use Java Write a method “removeBottomItemStack” that takes a stack of integers and removes the first item added (the one at the bottom).arrow_forward
- Write a program that uses a stack to test input strings to determine whether theyare palindromes. A palindrome is a sequence of words that reads the same as thesequence in reverse: for example, noon. Use this Python Example : class Stack:'''TODO: Remove the "pass" statements and implement each methodAdd any methods if necesssaryDON'T use any builtin stack class to store your items'''def __init__(self): # Constructor functionpassdef isEmpty(self): # Returns True if the stack is empty or False otherwisepassdef len(self): # Returns the number of items in the stackpassdef peek(self): # Returns the item at the top of the stackpassdef push(self, item): # Adds item to the top of the stackpassdef pop(self): # Removes and returns the item at the top of the stackpassdef palindrome(input_str):isPalindrome = False#TODO: Your work here# Return if input_str is palindrome or notreturn isPalindromeif __name__ == "__main__":my_str = "noon"print(palindrome(my_str)) # Correct Output: True#TODO…arrow_forwardYou have a stack of n boxes, with widths w1, heights hi, and depths di. The boxescannot be rotated and can only be stacked on top of one another if each box in the stack is strictlylarger than the box above it in width, height, and depth. Implement a method to compute theheight of the tallest possible stack. The height of a stack is the sum of the heights of each box.arrow_forwardThe calculator works as follows: if number, push to a stack if operator, pop two numbers, operate on them, and push the result if I hit an operator and there are not two numbers on the stack, it's a bad expression if I get to the end of the expression and there is not exactly one number on the stack, it's a bad expression. YOUR IMPLEMENTATION, 30 points: 10 - the input to output happening without a crash 10 for implementing LinkedListBasedStack 10 for implementing ArrayList YOUR CALCULATOR ONLY NEEDS TO WORK FOR +, -, and * YOUR CALCULATOR ONLY NEEDS TO WORK ON INTEGERS. RPNStringTokenizer.java package template; import java.util.ArrayList; public interface RPNStringTokenizer { public static ArrayList<String> tokenize (String expression) { // take a string. If it is valid RPN stuff - integers or operators // - then put them in a list for processing. // anything bad (not int or one of the operators we like) and return a null. // YOU WRITE THIS! return null; } }…arrow_forward
- Write a method max() that takes a reference to the first node in a linked list asargument and returns the value of the maximum key in the list. Assume that all keys arepositive integers, and return 0 if the list is emptyarrow_forwardFOR JAVA Consider rewriting the method max() given below such that it gives the correct answer even if the linked list contains positive and/or negative integers on their .item variable. public int max(Node x) { intmaxValue=0; while(x!=null){ if(x.item<maxValue)maxValue=x.item; x=x.next; } returnmaxValue; } }arrow_forwardin java languageWrite a program that implements the methods of a list using LINKED LISTS. Send source codeCreate a menu with an option for each of the list methods.END(L)PRINT_LIST(L)INSERT(x,p,L)LOCATE(x, L)RECOVER(p,L)DELETE(p, L)NEXT(p,L)PREVIOUS(p,L)DESTROY(L)FIRST(L)EMPTY(L) Validate p(position) between 1 and the size of the list.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning