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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;

}

}

program should output 

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`
Transcribed Image Text:```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`
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Avoiding deadlock
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education