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

By | April 21, 2017

PROGRAM STATEMENT

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

CONCEPT

[alert-announce]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 behaviors/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 physical existence of a logical template class. A class is declared using 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.[/alert-announce]

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 behavior. 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 integer or float. Each time you create an object, a new set of instance variables comes into existence which defines the characteristics of that object. If you want to create an object of the class and have the reference variable associated with this object, 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. [alert-success]In the following statement, obj is instance/object of 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 a 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();
}
[/alert-success]

Constructor

[alert-announce] 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 that 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 have no parameter is known as default constructor.

Student()
{
//block of code
}

Object creation:

Student obj=new Student();

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");
[/alert-announce]

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

[alert-success]
JAVA Stack operations - Push, Pop, display

Output of JAVA Stack operations – Push, Pop, display

[/alert-success]
Author: rabins

Rabins is CEO of R.S. Softwares Private Limited, a software and IT company in Nepal. He runs Nepal's first Internet of Things (IoT) website. He is the owner of RabinsXP who is constantly working for increasing IoT in Nepal. He also builds Android, iOS and Web Apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. RabinsXP is the internet nickname of Rabins Sharma Lamichhane too.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.