Question

In: Computer Science

Java programming! Implement a static stack class of char. Your class should include two constructors. One...

  1. Java programming!
  2. Implement a static stack class of char. Your class should include
  • two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack
  • a push and pop operator
  • an isEmpty and isFull method . Both return Booleans indicating the status of the stack

Change this cods according to instructions please:

public class Stack {

int stackPtr;

int data[];

public Stack() //constructor

{

stackPtr=0;

data=new int[10];

}

public Stack(int sz) //constructor

{

stackPtr=0;

data=new int[sz];

}

public void push(int val)

{

data[stackPtr]=val;

stackPtr++;

}

public int pop()

{

stackPtr--;

//int retVal;

return(data[stackPtr]);

}

public boolean isEmpty()

{

return(stackPtr==0);

}

public boolean isFull()

{

return(stackPtr==data.length);

}

}

Solutions

Expert Solution

Hey mate, before implementing stack as an array there are some few concepts need to be known-

1) Stack is a linear data structure which follows a particular order in which the operations are performed. The order is Last in First out (LIFO), that means the last element inserted will be the first element to be deleted or pop out.

2) The stackPtr or top of stack will always points to -1 value which represents the stack is empty. And it doesn't points to 0 for empty condition because in array concept 0th index stores the first value and therefore, stack implemented as an array will have first element at 0th position.

3) The insertion (push) of elements in stack as an array will start from 0th index and deletion (pop) will take place from last index of array (i.e. (size of array -1) position).

CLASS IMPLEMENTATION -

public class Stack {

int stackPtr=-1;
int data[ ];

public Stack() /* no-argument constructor*/
{
data=new int[10];
}

public Stack(int sz) /* parameterized constructor*/
{
data=new int[sz];
}

public void push(int val)
{
if(stackPtr>=(data.length-1))
{
System.out.println("Stack is full, cannot push more elements");
}

else
{
data[++stackPtr]=val;
}

}

public int pop()
{
if(stackPtr==-1)
{
System.out.println("Stack is empty, cannot pop any element");
}

else
{
return(data[stackPtr--]);
/* returning deleted value*/
}
return 0;
}

public boolean isEmpty()
{
return (stackPtr==-1);
/* return true if stack isEmpty else return false*/
}

public boolean isFull()
{
return (stackPtr==(data.length-1));
/* return true if stack isFull else return false*/
}
}

TESTING CODE -

class Main /* driver code to test our implementation*/
{
public static void main(String[] args)
{
Stack s=new Stack();
/* this will call no-argument constructor as no parameter is passed*/
System.out.println("Stack is full :"+ s.isFull()); /* checking stack status in the beginning*/
System.out.println("Stack is empty :"+ s.isEmpty());
s.push(1);
/* adding elements in stack*/
s.push(10);
s.push(4);
s.push(6);
s.push(14);
s.push(7);
s.push(23);
s.push(11);
s.push(98);
s.push(16);
s.push(132);
/* this value will not be added as the size of stack is 10 and stack is full
and the message present in if condition inside push() will be displayed*/

System.out.println("Stack is full :"+ s.isFull()); /* stack status after adding elements*/
System.out.println("Popped element :"+ s.pop());
System.out.println("Popped element :"+ s.pop());
System.out.println("Stack is full :"+ s.isFull());
/* stack status after popping out some elements*/
System.out.println("Stack is empty :"+ s.isEmpty());
}
}

OUTPUT -

Stack is full :false
Stack is empty :true
Stack is full, cannot push more elements   
Stack is full :true
Popped element :16
Popped element :98
Stack is full :false
Stack is empty :false




Related Solutions

In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants...
JAVA programming- answer prompts as apart of one java assignment Static static variables static methods constants Create a class Die representing a die to roll randomly. ☑ Give Die a public final static int FACES, representing how many faces all dice will have for this run of the program. ☑ In a static block, choose a value for FACES that is a randomly chosen from the options 4, 6, 8, 10, 12, and 20. ☑ Give Die an instance variable...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided...
Overloaded Constructors Add a pair of constructors to the Height class that implement the initializations provided by the two setHeight operations in Figure 7.11. Minimize the total number of statements by having the one parameter constructor call the one-parameter setHeight method and having the two-parameter constructor call the two-parameter setHeight method. Provide a complete rewritten main method for the HeightDriver class such that the new method uses one of the new constructors from part a) to generate this output: 6.0...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should have a private linked list member, and utilize the linked list methods to implement its functionality. The stack should include a constructor, a destructor, a push, a pop, and an isEmpty method (which returns a bool). c. Implement, QueueFromList, a templated queue class backed by the above singlylinked list. The queue should have a private linked list member, and utilize the linked list methods...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
in Java language, in most simple algorithm Using a stack class, write a static method called...
in Java language, in most simple algorithm Using a stack class, write a static method called parse that parses a String for balanced parentheses. we seek only to determine that the symbol ‘{‘ is balanced with ‘}’. parse accepts a single String parameter and returns an int. If parse returns a minus 1, then there are no errors, otherwise, parse should return the position within the String where an error occurred. For example parse(“{3 + {4/2} }”)   would return -1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT