2017-08-16 2 views
-7
public class StackSimple{ 
    private long capacity=1000;//maximum size of array 
    private int idx_top; 
    private Object data[]; 

    public StackSimple(int capacity) 
    { 
     idx_top=-1; 
     this.capacity=capacity; 
     data = new Object[capacity]; 
    } 

    public boolean isEmpty(){ 
     return(idx_top<0); 

    } 
    public boolean isFull(){ 
     return(idx_top>=capacity-1); 
    } 

    public int size() 
    { 
     return idx_top+1; 
    } 

    public boolean push(Object x){ 
     if (isFull()){ 
      throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack"); 
} 
     else 
     {`enter code here`data[++idx_top]=x; 
      return true; 
     } 
    } 

    public Object pop(){ 
     if(isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else{ 
      return data[idx_top--]; 
     } 
} 

public Object top(){ 
    if (isEmpty()) 
     throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
    else{ 
     return data[idx_top]; 
    } 
} 

public void print() 
{` 
for (int i=size()-1;i>=0;i--) 
     System.out.println(data[i]); 
} 



} 

public class Stack_Exercise { 
    public static void main(String[] args) { 
     StackSimple s = new StackSimple(capacity:3);//error shows here 
     s.push(x:"books");`enter code here` 
     s.push(x:"something"); 
     s.push(x:"200"); 
     s.print(); 
     System.out.println("Size=" +s.size()); 
    } 
} 

Pourquoi cela ne fonctionne-t-il pas? Pourquoi dit-on une déclaration invalide lors de la création de l'objet StackSimple? Le problème est dans la classe principale lors de son exécution. Il y a des erreurs en poussant les éléments.Quel est le problème avec ce code? Il ne fonctionnera pas

Error while compiling

+2

'nouveau StackSimple (capacité: 3)' ?? Juste 'new StackSimple (3)'. Utilisez-vous IntelliJ par hasard? –

+2

Mettez votre message d'erreur au lieu d'une image. – aristotll

+0

qu'est-ce que cela signifie pour vous?: *** StackSimple (capacité: 3); *** –

Répondre

0

passer des paramètres à une fonction que vous venez de passer les valeurs. Dans votre cas, pas StackSimple(capacity:3) mais juste StackSimple(3)

0

Première question, quelle version de Java utilisez-vous? Deuxièmement, en Java, vous devriez passer en tant que variable au lieu de StackSimple (capacité: 3). Changez votre principale méthode ci-dessous, voici ma recommandation:

StackSimple s = new StackSimple(3); 
    s.push("books"); 
    s.push("something"); 
    s.push("200"); 
    s.print(); 
    System.out.println("Size=" +s.size()); 
+0

J'utilise java 8. Ce code a été fourni par notre conférencier. J'ai essayé d'étudier et de l'exécuter mais je ne pouvais pas. – Rakshya

+0

Merci. Mais ça ne marchera toujours pas. – Rakshya

0

Vous n'êtes pas du tout pousser la valeur dans la pile, votre fonction Pusch ne fonctionne pas comme il devrait fonctionner.

Voici le programme correct.

class StackSimple { 
    private long capacity = 1000;// maximum size of array 
    private int idx_top; 
    private Object data[]; 

    public StackSimple(int capacity) { 
     idx_top = -1; 
     this.capacity = capacity; 
     data = new Object[capacity]; 
    } 

    public boolean isEmpty() { 
     return (idx_top < 0); 

    } 

    public boolean isFull() { 
     return (idx_top >= capacity - 1); 
    } 

    public int size() { 
     return idx_top + 1; 
    } 

    public boolean push(Object x) { 
     if (isFull()) { 
      throw new IllegalArgumentException("ERROR:Stack Overflow.Full Stack"); 
     } else { 
      data[++idx_top] = x; 
      return true; 
     } 
    } 

    public Object pop() { 
     if (isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else { 
      return data[idx_top--]; 
     } 
    } 

    public Object top() { 
     if (isEmpty()) 
      throw new IllegalArgumentException("ERROR:Stack Underflow.Empty Stack."); 
     else { 
      return data[idx_top]; 
     } 
    } 

    public void print() { 
     for (int i = size() - 1; i >= 0; i--) 
      System.out.println(data[i]); 
    } 

} 

public class test { 
    public static void main(String[] args) { 
     StackSimple s = new StackSimple(3);// error shows here 
     s.push("books"); 
     s.push("something"); 
     s.push("200"); 
     s.print(); 
     System.out.println("Size=" + s.size()); 
    } 
}