Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 0
- GraduallyDecreasingCarousel. Этот подкласс должен уменьшать элементы путем постепенного увеличения уменьшения. Когда вам нужно уменьшить элемент в первый раз, уменьшите его на 1. В следующий раз, когда вам нужно уменьшить тот же элемент, уменьшите его на 2. Далее уменьшите его на 3, затем на 4 и так далее. Вы не должны продолжать уменьшение с неположительными элементами.
- Делал похожее задание но чучуть с другой формулировкой но не могу понять реализацию данного подкласса. изначально было 2 класса Caruselrun Dreametingcorusel мой код:
- import java.util.LinkedList;
- public class CarouselRun {
- private String operation;
- private int amountOfNotZeroElements; //could be atomicInt
- private LinkedList<Integer> carusel;
- private int decrementPosition;
- public int getAmountOfNotZeroElements() {
- return amountOfNotZeroElements;
- }
- public void setAmountOfNotZeroElements(int amountOfNotZeroElements) {
- this.amountOfNotZeroElements = amountOfNotZeroElements;
- this.decrementPosition = 0;
- }
- public LinkedList<Integer> getCarousel() {
- return carusel;
- }
- public CarouselRun() {
- this.carusel = new LinkedList();
- }
- public CarouselRun(String operation) {
- this.carusel = new LinkedList();
- this.operation = operation;
- }
- public int next() {
- if (carusel.isEmpty() || amountOfNotZeroElements == 0) {
- return -1;
- }
- int currentValue = carusel.get(this.decrementPosition);
- int newValue = 0;
- if (operation.equals("-")) {
- newValue = currentValue - 1;
- } else if (operation.equals("/")) {
- newValue = currentValue / 2;
- }
- carusel.set(decrementPosition, newValue);
- if (newValue == 0) {
- this.amountOfNotZeroElements--;
- }
- this.decrementPosition = calculateNewDecrementPosition(this.decrementPosition);
- return currentValue;
- }
- public int calculateNewDecrementPosition(int currentDecrementPosition) {
- if (amountOfNotZeroElements == 0) {
- return 0;
- }
- int newDecrementPosition = 0;
- if ((carusel.size() - 1) == currentDecrementPosition) {
- newDecrementPosition = 0;
- } else {
- newDecrementPosition = currentDecrementPosition + 1;
- }
- if (carusel.get(newDecrementPosition) == 0) {
- newDecrementPosition = calculateNewDecrementPosition(newDecrementPosition);
- }
- return newDecrementPosition;
- }
- public boolean isFinished() {
- if (amountOfNotZeroElements > 0) {
- return false;
- } else {
- return true;
- }
- }
- }
- package com.epam.rd.autotasks;
- public class DecrementingCarousel {
- private final CarouselRun carouselRun;
- private boolean isRunning;
- private int capacity;
- public DecrementingCarousel(int capacity) {
- this.isRunning = false;
- this.capacity = capacity;
- carouselRun = new CarouselRun("-");
- }
- public boolean addElement(int element) {
- if (isRunning) {
- return false;
- }
- if (element > 0 && capacity > 0) {
- carouselRun.setAmountOfNotZeroElements(carouselRun.getAmountOfNotZeroElements() + 1);
- carouselRun.getCarousel().add(element);
- capacity = capacity - 1;
- return true;
- } else {
- return false;
- }
- }
- public CarouselRun run() {
- if (isRunning) {
- return null;
- } else {
- isRunning = true;
- return carouselRun;
- }
- }
- }
- сам метод Gradually Decreasing Carousel
- public class GraduallyDecreasingCarousel extends DecrementingCarousel {
- private boolean isRunning;
- private int capacity;
- public void GraduallyDecreasing(int capacity) {
- this.isRunning = false;
- this.capacity = capacity;
- }
- public GraduallyDecreasingCarousel(int capacity) {
- super(capacity);
- }
- public boolean addElement(int element) {
- if (isRunning) {
- return false;
- }
- if (element > 0 && capacity > 0) {
- capacity = capacity - 1;
- return true;
- } else {
- return false;
- }
- }
- public CarouselRun run() {
- if (isRunning) {
- return null;
- } else {
- isRunning = true;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement