Advertisement
ChaizOFF3123123

Untitled

Aug 16th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.95 KB | None | 0 0
  1. 0
  2.  
  3.  
  4. GraduallyDecreasingCarousel. Этот подкласс должен уменьшать элементы путем постепенного увеличения уменьшения. Когда вам нужно уменьшить элемент в первый раз, уменьшите его на 1. В следующий раз, когда вам нужно уменьшить тот же элемент, уменьшите его на 2. Далее уменьшите его на 3, затем на 4 и так далее. Вы не должны продолжать уменьшение с неположительными элементами.
  5.  
  6. Делал похожее задание но чучуть с другой формулировкой но не могу понять реализацию данного подкласса. изначально было 2 класса Caruselrun Dreametingcorusel мой код:
  7.  
  8. import java.util.LinkedList;
  9.  
  10. public class CarouselRun {
  11.  
  12.     private String operation;
  13.     private int amountOfNotZeroElements; //could be atomicInt
  14.     private LinkedList<Integer> carusel;
  15.     private int decrementPosition;
  16.  
  17.     public int getAmountOfNotZeroElements() {
  18.         return amountOfNotZeroElements;
  19.     }
  20.  
  21.     public void setAmountOfNotZeroElements(int amountOfNotZeroElements) {
  22.         this.amountOfNotZeroElements = amountOfNotZeroElements;
  23.         this.decrementPosition = 0;
  24.     }
  25.  
  26.     public LinkedList<Integer> getCarousel() {
  27.         return carusel;
  28.     }
  29.  
  30.     public CarouselRun() {
  31.         this.carusel = new LinkedList();
  32.     }
  33.  
  34.     public CarouselRun(String operation) {
  35.         this.carusel = new LinkedList();
  36.         this.operation = operation;
  37.     }
  38.  
  39.     public int next() {
  40.  
  41.         if (carusel.isEmpty() || amountOfNotZeroElements == 0) {
  42.             return -1;
  43.         }
  44.         int currentValue = carusel.get(this.decrementPosition);
  45.  
  46.         int newValue = 0;
  47.         if (operation.equals("-")) {
  48.             newValue = currentValue - 1;
  49.         } else if (operation.equals("/")) {
  50.             newValue = currentValue / 2;
  51.         }
  52.  
  53.         carusel.set(decrementPosition, newValue);
  54.  
  55.         if (newValue == 0) {
  56.             this.amountOfNotZeroElements--;
  57.         }
  58.  
  59.         this.decrementPosition = calculateNewDecrementPosition(this.decrementPosition);
  60.  
  61.         return currentValue;
  62.  
  63.  
  64.     }
  65.  
  66.     public int calculateNewDecrementPosition(int currentDecrementPosition) {
  67.  
  68.         if (amountOfNotZeroElements == 0) {
  69.             return 0;
  70.         }
  71.  
  72.         int newDecrementPosition = 0;
  73.  
  74.         if ((carusel.size() - 1) == currentDecrementPosition) {
  75.             newDecrementPosition = 0;
  76.         } else {
  77.             newDecrementPosition = currentDecrementPosition + 1;
  78.         }
  79.  
  80.         if (carusel.get(newDecrementPosition) == 0) {
  81.             newDecrementPosition = calculateNewDecrementPosition(newDecrementPosition);
  82.         }
  83.  
  84.         return newDecrementPosition;
  85.     }
  86.  
  87.     public boolean isFinished() {
  88.         if (amountOfNotZeroElements > 0) {
  89.             return false;
  90.         } else {
  91.             return true;
  92.         }
  93.     }
  94.  
  95. }
  96. package com.epam.rd.autotasks;
  97.  
  98.  
  99.  
  100. public class DecrementingCarousel {
  101.     private final CarouselRun carouselRun;
  102.     private boolean isRunning;
  103.     private int capacity;
  104.  
  105.     public DecrementingCarousel(int capacity) {
  106.         this.isRunning = false;
  107.         this.capacity = capacity;
  108.         carouselRun = new CarouselRun("-");
  109.  
  110.     }
  111.  
  112.     public boolean addElement(int element) {
  113.  
  114.         if (isRunning) {
  115.             return false;
  116.         }
  117.  
  118.         if (element > 0 && capacity > 0) {
  119.             carouselRun.setAmountOfNotZeroElements(carouselRun.getAmountOfNotZeroElements() + 1);
  120.             carouselRun.getCarousel().add(element);
  121.             capacity = capacity - 1;
  122.             return true;
  123.         } else {
  124.             return false;
  125.         }
  126.     }
  127.  
  128.     public CarouselRun run() {
  129.         if (isRunning) {
  130.             return null;
  131.         } else {
  132.             isRunning = true;
  133.             return carouselRun;
  134.         }
  135.     }
  136. }
  137. сам метод Gradually Decreasing Carousel
  138.  
  139. public class GraduallyDecreasingCarousel extends DecrementingCarousel {
  140.     private boolean isRunning;
  141.     private int capacity;
  142.  
  143.  
  144.  
  145.     public void GraduallyDecreasing(int capacity) {
  146.  
  147.         this.isRunning = false;
  148.         this.capacity = capacity;
  149.  
  150.     }
  151.  
  152.     public GraduallyDecreasingCarousel(int capacity) {
  153.         super(capacity);
  154.     }
  155.  
  156.     public boolean addElement(int element) {
  157.  
  158.         if (isRunning) {
  159.             return false;
  160.         }
  161.  
  162.         if (element > 0 && capacity > 0) {
  163.  
  164.             capacity = capacity - 1;
  165.             return true;
  166.         } else {
  167.             return false;
  168.         }
  169.     }
  170.  
  171.     public CarouselRun run() {
  172.         if (isRunning) {
  173.             return null;
  174.         } else {
  175.             isRunning = true;
  176.  
  177.         }
  178.         return null;
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement