Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Stack to solve problems (answer should be in Java) 1) Design and Implement a Java program that reads a sentence from the user and

Using Stack to solve problems (answer should be in Java)

1) Design and Implement a Java program that reads a sentence from the user and prints the sentence with the characters of each word backwards. Use the ArrayStack in part 1 to reverse the characters of each word. A sample output is shown in Figure 1. Note that different sentence will be used for grading. It is required that the ArrayStack class should be utilized in solving this problem.

image text in transcribed

2) Implement a postfix-to-infix translator using stacks. Postfix notation1[1] is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity, some popular hand-held calculators use postfix notation to avoid the complications of the multiple parentheses required in nontrivial infix expressions. We have discussed how these postfix expressions can be evaluated using a stack in class. You are then to write a Java program to translate a postfix notation to infix notation. A sample output is shown in Figure 2, and some postfix expressions are given in as below for testing purpose. Note that different postfix notations will be used for grading. It is required that the LinkedStack class in part 1 should be utilized in solving this problem.

image text in transcribed

ArrayStack Class: ----------

public class ArrayStack implements StackADT { //variables or attributes private boolean debug = true; private T[] stack; private int top; //gives the next available free array slot, 2) gives the number of elements in the stack private final int MAX = 100; ArrayStack(){ top = 0; stack = (T[]) (new Object[MAX]); } ArrayStack(int initialCapacity) { top = 0; stack = (T[]) (new Object[initialCapacity]); } //part 2: constructors public String toString(){ String result = ""; for(int i = 0; i out.println("calling expandCapacity "+stack.length +" to "+ stack.length *2); } T[] larger = (T[])(new Object[stack.length * 2]); System.arraycopy(stack, 0, larger, 0, stack.length); stack = larger; } }
run: Enter a sentence: Hello nice to meet you in CS102 Reversing each word: ollet ecin ot teem uoy ni 2015C BUILD SUCCESSFUL (total time: 14 seconds) Figure 1: reverse character run: Enter a postfix expression: 3 4 + 2 * In Infix Notation: ((3 - 4) * 2) Translate another expression (Y/N]? Y Enter a postfix expression: 4 2 - 3 5 1 - * - In Infix Notation: ((4 + 2) + (3 (5 - 1))) Translate another expression (Y/N)? Figure 2: postfix-to-infix translator Some Postfix For Testing 45 72 +-*-16 - 3 4 + 2 * 7/2+ 5 7 + 6 2 - * 48 - 42 351-+* + 18 * 42 + 351 - * + 18 /

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions