create an interface in java that contains the following methods: boolean myPush(T element); // pushes element onto the stack; returns true if element successfully pushed, false otherwise boolean myPop(); // removes the top element of the stack; returns true if element successfully popped, false otherwise T myTop(); // if the stack is not empty returns the top element of the stack, otherwise returns null String toString(); // returns contents of the stack as a single String int size(); // return number of elements in the stack then create java file that Implements your interface that you created as a linked stack, then use the driver shown in screen shot to test. below is the code for the LLNode class public class LLNode { protected LLNode link; protected T info; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode link) { this.link = link; } public LLNode getLink() { return link; } } program should output fruits cost = 35
how to create an interface in java that contains the following methods:
boolean myPush(T element); // pushes element onto the stack; returns true if element successfully pushed, false otherwise
boolean myPop(); // removes the top element of the stack; returns true if element successfully popped, false otherwise
T myTop(); // if the stack is not empty returns the top element of the stack, otherwise returns null
String toString(); // returns contents of the stack as a single String
int size(); // return number of elements in the stack
then create java file that Implements your interface that you created as a linked stack, then use the driver shown in screen shot to test. below is the code for the LLNode class
public class LLNode<T> {
protected LLNode<T> link;
protected T info;
public LLNode(T info) {
this.info = info;
link = null;
}
public void setInfo(T info) {
this.info = info;
}
public T getInfo() {
return info;
}
public void setLink(LLNode<T> link) {
this.link = link;
}
public LLNode<T> getLink() {
return link;
}
}
fruits cost = 35
![```java
public class driver {
public static void main(String[] args) {
myLinkedStack<String> a = new myLinkedStack<String>();
int n = 0;
if (a.size() == 0)
n += 5;
if (a.myPush("Apple"))
n += 5;
if (a.toString().equalsIgnoreCase("Apple\n"))
n += 5;
if (a.size() == 1)
n += 5;
if (a.myPush("Orange") && a.size() == 2 && a.toString().equalsIgnoreCase("Orange\nApple\n"))
n += 5;
if (a.myTop().equalsIgnoreCase("Orange"))
n += 5;
while (a.myPop()) {
}
if (a.myTop() == null)
n += 5;
System.out.println("fruits cost =" + n);
}
}
```
### Explanation
This Java program is designed to work with a custom stack implementation called `myLinkedStack`. This class seems to manage a stack of strings, although the implementation details of `myLinkedStack` are not provided. Here's an overview of what the program does:
1. **Initialization:**
- A new `myLinkedStack` object `a` is created to store strings.
- An integer `n` is initialized to 0.
2. **Operations on Stack:**
- Checks if the stack `a` is empty. If true, increments `n` by 5.
- Pushes the string "Apple" onto the stack and increases `n` by 5 if successful.
- Verifies if the stack's string representation is "Apple\n". If so, adds 5 to `n`.
- Ensures the stack size is 1, then increments `n` by 5.
- Attempts to push "Orange" onto the stack and checks if size is 2 with expected string sequence. Adds 5 to `n` if conditions are met.
- Verifies the top of the stack is "Orange", increasing `n` by 5 if true.
3. **Clearing the Stack:**
- Uses a loop to pop all elements from the stack.
4. **Final Check:**
- Checks if the stack is empty with `myTop()`. If empty (`null`](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F1b2c1e36-9974-49eb-8a0c-fe4d3c635f06%2Ff77ad18b-2096-4bc4-a4ec-b4aa5b5739b2%2Foj4vmcc_processed.png&w=3840&q=75)

Step by step
Solved in 2 steps









