Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.6 kB
5
Indexable
Never
// Modify this class to hold different types, instead of only int. Don't copy!
class HoldOne {
    protected int held = 0;

    public int put(int item) {
        int previouslyHeld = held;
        held = item;
        return previouslyHeld;
    }
}

class HoldMany {
    protected static final int MAX_ITEMS = 3;
    protected int heldCount = 0;
    protected int[] held = new int[MAX_ITEMS];
    
    /**
     * If there is space for the item, it will be added to the end of the list.
     * If not, the first item in the list will be removed, everything else will be shifted towards the beginning, and the new item will be added to the end.
     * 
     * @returns 0 if there was room for the item. Otherwise, returns the removed first item.
     */
    public int put(int item) {
        int previouslyHeld = 0;
        if (heldCount == MAX_ITEMS) {
            previouslyHeld = held[0];
            
            for (int i = 1; i < MAX_ITEMS; i++) {
                held[i-1] = held[i];
            }
            held[MAX_ITEMS-1] = item;
        }
        else {
            held[heldCount] = item;
            ++heldCount;
        }
        
        return previouslyHeld;
    }
    
    /**
     * Removes the first item and shortens the list.
     * 
     * @returns 0 if the list was empty. Otherwise, returns the removed first item.
     */
    public int drop() {
       int previouslyHeld = 0;
       if (heldCount > 0) {
            previouslyHeld = held[0];
            
            for (int i = 1; i < MAX_ITEMS; i++) {
                held[i-1] = held[i];
            }
            heldCount--;
       }
       return previouslyHeld;
    }
}

interface Animal {
    public void eat(int food) throws Exception;
    public int poop() throws Exception;
}

// Mouse can only eat one thing and then it will poop it
class Mouse extends HoldOne implements Animal {
    @Override
    public void eat(int food) throws Exception {
        if (held != 0) throw new Exception("I\'m full! I need to poop, first!");
        
        put(food);
    }
    
    @Override
    public int poop() throws Exception {
        if (held == 0) throw new Exception("I\'m empty! Feed me, first!");

        return put(0);  // You'll need to change this to put(null) when you switch to generics, since you can only use Objects as a type in a generic
    }
}

// Cow can eat several things and then poop it all
class Cow extends HoldMany implements Animal {
    public void eat(int food) throws Exception {
        if (heldCount == MAX_ITEMS) throw new Exception("I\'m full! I need to poop, first!");
        
        put(food);
    }
    
    public int poop() throws Exception {
        if (heldCount == 0) throw new Exception("I\'m empty! Feed me, first!");
        
        int firstEaten = drop();
        while (heldCount > 0) {
            drop();
        }
        
        return firstEaten;
    }
}

public class MyClass {
    // Modify this function to accept different types. Don't copy!
    private static void printThing(int thing) {
        System.out.println("" + thing);
    }

    // Modify this function to only accept animals that extend HoldMany. Those animals should be able to eat different types. Don't copy!
    private static void handleLargeAnimal(Animal a, int[] food) throws Exception {
        for (int item : food) {
            a.eat(item);
        }
        printThing(a.poop());
    }
    
    // Modify this function to only accept animals that extend HoldOne. Those animals should be able to eat different types. Don't copy!
    private static void handleSmallAnimal(Animal a, int food) throws Exception {
        a.eat(food);
        printThing(a.poop());
    }
    
    public static void main(String args[]) throws Exception {
        HoldOne intHolder = new HoldOne();
        printThing(intHolder.put(20));
        printThing(intHolder.put(-1));
        
        Cow c = new Cow();
        handleLargeAnimal(c, new int[] {1, 2, 3});
        
        Mouse m = new Mouse();
        handleSmallAnimal(m, 42);
        
        /* Uncomment this section and make it work!
        
        HoldOne doubleHolder = new HoldOne(1.5);
        printThing(doubleHolder.put(3.5));
        printThing(doubleHolder.put(-1.0));
        
        HoldOne stringHolder = new HoldOne("Hello");
        printThing(stringHolder.put("There"));
        printThing(stringHolder.put("!"));
        
        Cow stringCow = new Cow();
        handleLargeAnimal(c, new String[] {"a", "b", "c"});
        
        Mouse stringMouse = new Mouse();
        handleSmallAnimal(m, "Meep");
        
        */
    }
}