Anda di halaman 1dari 5

/*Presented by: Mary Roxanie Kay L. Rafer Randolf Macahilas Ae3aa September 14, 2011*/ import java.io.

*; public class TestStack{ public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStream(System.in())); int option; Stack_class stack = new Stack_class(); option = Integer.parseInt(System.out.println("--OPTIONS--" + "\n [1] Push" + "\n [2] Pop" + "\n [3] Top" + "\n [4] isEmpty" + "\n [5] Exit")); do{ switch(option){ case 1: stack.push(br.readLine()); break; case 2: stack.pop(); break; case 3: stack.top(); break; case 4: stack.empty(); break; case 5: System.exit(0); break; default: System.out.println("Invalid option!"); break; } }while(option != 0); }

} class Stack_class{ private int[] stack_ref; private int max_len, top_index; public stack_class(){ stack_ref = new int[100]; max_len = 99; top_index = -1; } public void push(int number){ if(top_index==max_len){ System.out.println("Error in push. Stack is full."); }else{ stack_ref[++top_index] = number; } } public void pop(){ if(top_index==-1){ System.out.println("Error in pop. Stack is empty."); }else{ --top_index; } } public void top(){stack_ref[top_index];} public boolean empty(){return(top_index==-1);} }

import java.util.io.*; import java.io.*; class Stack_class{

/* A library named java.util.* was called through the command*/ /* A library named java.io.* was called through the command*/ /* A subclass was created named Stack_class */

private int[] stack_ref;

/* An array was declared in private with data type integers*/

private int max_len, top_index; /* Another variable were declared in private with data type integers */ public stack_class(){ /*A constructor was creatednamed Stack_class in public*./

stack_ref=new int[100]; /* The array Stack_ref was assigned with 100 locations/containers in a memory for new integers*/ max_len=99; /*The integer variable max_len was assigned within a maximum value for a stack based on Stack_ref which is 100*/ /*Another integer variable was assigned with the minimum value for a stack based on stack_ref which is -1*/

top_index=-1;

} public void push(int number){ /* A method was created in public named push with a parameter number having a data type of integer*/ if(top_index==max_len) { /*A conditional statement comparing top_index to max_len*/

System.out.println(Error in push. Stack is full); /* An output Error in push. Stack is full will be dispayed if the top_index is equal to max_len*/ }else{ stack_ref[++top_index]=number, /* Another conditional statement which will be done if the previous conditional statement if statement was not met. Also the top_index will increased by one and the stack_ref with an increased top_index will be equal to the number which is the parameter called by the method. A } stack value was added.*/ /*Another method was created in public named pop*/ /* A conditional statement company the top_index to a value which is -1*/

Public void pop(){ if(top_index==-1)

System.out.println(Error in pop_Stack is empty; /* A conditional statement was returned . Error in pop.Stack is empty will be displayed*/ }else{ --top_index; /* Another conditional statement which will be done if the statement was not met. The top_index will decrease by one meaning a stack was deleted.*/

} )

public in top(){stack_ref[top_index];} /* A return type of method that returns an integer value back to the subprogram caller which corresponds to Stack_ref[top_index]*/ public boolean empty(){return(top_index==-1);} /* The top value of the stack was returned*/

NOTE: { } ->>BLOCKS OF STATEMENTS BOUNDED BY CURLY BRACES

RECITATION
IN

COS7
SUBMITTED BY:
Mary Roxanie Kay L. Rafer Randolf M. Macahilas

SUBMITTED TO: Ms. Shiela Bocabal COS7 Instructor


September14, 2011

Anda mungkin juga menyukai