Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED HELP WITH THIS PROJECT IMMEDIATELY! ALL CODE MUST BE DONE IN JAVA ONLY! Write a program that uses Stacks to determine if a line

NEED HELP WITH THIS PROJECT IMMEDIATELY! ALL CODE MUST BE DONE IN JAVA ONLY!

Write a program that uses Stacks to determine if a line is well-formed or not. The program should read user input, and should push an open ( when it is encountered, and perform a pop when a closed ) is encountered.

This way if at the end of a program run, the stack is empty, then the line is considered well-formed.

If the end input is reached and the stack is not empty, that means there were too many open parentheses, (.

If at any time, the program sees a closed parenthesis, ), and the stack is empty already, then an exception will be thrown and this is an indicator that there are too many, or at least a misplaced, closed parenthesis.

Either situation should cause the program to indicate that it is not well-formed.

An example run may look like this: Please input a set of parentheses

()(())(())

--> Input is well formed.

Another example might look like this:

Please input a set of parentheses

)(())(())

Sorry, input is not well formed.

My source code for the project is included. Most of the work should already be done and ready to go, but I keep running into two strange errors. One is a complier error: error: cannot infer type arguments for MyStack StackInterface openDelimiterStack = new MyStack<>(); reason: cannot use '<>' with non-generic class MyStack. I have no idea what isn't working so if anyone can help me out here I would greatly appreciate it. The second is an error where when the project is running no matter how many paretheses in no matter the combination I input I always get Sorry, input is not well formed. return to me. If you can help fugure out where these two issues are coming from I would be so grateful.

Source code:

//CLIENT FILE

project2.java

package project2; import project2.Interface.StackInterface;

/** * * @author Darryl */ public class Project2 {

/** * @param expression * @return */ public static boolean checkBalance(String expression) { StackInterface openDelimiterStack = new MyStack<>();

int characterCount = expression.length(); boolean isBalanced = true; int index = 0; char nextCharacter = ' '; while (isBalanced && (index < characterCount)) { nextCharacter = expression.charAt(index); switch (nextCharacter) { case '(': openDelimiterStack.push(nextCharacter); break; case ')': if (openDelimiterStack.isEmpty()) isBalanced = false; else { char openDelimiter = openDelimiterStack.pop(); isBalanced = isPaired(openDelimiter, nextCharacter); } // end if break; default: break; // Ignore unexpected characters } // end switch index++; } // end while if (!openDelimiterStack.isEmpty()) isBalanced = false; return isBalanced; } // end checkBalance // Returns true if the given characters, open and close, form a pair // of parentheses, brackets, or braces. private static boolean isPaired(char open, char close) { return (open == '(' && close == ')'); } // end isPaired } // end BalanceChecker

//IMPLEMENTATION FILE

MyClass.java

package project2; import java.util.Stack; import java.util.Scanner; /** * * @author Darryl */ public class MyStack { public static void main(String[]args) { System.out.print(" Expression is valid only with ()"); System.out.print(" "); for (int i = 0; i < 10000; i++) { Scanner exp = new Scanner(System.in); System.out.print(" Please input a set of parenthesis:"); String input = exp.next(); Stack BalanceChecker = new Stack(); for (int j =0; j < input.length(); j++) { char ex = input.charAt(j); if (ex =='(') { BalanceChecker.push(ex); } else if (ex =='(') { if (BalanceChecker.isEmpty()) { System.out.println("Sorry, but the input is not well formed."); return; } char open; open = (char) BalanceChecker.pop(); if (!(open == '(' && ex == ')')) { System.out.println("Sorry, but the input is not well formed."); return; } } } if(BalanceChecker.isEmpty()) { System.out.print("This input is well formed."); } else { System.out.print("Sorry, the input is not well formed."); } } } }

//STACK INTERFACE StackInterface.java

package project2.Interface;

/** * * @author Darryl */ public interface StackInterface { public void push(T newEntry); public T pop(); public T peek(); public boolean isEmpty(); public void clear(); }

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_2

Step: 3

blur-text-image_3

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

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago