Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Aufgabe1 {
- int[] zahlen;
- int currentIndex;
- Aufgabe1(int max) {
- this.zahlen = new int[max];
- this.currentIndex = 0;
- }
- boolean isEmpty() {
- return this.currentIndex == 0;
- }
- boolean isFull() {
- return this.currentIndex == this.zahlen.length;
- }
- void put(int wert) {
- if (!this.isFull()) {
- this.zahlen[this.currentIndex] = wert;
- this.currentIndex++;
- }
- }
- int get() {
- if (!this.isEmpty()) {
- int erg = this.zahlen[0];
- for (int i = 0; i < this.currentIndex - 1; i++) {
- this.zahlen[i] = this.zahlen[i + 1];
- }
- this.currentIndex--;
- return erg;
- }
- return 0; // Fehlerfall: spaeter mit Exceptions zu behandeln
- }
- void push(int wert) {
- this.zahlen[++this.currentIndex] = wert;
- }
- int pop() {
- return this.zahlen[this.currentIndex--];
- }
- int first() {
- return this.zahlen[this.currentIndex];
- }
- int last() {
- return this.zahlen[this.currentIndex = 1];
- }
- //Testprogramm
- public static void main(String[] args) {
- Aufgabe1 schlange = new Aufgabe1(10);
- int pushput = IO.readInt("Push (1) oder Put(0)? " );
- for (int i = 0; i < 4; i++) {
- if (pushput == 0){
- schlange.put(IO.readInt());
- }else {
- schlange.push(IO.readInt());
- }
- }
- System.out.println("Erste Zahl: " + schlange.first());
- System.out.println("Letzte Zahl: " + schlange.last());
- int getpop = IO.readInt("pop (1) oder get(0)? " );
- while (!schlange.isEmpty()) {
- if (getpop == 0){
- System.out.println(schlange.get());
- } else {
- System.out.println(schlange.pop());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment