Assume the following abstract data types: Stack and Bag You are required to: a)
ID: 3872095 • Letter: A
Question
Assume the following abstract data types: Stack and Bag
You are required to:
a) Implement these abstract data types as two separate classes without inheritance between them
b) Create a client class, MyClientTest1, and provide methods to test each one of the methods implemented by Stack and Bag
c) Now, re-factor your program to use inheritance. Decide what class, e.g., Stack or Bag, should be the super-class. Provide a rationale of your design decision
d) Create a client class, MyClientTest2, and provide methods to test each one of the methods implemented by Stack and Bag
the Stack class
State: a sequence of Objects s
isEmpty() : boolean
Precondition: true
Postcondition: returns true if and only if s is empty
take() : Object
Precondition: ! isEmpty()
Postcondition: result is the first item of s. The final value of s is the initial value with the first item removed.
put( ob : Object )
Precondition: true
Postcondition: The final value of s is its initial value with ob prepended.
the Bag class
State: a bag (i.e. multiset) of Objects b
isEmpty() : boolean
Precondition: true
Postcondition: Returns true if and only if b is empty.
take() : Object
Precondition: ! isEmpty()
Postcondition: result is an arbitrary member of b. The final value of b is the initial value with the result removed.
put( ob : Object )
Precondition: true
Postcondition: The final value of b is its initial value with ob
added.
i need to know how to do this.
Explanation / Answer
/* Java program to implement basic stack
operations */
class Stack
{
static final int MAX = 1000;
int top;
int a[] = new int[MAX]; // Maximum size of Stack
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean put(int x)
{
if (top >= MAX)
{
System.out.println("Stack Overflow");
return false;
}
else
{
a[++top] = x;
return true;
}
}
int take()
{
if (top < 0)
{
System.out.println("Stack Underflow");
return 0;
}
else
{
int x = a[top--];
return x;
}
}
}
*java file to implement bag*/
public class Bag<T>
{
//Initialise instantiate hashmap to implement bag
private HashMap<T,Integer> map;
public Bag(){
//Initialise the hasmap in the no-args constructor
//this keyword refers to the current class we are in
this.map = new HashMap<T,Integer>();
}
if(map.isEmpty())
{ //Return null if it is
return null;
}
/****
* Method to add item x of type T to Bag
*****/
public void add(T x)
{ //Check if the map contains this item already
if(map.containsKey(x))
{ //If it does see how many times this element occurs
int numElem = map.get(x);
//Increment by one the number of times this element occurs
//i.e. add another of these elements to the bag
map.put(x, (numElem+1));
}
else
{ //If the element does not exist create a new item in the map
//with the number of items set to one
map.put(x, 1);
}
}
//MyClientTest1
import java.uti.*;
public class MyClientTest1 {
public static void main(String args[])
{
Stack s = new Stack();
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop() + " Popped from stack");
Bag<String> bagTest = new Bag<String>();
bagTest.add("Hello");
bagTest.add("Hello");
bagTest.add("Hello");
}
}
/*A bag is a collection of objects, where you can keep adding objects to the bag, but you cannot remove them once added to the bag.
So with a bag data structure, you can collect all the objects, and then iterate through them. ThereforeBag must be super class
followed by Stack as a derived class while implementing inheritance*/
//MyClientTest2
public class MyClientTest1 {
public static void main(String args[])
{
Stack s = new Stack();
s.put(10);
s.put(20);
s.put(30);
System.out.println(s.pop() + " Popped from stack");
Bag<String> bagTest = new Bag<String>();
bagTest.add("Hello");
bagTest.add("Hello");
bagTest.add("Hello");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.