Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Weight {
- public abstract int capacity();
- }
- class SimpleWeight extends Weight {
- private int capacity;
- public SimpleWeight(int capacity) {
- this.capacity = capacity;
- }
- @Override
- public int capacity() {
- return capacity;
- }
- }
- class DoubleWeight extends Weight {
- private Weight weight1, weight2;
- public DoubleWeight() {}
- public DoubleWeight(Weight weight1, Weight weight2) {
- this.weight1 = weight1;
- this.weight2 = weight2;
- }
- public void setWeight1(Weight w) {
- weight1 = w;
- }
- public void setWeight2(Weight w) {
- weight2 = w;
- }
- @Override
- public int capacity() {
- return weight1.capacity() + weight2.capacity();
- }
- }
- class MultipleWeight extends Weight {
- private Weight[] weights;
- public MultipleWeight(Weight[] weights) {
- this.weights = weights;
- }
- @Override
- public int capacity() {
- int sum = 0;
- for (int i = 0; i < weights.length; i++) {
- sum += weights[i].capacity();
- }
- return sum;
- }
- }
- class WeightsColection {
- private Weight[] weights;
- private int len;
- public WeightsColection(int maxLen) {
- weights = new Weight[maxLen];
- }
- public void add(Weight weight) {
- if (len == weight.length) {
- return;
- }
- weights[len++] = weight;
- }
- public double mean() {
- int sum = 0;
- for (int i = 0; i < len; i++) {
- sum += weights[i].capacity();
- }
- return (double)sum / len;
- }
- }
- public class Main {
- public static void main(String[] args) {
- SimpleWeight sw = new SimpleWeight(3);
- DoubleWeight dw = new DoubleWeight();
- dw.setWeight1(new SimpleWeight(5));
- dw.setWeight2(new SimpleWeight(10));
- MultipleWeight mw = new MultipleWeight(new Weight[]{sw, dw, new SimpleWeight(100)});
- System.out.println(mw.capacity());
- DoubleWeight dw2 = new DoubleWeight();
- dw2.setWeight1(new SimpleWeight(30));
- dw2.setWeight2(new SimpleWeight(40));
- WeightsColection wc = new WeightsColection(5);
- wc.add(new SimpleWeight(10));
- wc.add(dw2);
- wc.add(mw);
- System.out.println(wc.mean());//*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement