Stack using arrays / Push(), Pop(), and Display() methods in JAVA Programming Language

PROGRAM STATEMENT

Write a Java program to implement the Stack using arrays. Write Push(), Pop(), and Display() methods to demonstrate their working.

CONCEPT

In Java everything is encapsulated under classes. The class is the core of Java language. The class can be defined as a template/ blueprint that describes the behaviours/states of a particular entity. A class defines the new data type. This type can be used to create an object of that type. An object is an instance of the class. You may also call it as the physical existence of a logical template class. A class is declared using the class keyword. A class contains both data and code that operate on that data. The data or variables defined within a class are called instance variables and the code that operates on this data is known as methods.

A simple class example:

class Student{
String USN,name , branch;
int phoneno;
}

Object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behaviour. An object reference provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables. Creating variables of your class type is similar to creating variables of primitive data types, such as integers or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. Suppose you want to create an object of the class and have the reference variable associated with this object. In that case, you must also allocate memory for the object by using the new operator. This process is called instantiating an object or creating an object instance.

In the following statement, obj is the instance/object of the Student class.

Student obj=new Student();

An array of objects is created just like an array of primitive-type data items in the following way.

Student[] obj_Array = new Student[7];

However, in this particular case, we may use a for loop since all Student objects are created with the same default constructor.

for ( int i=0; i<obj_Array.length; i++) {
obj_Array[i]=new Student();
}

Constructor

The Constructor in java is a special type of method that is used to initialize the object. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object which is why it is known as the Constructor.

Types of java constructors

There are two types of constructors:

  • Default constructor (no-arg constructor)
  • Parameterized constructor

A constructor that has no parameter is known as the default constructor.

Student()
{
//block of code
}

Object creation:

Student obj=new Student();

A constructor with arguments is known as parameterized constructor.

Student(int i,String n){
id = i;
name = n;
}

Object creation:

Student4 s1 = new Student4(350,"Rabins");

EXAMPLE STACK OPERATIONS PROGRAM

/*stack.java*/
import java.io.*;
import java.util.Scanner;
public class stack
{ int element,maxsize,top;
int[] st;
public stack()
{ Scanner integer=new Scanner(System.in);
System.out.println("Enter stack size");
maxsize=integer.nextInt();
st=new int[maxsize];
top=-1;
}
public void push(int element)
{ /*if(top>=maxsize)
{ System.out.println("Overflow!!");
//return(0);
}*/
try
{ st[++top]=element;
}
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println(e);
}
}
public int pop()
{ if(top==-1)
{ System.out.println("UnderFlow");
return(-1);
}
return(st[top--]);
}
public void display(int[] st, int max_size)
{ int i;
System.out.println("Stack Elements:");
for(i=0;i<=max_size;i++)
System.out.println(st[i]);
}
}
/*myStack.java*/
import java.io.*;
import java.util.Scanner;
public class myStack
{ static int option;
public static void main(String[] args)
{ stack obj=new stack();
while(true)
{ System.out.println("\nEnter yours choice\n1.PUSH\n2.POP\n3.Display\n4..EXIT");
Scanner integer=new Scanner(System.in);
option=integer.nextInt();
switch(option)
{ case 1: System.out.println("Enter Element");
obj.element=integer.nextInt();
obj.push(obj.element);
break;
case 2:System.out.printf("Poped element is %d",obj.pop());
break;
case 3:obj.display(obj.st,obj.top);
break;
case 4:System.exit(0);
default:System.out.println("Wrong option");
}
}
}
}

OUTPUT

JAVA Stack operations - Push, Pop, display

The output of JAVA Stack operations – Push, Pop, display

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is senior ICT professional who talks about #it, #cloud, #servers, #software, and #innovation. Rabins is also the first initiator of Digital Nepal. Facebook: rabinsxp Instagram: rabinsxp

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *