Click here to Skip to main content
15,867,686 members

Comments by Cris29M (Top 6 by date)

Cris29M 17-Jan-23 7:32am View    
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;
}
}
```
Cris29M 8-Dec-22 8:14am View    
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.
Cris29M 1-Dec-22 7:12am View    
I didn't know how to implement in a single method.
I implemented 3 methods for every length of the segment:
public static double length (Point a, Point b)
    {
        return sqrt(pow(b.getX() - a.getX(), 2) + pow(b.getY() - a.getY(), 2));
    }
    public static double length2 (Point a, Point c)
    {
        return sqrt(pow(c.getX() - a.getX(), 2) + pow(c.getY() - a.getY(), 2));
    }
    public static double length3 (Point b, Point c)
    {
        return sqrt(pow(c.getX() - b.getX(), 2) + pow(c.getY() - b.getY(), 2));
    }


This is all my Triangle class:
class Triangle {

    Point a;
    Point b;
    Point c;

    public Triangle(Point a, Point b, Point c) {
        //TODO
        double lab, lac, lbc;
        lab = length(a, b);
        lac = length2(a, c);
        lbc = length3(b, c);

        if ((lab + lac) > lbc || (lab + lbc) > lac || (lbc + lac) > lab) {
            throw new IllegalArgumentException();
        }
        this.a = a;
        this.b = b;
        this.c = c;

    }
    public static double length (Point a, Point b)
    {
        return sqrt(pow(b.getX() - a.getX(), 2) + pow(b.getY() - a.getY(), 2));
    }
    public static double length2 (Point a, Point c)
    {
        return sqrt(pow(c.getX() - a.getX(), 2) + pow(c.getY() - a.getY(), 2));
    }
    public static double length3 (Point b, Point c)
    {
        return sqrt(pow(c.getX() - b.getX(), 2) + pow(c.getY() - b.getY(), 2));
    }

    public double area() {
        //TODO

        return Math.abs((a.getX() - c.getX()) * (b.getY() - a.getY()) - (a.getX() - b.getX()) * (c.getY() - a.getY())) /2;

    }

    public Point centroid(){
        //TODO

        double x = (a.getX() + b.getX() + c.getX()) / 3;
        double y = (a.getY() + b.getY() + c.getY()) / 3;

        return new Point(x, y);

    }
}


I suppose it's not good implemented because when I'm running the code I receive this error
"Exception in thread "main" java.lang.IllegalArgumentException"
Cris29M 30-Nov-22 4:43am View    
Indeed! My mistake!
Cris29M 27-Nov-22 10:08am View    
Yeah, you're right! I should have been more explicit!
I received this result:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3"