Answered step by step
Verified Expert Solution
Link Copied!

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, p, 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 {"ab","cd","ae","bd"} 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 "Ex1" on your lab4 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 (Last-In-First-Out) 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 push(String 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.
if(element == null) throw new Exception();
if(false == items.contains(element))
items.add(element);
}
public String pop() throws Exception {
// MODIFIES: this
// EFFECTS: Removes an element from the top of the stack
if (items.size()==0) throw new Exception();
return items.remove(items.size()-1);
}
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
}
}
image text in transcribed

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

Step: 3

blur-text-image

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

Sql++ For Sql Users A Tutorial

Authors: Don Chamberlin

1st Edition

0692184503, 978-0692184509

More Books

Students also viewed these Databases questions

Question

Why does sin 2x + cos2x =1 ?

Answered: 1 week ago

Question

What are DNA and RNA and what is the difference between them?

Answered: 1 week ago

Question

Why do living creatures die? Can it be proved that they are reborn?

Answered: 1 week ago

Question

How would we like to see ourselves?

Answered: 1 week ago