Advertisement
STANAANDREY

weights java

Nov 13th, 2023 (edited)
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. abstract class Weight {
  2.     public abstract int capacity();
  3. }
  4.  
  5. class SimpleWeight extends Weight {
  6.     private int capacity;
  7.  
  8.  
  9.     public SimpleWeight(int capacity) {
  10.         this.capacity = capacity;
  11.     }
  12.  
  13.     @Override
  14.     public int capacity() {
  15.         return capacity;
  16.     }
  17. }
  18.  
  19. class DoubleWeight extends Weight {
  20.     private Weight weight1, weight2;
  21.  
  22.     public DoubleWeight() {}
  23.     public DoubleWeight(Weight weight1, Weight weight2) {
  24.         this.weight1 = weight1;
  25.         this.weight2 = weight2;
  26.     }
  27.  
  28.     public void setWeight1(Weight w) {
  29.         weight1 = w;
  30.     }
  31.  
  32.     public void setWeight2(Weight w) {
  33.         weight2 = w;
  34.     }
  35.  
  36.     @Override
  37.     public int capacity() {
  38.         return weight1.capacity() + weight2.capacity();
  39.     }
  40. }
  41.  
  42. class MultipleWeight extends Weight {
  43.     private Weight[] weights;
  44.  
  45.     public MultipleWeight(Weight[] weights) {
  46.         this.weights = weights;
  47.     }
  48.  
  49.     @Override
  50.     public int capacity() {
  51.         int sum = 0;
  52.         for (int i = 0; i < weights.length; i++) {
  53.             sum += weights[i].capacity();
  54.         }
  55.         return sum;
  56.     }
  57. }
  58.  
  59. class WeightsColection {
  60.     private Weight[] weights;
  61.     private int len;
  62.  
  63.     public WeightsColection(int maxLen) {
  64.         weights = new Weight[maxLen];
  65.     }
  66.  
  67.     public void add(Weight weight) {
  68.         if (len == weight.length) {
  69.             return;
  70.         }
  71.         weights[len++] = weight;
  72.     }
  73.  
  74.     public double mean() {
  75.         int sum = 0;
  76.         for (int i = 0; i < len; i++) {
  77.             sum += weights[i].capacity();
  78.         }
  79.         return (double)sum / len;
  80.     }
  81. }
  82.  
  83. public class Main {
  84.     public static void main(String[] args) {
  85.         SimpleWeight sw = new SimpleWeight(3);
  86.         DoubleWeight dw = new DoubleWeight();
  87.         dw.setWeight1(new SimpleWeight(5));
  88.         dw.setWeight2(new SimpleWeight(10));
  89.         MultipleWeight mw = new MultipleWeight(new Weight[]{sw, dw, new SimpleWeight(100)});
  90.         System.out.println(mw.capacity());
  91.         DoubleWeight dw2 = new DoubleWeight();
  92.         dw2.setWeight1(new SimpleWeight(30));
  93.         dw2.setWeight2(new SimpleWeight(40));
  94.         WeightsColection wc = new WeightsColection(5);
  95.         wc.add(new SimpleWeight(10));
  96.         wc.add(dw2);
  97.         wc.add(mw);
  98.         System.out.println(wc.mean());//*/
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement