Question

Abstract Data Types (54 points, 6bonus points) In computer science, an abstract data type (ADT) is a mathematical model for data types where a data type is defined by its behavior (semantics) from the point of view of a user of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. (Source: Abstract Data Types. Wikipedia. Wikimedia Foundation, 2 Nov. 2016. Web. 9 Nov 2016. < https://en.wikipedia.org/wiki/Abstract data type >.) A stack is an example of an abstract data type (ADT) A. Create a Power Point Presentation (>= 6 slides) Research the basic concept of a stack and explain it in your presentation. Illustrate the concept with two reallife examples. Cite your sources Implementation of the ADT Implement the stack ADT in a Stack class. The stack will hold integers (ints). For the implementation, use a fixed size array (not the ArrayList class). Implement the following methods 1. the no-arg constructor initializes a stack with a default capacity of 4, i.e. the stack can take4 elements at most), 2. a constructor that initializes a stack with a specific capacity (given as parameter), 3. push (adds a new element to the stack if it is not full), 4. pop (removes the last element from a stack if it is not empty), 5. peek (if the stack is not empty, returns the value of the last element from a stack, but does not remove the element from the stack), 6. getSize (returns how many elements are currently in the stack), 7. getCapacity (returns the capacity of the stack), 8. isFull (returns true if the stack is full), 9. isEmpty (returns true if the stack is empty) 10. toString (returns a string of all the elements) Optional (for bonus points) 11. equals (compares the stack with another stack given as parameter and returns true if both are of equal capacity and have the same elements in the same positions), 12. a constructor that copies another stack given as parameter, B. Design the Stack class using a UML diagram. C. Implement the Stack class in Java D. Test all the methods of your ADT in a separate demo/ test progranm It is recommended to test each method right after its implementation (to make sure it is working correctly), before moving on to the next one

Part A is done, I do not know how to start these steps..

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the solution:

I have covered the bonus section also:

//StackClass.java

import java.util.NoSuchElementException;

public class StackClass {

//instance variables

int topElement,capacity,currentSize;

int stackArray[] = new int[capacity];

//constructor that takes capacity

public StackClass(int capacity)

{

this.capacity=capacity;

topElement=-1;

currentSize=0;

stackArray = new int[capacity];

}

//constructor without parameter

public StackClass()

{

this.capacity=4;

topElement=-1;

currentSize=0;

stackArray = new int[capacity];

}

//push function

public void push(int n)

{

if(isFull())

throw new StackOverflowError("Stack already full");

stackArray[currentSize]=n;

topElement++;

currentSize++;

}

//empty function

public boolean isEmpty()

{

return currentSize==0;

}

public int pop()

{

if(isEmpty())

throw new NoSuchElementException("Stack under flow");

int n= stackArray[topElement];

topElement--;

currentSize--;

return n;

}

//it will return the top element

public int peek()

{

if(isEmpty())

throw new NoSuchElementException("Stack under flow");

return stackArray[topElement];

}

//it will return the capacity

public int getCapacity()

{

return this.capacity;

}

//it will return the size

public int getSize()

{

return this.currentSize;

}

//check if array is full or not

public boolean isFull()

{

return currentSize==capacity;

}

//toString function

public String toString()

{

StringBuffer s = new StringBuffer();

if(isEmpty())

s.append("Empty Stack\n");

else{

int j=topElement;

for(j=topElement;j>=0;j--)

{

s.append(stackArray[j]+"\n");

}

}

return "Current elements in Stack \n"+s;

}

//this is the bonus section

//i have implemeneted

//check for two stack eqaulity

public boolean equals(StackClass anotherStack)

{

boolean flag = true;

if(this.getCapacity()!=anotherStack.getCapacity())

return false;

if(this.getSize()!=anotherStack.getSize())

return false;

int index=0;

int t1[]=new int[this.getSize()];

int t2[]=new int[anotherStack.getSize()];

while(!this.isEmpty())

{

t1[index]=this.pop();

t2[index]=anotherStack.pop();

index++;

}

for(int i=0;i<t1.length;i++)

{

if(t1[i]!=t2[i])

{

flag=false;

break;

}

}

for(int l=t1.length-1;l>=0;l--)

{

this.push(t1[l]);

anotherStack.push(t2[l]);

}

return flag;

}

//copy the stack to another stack

public StackClass(StackClass another)

{

this.stackArray=new int[another.getCapacity()];

this.topElement=-1;

this.currentSize=0;

this.capacity=another.capacity;

int a[] = new int[stackArray.length];

for(int i=0;i<a.length;i++)

{

a[i]=another.pop();

}

for(int j=a.length-1;j>=0;j--)

{

this.push(a[j]);

another.push(a[j]);

}

}

}

//TestStack.java

public class TestStack {

public static void main(String[] args) {

//created a stack

StackClass stack = new StackClass();

//pushed some values

stack.push(34);

stack.push(23);

stack.push(90);

stack.push(12);

//created one more stack by using constructor

StackClass another = new StackClass(stack);

//checking for equality of

//it should return true

//because another is created using copy constructor

System.out.println("Is stack equal to another stack : "+stack.equals(another));

//getting the peek element

System.out.println("Stack peek element : "+stack.peek());

System.out.println("another Stack peek element : "+another.peek());

System.out.println("Is stack full : "+ stack.isFull());

System.out.println("Removing the top element from stack : "+stack.pop());

System.out.println("Checking the top element after removing : "+ stack.peek());

System.out.println("Capacity of stack : "+another.getCapacity());

//it will call toString method:

System.out.println(stack);

}

}

OUTPUT:

Is stack equal to another stack: true Stack peek element: 12 another Stack peek element12 Is stack full true Removing the top

UML diagram:

Stack clas tor lcment t MLー 0 Shack Sn s Smphy: boolean isfu)boslcan

Please do let me know if u have any doubts...

Add a comment
Know the answer?
Add Answer to:
Part A is done, I do not know how to start these steps.. "In computer science,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we...

    PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we will implement the DrippingStack class. This is a fixed capacity stack; it's size does not increase or decrease automatically. Capacity of this DrippingStack will be determined during initialization. When push is invoked with the DrippingStack at full capacity, rather than throwing a FullStack exception, a more typical semantic is to accept the pushed element at the top while dripping the oldest element from the...

  • Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DE...

    Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using  a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array...

    JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...

  • can i please get help on this? i don't know how to do it and I'm...

    can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...

  • In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These...

    In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT