Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3740083 • Letter: N

Question

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();
  
  
  
  
}

Explanation / Answer

Hello, there were so many errors in your code. By the given code of StackInterface, I suppose you wanted to implement a custom Stack , but in the MyStack class you were importing java.util.Stack or built in Stack class, that too without specifying the type of elements (should be Character). So I have completely modified the code. Using StackInterface, implemented the class MyStack and all the required methods to push, pop, peek, check if empty etc using a linked list type implementation of the stack. Created an inner class Node to achieve this. And then in the Project2.java, created an instance of MyStack and evaluated the input expression as needed. Check comments for a better understanding. Thanks.

// StackInterface.java

/**

*

* @author Darryl

*/

public interface StackInterface<T> {

                public void push(T newEntry);

                public T pop();

                public T peek();

                public boolean isEmpty();

                public void clear();

}

// MyStack.java

/**

*

* @author Darryl Linked list type implementation of a Stack

*/

public class MyStack<T> implements StackInterface<T> {

                Node<T> top;// points to the top node

                int count;// number of items in the stack

                // default constructor

                public MyStack() {

                                top = null;

                                count = 0;

                }

                @Override

                public void push(T newEntry) {

                                /**

                                * Defining a node

                                */

                                Node<T> node = new Node<T>(newEntry);

                                /**

                                * linking to the stack in top position

                                */

                                if (top == null) {

                                                top = node;

                                } else {

                                                node.next = top;

                                                top = node;

                                }

                                count++;

                }

                @Override

                public T pop() {

                                if (isEmpty()) {

                                                // no elements

                                                return null;

                                }

                                // getting the top element

                                T data = top.data;

                                // updating the top pointer

                                top = top.next;

                                // decrementing the count of items

                                count--;

                                // returning the popped data

                                return data;

                }

                @Override

                public T peek() {

                                if (isEmpty()) {

                                                return null;

                                }

                                // returning the top element without removing

                                return top.data;

                }

                @Override

                public boolean isEmpty() {

                                // if count is 0, stack is empty

                                return count == 0;

                }

                @Override

                public void clear() {

                                // making the stack empty

                                top = null;

                                count = 0;

                }

}

/**

* This is an inner class within MyStack.java to represent one node of the

* stack. It has a data part and a link part to the next node

*/

class Node<T> {

                T data;

                Node<T> next;

                public Node(T data) {

                                this.data = data;

                }

}

// Project2.java

import java.util.Scanner;

/**

*

* @author Darryl

*/

public class Project2 {

                public static void main(String[] args) {

                                /**

                                * Defining a custom stack of character type

                                */

                                MyStack<Character> stack = new MyStack<Character>();

                                Scanner scanner = new Scanner(System.in);

                                /**

                                * prompting and receiving the input expression

                                */

                                System.out.println("Enter an expression: ");

                                String input = scanner.nextLine();

                                try {

                                                /**

                                                * Looping through all the characters in the expression

                                                */

                                                for (int i = 0; i < input.length(); i++) {

                                                                char c = input.charAt(i);

                                                                if (c == '(') {

                                                                                // opening brace, adding to stack

                                                                                stack.push(c);

                                                                } else if (c == ')') {

                                                                                /** closing brace, removing from stack if not exist */

                                                                                if (stack.isEmpty()) {

                                                                                                /**

                                                                                                * expression is already not balanced, because a closing

                                                                                                * brace found before an opening brace

                                                                                                */

                                                                                                throw new Exception();

                                                                                } else {

                                                                                                //removing the top element

                                                                                                stack.pop();

                                                                                }

                                                                }

                                                }

                                                /**

                                                * After the loop, if the stack is empty, the input is balanced, else not

                                                */

                                                if (stack.isEmpty()) {

                                                                System.out.println("Expression is balanced!");

                                                } else {

                                                                System.out.println("Expression is not balanced!");

                                                }

                                } catch (Exception e) {

                                                System.out.println("Expression is not balanced!");

                                }

                }

}

/*OUTPUT*/

Enter an expression:

(((())))()()

Expression is balanced!

Enter an expression:

)(()

Expression is not balanced!