Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please Solve Whole program with completed code Abstract Concept of a stack of distinct strings: A stack of distinct strings, p , is a collection
Please Solve Whole program with completed code
Abstract Concept of a stack of distinct strings:
A stack of distinct strings, is a collection of strings with a top where a string cannot exist more than
once in the collection. For example, the collection of strings abcdaebd is a stack with top
bd The only operations supported are the addition of a string at the top, known as push, and the
deletion of a string from the top, known as pop.
Implementation of a stack of distinct strings:
The following class, StackOfDistinctstrings, represents a stack of distinct strings.
Click on Project New Project in the Netbeans program and save it as Ex on your lab directory.
Create a new class called "StackOfDistinctStrings" and copy the code in it For this class:
a Write the abstraction function in the Overview clause.
b Write the rep invariant in the Overview clause.
b Fill in the body of the method repOK
b Fill in the body of the method tostring
import java.util.ArrayList;
public class StackOfDistinctStrings
Overview: StacksOfDistinctStrings are mutable, bounded
collection of distinct strings that operate in
LIFO LastInFirstOut order.
The abstraction function is:
a Write the abstraction function here
The rep invariant is:
b Write the rep invariant here
the rep
private ArrayList items;
constructor
public StackOfDistinctStrings
EFFECTS: Creates a new StackOfDistinctStrings object
items new ArrayList;
public void pushString element throws Exception
MODIFIES: this
EFFECTS: Appends the element at the top of the stack
if the element is not in the stack, otherwise
does nothing.
ifelement null throw new Exception;
iffalse items.containselement
items.addelement;
public String pop throws Exception
MODIFIES: this
EFFECTS: Removes an element from the top of the stack
if itemssize throw new Exception;
return items.removeitemssize;
public boolean repOK
EFFECTS: Returns true if the rep invariant holds for this
object; otherwise returns false
c Write the code for the repOK here
public String toString
EFFECTS: Returns a string that contains the strings in the
stack and the top element. Implements the
abstraction function.
d Write the code for the toString here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started