Archive

Posts Tagged ‘demo’

MY EXCEPTION – Java Program

July 4, 2011 1 comment

INTERFACE – Java Program

July 4, 2011 3 comments

Question: [INTERFACE]
A stack is a container that stores objects in a manner indicated by its nameā€”in a vertical stack where only the object at the top of the stack is accessible. It works rather like a sprung stack of plates in a cafeteria. Only the top plate is at counter level and, therefore, is the only one you can access. When you add a plate to the stack, the existing plates are pushed down so the new plate is now the one that you can access. Define an interface IStack and implement it in class Stack with a method push() that adds the object that is passed as an argument to the top of the stack, and with a method pop() that removes and returns the object that is currently at the top of the stack. The pop() method should return null when the stack is empty. Demonstrate the operation of your Stack implementation by storing and retrieving 10 objects in stacks of a suitable type. Implement and demonstrate a listAll() method in the Stack class definition that will list the objects in the stack.

Answer

/**
* FileName: InterfaceDemo.java
* @author (Sandeep Negi)
* MCA 2nd Year
* Java Programming Lab-406
*/

import java.util.Scanner;

// Define an integer stack interface.
interface IStack {
void push(int item); // store an item
int pop(); // retrieve an item
}

// Implement a "growable" stack.
class DynStack implements IStack {
private int stck[];
private int tos;
// allocate and initialize stack
DynStack(int size) {
stck = new int[size];
tos = -1;
}

// Push an item onto the stack
public void push(int item) {
// if stack is full, allocate a larger stack
if(tos==stck.length-1) {
int temp[] = new int[stck.length * 2]; // double size
for(int i=0; i<stck.length; i++)
temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
}else
stck[++tos] = item;
}

// Pop an item from the stack
public int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}else
return stck[tos--];
}

// List all items of stack
public void listAll() {
int i=tos;
System.out.println("\nItems in stack-->");
while(i>=0){
System.out.println(stck[i]);
i--;
}
}

}

class InterfaceDemo {
public static void main(String args[]) {
DynStack mystack = new DynStack(5);
Scanner sc = new Scanner(System.in);
char ch='y';
int operation,tempNumber;

while(ch == 'y'){
System.out.println("\nMenu");
System.out.println("=====");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.List All");
System.out.println("0.Exit");
System.out.print("Enter ur choice :");
operation = sc.nextInt();

switch(operation){
case 1:System.out.print("Enter item value :");
tempNumber = sc.nextInt();
mystack.push(tempNumber);
break;

case 2:System.out.println(mystack.pop());
break;

case 3:mystack.listAll();
break;

case 0: System.exit(0);
break;

default: System.out.println("Invalid input...choose again.");

}
}
}
}
Categories: JAVA, Java SE Codes Tags: , , ,

PACKAGE – Java Program

Categories: JAVA, Java SE Codes Tags: , , ,

DATE – Java Program

July 4, 2011 1 comment
Categories: JAVA, Java SE Codes Tags: , , ,