Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In this implementation I must produce a carousel run, which limits number of calls to the next method. When the limit of calls reached carousel run must consider itself finished.

Carousel Run class:

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;
    }
}



Decrementing Carousel class:


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;
    }
}


DecrementingCarouselWithLimitedRun class


Java
public class DecrementingCarouselWithLimitedRun extends DecrementingCarousel {
 
    public DecrementingCarouselWithLimitedRun(final int capacity, final int actionLimit) {
 
        super(capacity);
    }
    @Override
    public CarouselRun run() {
        if (!isRun) {
            isRun = true;
            return new DecrementingCarouselWithLimitRun();
        }
        return null;
    }
}


DecrementingCarouselWithLimitRun class:


Java
<pre>public class DecrementingCarouselWithLimitRun extends CarouselRun {
    public int decrement = 1;
 
    @Override
    public int next() {
        int beforeDecreasing;
        if (isFinished())
            return -1;
        else {
            beforeDecreasing = array[position];
            array[position] -= decrement;
            do {
                position++;
            }
            while (array[position %= array.length] <= 0 && !isFinished());
        }
            return beforeDecreasing;
    }
}



This is Main class:


Java
public class Main {
    public static void main(String[] args) {
        DecrementingCarousel carousel = new DecrementingCarouselWithLimitedRun(7, 10);
 
        carousel.addElement(20);
        carousel.addElement(30);
        carousel.addElement(10);
 
        CarouselRun run = carousel.run();
 
        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()); //19
        System.out.println(run.next()); //29
        System.out.println(run.next()); //9
 
        System.out.println(run.next()); //18
        System.out.println(run.next()); //28
        System.out.println(run.next()); //8
 
        System.out.println(run.next()); //17
 
        System.out.println(run.isFinished()); //true
        System.out.println(run.next()); //-1
    }
}


What I have tried:

When I'm running the code I get:
?

Java
false
20
30
10
19
29
9
18
28
8
17
false
27


But it should be:


Java
false
20
30
10
19
29
9
18
28
8
17
true
-1


How I stop the code to run only for a limited times?
Posted
Updated 17-Jan-23 2:54am
Comments
Graeme_Grant 8-Dec-22 6:26am    
This is a repost of this question: How to implement halving carousel?[^].

Please do not repost a question if it has not been answered or to your satisfaction. There are a number of reasons why it happens. If instructions are given, please follow them. If not answered, then you need to look at your question and update it, not post a new one hoping for a better outcome. All that you are doing is wasting your time and ours, we are all volunteers and are not obligated to answer questions, we try to assist, in our own time, at our own expense. So please respect that.
Cris29M 8-Dec-22 8:14am    
No, there is a difference between them. In Halving Carousel must halve elements instead of decrementing it by one and in Decrementing Carousel with Limit the implementation must produce a carousel run, which limits number of calls to the next method. When the limit of calls reached carousel run must consider itself finished.
PS: I solved Halving Carousel.
baranchyk 17-Jan-23 7:14am    
Hello, what is the code for halving elements can't get by myself :( Tried everything I know but that doesnt work
Cris29M 17-Jan-23 7:32am    
CarouselRun class:
```
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;
}
}
```
DecrementingCarousel class:
```
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;
}
}
```
HalvingCarousel class:
```
public class HalvingCarousel extends DecrementingCarousel {

public HalvingCarousel(final int capacity) {

super(capacity);

}
@Override
public CarouselRun run() {
if (!isRun) {
isRun = true;
return new HalvingCarouselRun();
}
return null;
}
}
```
HalvingCarouselRun class:
```
public class HalvingCarouselRun extends CarouselRun {
public int decrement = 2;

@Override
public int next() {
int beforeDecreasing;
if (isFinished())
return -1;
else {
beforeDecreasing = array[position];
array[position] = array[position] / decrement;
do {
position++;
if (position == array.length) {
position = 0;
}
} while(array[position %= array.length] <= 0 && !isFinished());
}
return beforeDecreasing;
}
}
```
baranchyk 17-Jan-23 8:06am    
thanks a lot, now time to understand why my code doesnt work; Thanks once again.

1 solution

You should add field limit of actions and decrease it before return statemenet. And add one more if condition in isFinished()



@Override
    public int next() {
        int beforeDecreasing;
        if(isFinished()){
            return -1;
        } else {
            while (array[position %= array.length] <= 0) {
                position++;
            }
        }
        limitOfRuns--;
        return array[position++]--;
    }


public boolean isFinished() {
        if(limitOfRuns==0){
            return true;
        }
        else {
            for (int i : array) {
                if (i > 0) {
                    return false;
                }
            }
        }
        return true;
    }
 
Share this answer
 

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