سلام !
اولین برنامه ای که می خوام اینجا بذارم یک پشته به زبان جاوا با استفاده از اینترفیسه و با استفاده از یک استک آرایه ای از اون استفاده کردم و در نهایت اجرای آن .
نکته : این برنامه به طور کلی از اپلت استفاده نمی کند (فقط برای نشان دادن قدرت آن و همچنین زیبایی هرچند اندک برنامه)و فقط برای گرفتن عدد مورد استفاده قرار گرفته .
نکته 2:هر فایل public در فایل جداگانه و با نام آن کلاس ذخیره شود .(اگر در محیط netbeans اجرا شود که چه بهتر):
this is a java programm that creat a Generic Stack (In Array Model).
package Data;
import java.io.IOException;
/**
*
* @author eprogrammer
*/
public interface Stack {
public void push(Object x);
public Object pop() throws UnderFlow;
public boolean isEmpty();
public void MakeEmpty();
}
//————————————–
/*
* ArrayStack.java
*
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.lang.*;
/**
*
* @author eprogrammer
*/
public class ArrayStack implements Stack {
private int capacity=100;
private Object []data;
private int topindex=0;
private Object []data2;
/** Creates a new instance of ArrayStack */
public ArrayStack(int capacity) {
this.capacity=capacity;
data = new Object[capacity+1];
}
public ArrayStack(){
this(101);
}
public void push(Object x){
/* if(topindex==this.capacity-1)
{
this.capacity*=2;
data2=new Object[this.capacity];
for(int i=0;i<topindex;i++)
data2[i]=data[i];
data=data2;
} */
try{
data[topindex++]=x;
}catch(ArrayIndexOutOfBoundsException ex){
System.out.println(“Out of boounds”);
}
}
public Object pop()throws UnderFlow{
if(this.isEmpty())
throw new UnderFlow(“The stack is empty”);
else
data[topindex--]=null;
return data[topindex+1];
}
public void MakeEmpty(){
topindex=-1;
}
public Object rtop(){
return data[topindex];
}
public Object retrieve(int i){
return data[i];
}
public boolean isEmpty(){
if(topindex==-1)
return true;
else
return false;
}
public int returnsize(){
return topindex;
}
public static void main(String[] args)
{
// Scanner in=new Scanner(System.in);
ArrayStack mystack=new ArrayStack();
int num=0;
for(;num==0;)
{
try{
num=Integer.parseInt(JOptionPane.showInputDialog(null,”Enter size of Stack “));
}catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null,”Must Enter a Number !”);
}
}
for(int i=0;i<num;i++)
{
String str=JOptionPane.showInputDialog(“Enter “+(i+1) + “th Object : “);
try{
mystack.push(new String(str));
}catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println(“Out Of Bound”);
}
}
String str = null;
try {
str=(String) mystack.pop();
} catch (UnderFlow ex) {
ex.printStackTrace();
}catch(ArrayIndexOutOfBoundsException ex)
{
JOptionPane.showMessageDialog(null,”Out of bounds “);
}
if (!mystack.isEmpty())
JOptionPane.showConfirmDialog(null,str+” Poped”) ;
for(int i=0;i<mystack.returnsize();i++)
System.out.println(mystack.retrieve(i));
}
}
class UnderFlow extends IOException{
/** Creates a new instance of UnderFlow */
public UnderFlow() {
}
public UnderFlow(String message)
{
super(message);
}
}