Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
In this exercise I need to extend DecrementingCarousel. I need to implement HalvingCarousel. This subclass must halve elements instead of decrementing it by one. Note that you need to apply regular integer division, discarding the remainder. For example, 5 / 2 = 2.


I have a superclass called DecrementCarousel which has a method that returns an object called CarouselRun. CarouselRun has its own methods which I need to override in HalvingCarousel, but I don't know how.

Decrement Carousel:

Java
<pre>public class DecrementingCarousel {
    private final int capacity;
    static int[] carousel;
    int index;
    boolean isRun;
 
 
    {
        index = 0;
        isRun = false;
    }
 
    public DecrementingCarousel(int capacity) {
        this.capacity = capacity;
        carousel = new int[capacity];
 
    }
 
    public boolean addElement(int element){
        if (element > 0 && index < capacity && !isRun) {
            carousel[index++] = element;
            return true;
        }
        return false;
    }
 
    public CarouselRun run() {
        if (!isRun) {
            isRun = true;
            return new CarouselRun();
        }
        return null;
    }
}


Here are methods in CarouselRun:

Java
<pre>public class CarouselRun {
    protected final int[] array = DecrementingCarousel.carousel.clone();
    protected int position = 0;
 
    public int next() {
        if (isFinished())
            return -1;
        else {
            while (array[position %= array.length] <= 0) {
                position++;
            }
        }
        return array[position++]--;
    }
 
    public boolean isFinished() {
        for (int el : array)
            if (el > 0)
                return false;
        return true;
    }
}


This is HalvingCarousel class:

Java
public class HalvingCarousel extends DecrementingCarousel {
 
    public HalvingCarousel(final int capacity) {
 
        super(capacity);
 
    }
}


Main Class:


Java
public class Main {
    public static void main(String[] args) {
        CarouselRun run = new HalvingCarousel(7).run();
        System.out.println(run.isFinished()); //true
        System.out.println(run.next()); //-1
 
        DecrementingCarousel carousel = new HalvingCarousel(7);
 
        carousel.addElement(20);
        carousel.addElement(30);
        carousel.addElement(10);
 
        System.out.println(run.isFinished()); //false
 
        System.out.println(run.next()); //20
        System.out.println(run.next()); //30
        System.out.println(run.next()); //10
 
        System.out.println(run.next()); //10
        System.out.println(run.next()); //15
        System.out.println(run.next()); //5
 
        System.out.println(run.next()); //5
        System.out.println(run.next()); //7
        System.out.println(run.next()); //2
 
        System.out.println(run.next()); //2
        System.out.println(run.next()); //3
        System.out.println(run.next()); //1
 
        System.out.println(run.next()); //1
        System.out.println(run.next()); //1
 
        System.out.println(run.isFinished()); //true
        System.out.println(run.next()); //-1
    }
}


What I have tried:

I tried to override the methods from Decrementing Carousel in HalvingCarousel and then to make another class called HalvingCarouselRun where I could called methods from CarouselRun. I'm not sure about this part, how to make the elements to halve
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900