Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

5 - Skládání objektů versus dědičnost

Stáhněte si archiv se šablonami tříd pro 5. cvičení.

1. úloha na cvičení

  • Implementujte třídu Bag.
    /**
     * Trida Bag reprezentuje kolekci cisel s opakovanim,
     * dvakrat vlozene cislo je tam dvakrat a je ho potreba dvakrat vyjmout.
     */
    class Bag {
        int[] contents = new int[1000];
        int size = 0;
     
        void add(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        boolean contains(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        int indexOf(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        void remove(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
    }
  • Implementuje třídu Set.
    /**
     * Trida Set reprezentuje mnozinu cisel, dvakrat vlozene cislo je v ni jen jednou.
     */
    class Set {
        Bag b = new Bag();
     
        void add(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        boolean contains(int e) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        void remove(int e) {
            b.remove(e);
        }
    }
  • Upravte třídu RedBlueSet. Zredukujte duplikaci kódu, zachovejte funkčnost třídy.
    class RedBlueSet {
        int[] redContents;
        int redCount;
        int[] blueContents;
        int blueCount;
     
        void addRed(int c) {
            redContents[redCount++] = c;
        }
     
        int indexOfRed(int c) {
            for (int i = 0; i < redCount; i++)
                if (redContents[i] == c) return i;
            return -1;
        }
     
        boolean containsRed(int c) {
            return indexOfRed(c) != -1;
        }
     
        void removeRed(int c) {
            int i = indexOfRed(c);
            if (i == -1) return;
            redContents[i] = redContents[--redCount];
        }
     
        void addBlue(int c) {
            if (containsBlue(c)) return;
            blueContents[blueCount++] = c;
        }
     
        int indexOfBlue(int c) {
            for (int i = 0; i < blueCount; i++)
                if (blueContents[i] == c) return i;
            return -1;
        }
     
        boolean containsBlue(int c) {
            return indexOfBlue(c) != -1;
        }
     
        void removeBlue(int c) {
            int i = indexOfBlue(c);
            if (i == -1) return;
            blueContents[i] = blueContents[--blueCount];
        }
    }

2. úloha na cvičení - Proxy a Adapter

  • Implementujte třídu StackImpl.
    class StackImpl {
        int[] contents = new int[10000];
        int size = 0;
     
        StackImpl() {
            System.out.println("Stack instanciovan");
        }
     
        public void push(int i) {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        public int pop() {
            throw new UnsupportedOperationException("naimplementujte!");
        }
     
        public boolean isEmpty() {
            throw new UnsupportedOperationException("naimplementujte!");
        }
    }
  • V metodě main vytvářejte místo zásobníku proxy, které opravdové zásobníky vytvářejí až při prvním použití
    public static void main(String [] args){
        StackImpl[] stacks = new StackImpl[100];
        for (int i = 0; i < 100; i++) stacks[i] = new StackImpl(); 
    }
  • Vytvořte adaptér, který adaptuje StackImpl na WeirdStack
    interface WeirdStack {
        //vlozi na vrchol zasobniku i
        public void push(int i);
     
        //vlozi postupne na vrchol zasobniku vsechny prvky is
        public void push(int[] is);
     
        //vrati hodnotu na vrcholu zasobniku (neodstranuje ji)
        public int top();
     
        //vrati hodnotu na vrcholu zasobniku (odstrani ji)
        public int pop();
     
        //projde zasobnik od vrcholu a vrati prvni zaporny prvek (ten je ze zasobniku odstranen)
        public int popFirstNegativeElement();
     
        //testuje, zda je zasobnik prazdny
        public boolean isEmpty();
    }
courses/b6b36omo/labs/lab05.txt · Last modified: 2018/10/04 10:02 by sebekji1