Guest User

Untitled

a guest
May 20th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.  
  2. class Aufgabe1 {
  3.  
  4.     int[] zahlen;
  5.     int currentIndex;
  6.    
  7.     Aufgabe1(int max) {
  8.         this.zahlen = new int[max];
  9.         this.currentIndex = 0;
  10.     }
  11.  
  12.     boolean isEmpty() {
  13.         return this.currentIndex == 0;
  14.     }
  15.  
  16.     boolean isFull() {
  17.         return this.currentIndex == this.zahlen.length;
  18.     }
  19.  
  20.     void put(int wert) {
  21.         if (!this.isFull()) {
  22.             this.zahlen[this.currentIndex] = wert;
  23.             this.currentIndex++;
  24.         }
  25.     }
  26.  
  27.     int get() {
  28.         if (!this.isEmpty()) {
  29.             int erg = this.zahlen[0];
  30.             for (int i = 0; i < this.currentIndex - 1; i++) {
  31.                 this.zahlen[i] = this.zahlen[i + 1];
  32.             }
  33.             this.currentIndex--;
  34.             return erg;
  35.         }
  36.         return 0; // Fehlerfall: spaeter mit Exceptions zu behandeln
  37.     }
  38.  
  39.    
  40.    
  41.     void push(int wert) {
  42.         this.zahlen[++this.currentIndex] = wert;
  43.        
  44.     }
  45.    
  46.     int pop() {
  47.         return this.zahlen[this.currentIndex--];
  48.     }
  49.        
  50.     int first() {
  51.         return this.zahlen[this.currentIndex];
  52.        
  53.         }
  54.     int last() {
  55.         return this.zahlen[this.currentIndex = 1];
  56.     }
  57.    
  58.    
  59.  
  60.  
  61. //Testprogramm
  62.     public static void main(String[] args) {
  63.         Aufgabe1 schlange = new Aufgabe1(10);
  64.         int pushput = IO.readInt("Push (1) oder Put(0)? " );
  65.         for (int i = 0; i < 4; i++) {
  66.             if (pushput == 0){
  67.             schlange.put(IO.readInt());
  68.             }else {
  69.             schlange.push(IO.readInt());
  70.             }
  71.         }
  72.         System.out.println("Erste Zahl: " + schlange.first());
  73.         System.out.println("Letzte Zahl: " + schlange.last());
  74.         int getpop = IO.readInt("pop (1) oder get(0)? " );
  75.         while (!schlange.isEmpty()) {
  76.             if (getpop == 0){
  77.             System.out.println(schlange.get());
  78.             } else {
  79.             System.out.println(schlange.pop());
  80.             }
  81.         }
  82.  
  83.     }
  84. }
Add Comment
Please, Sign In to add comment