We must look for the intersection of sets. Each class has a method that adds an element, a method that displays the contents of a class on the screen. And others. And how to make one addElement method which, depending on the class, would add Interval in one case, IntervalSet in another case, or allIntervalSet In the same way, one name of the output method on the screen, for example convertToString. Override shorter. Thank.
The class class describes 1 interval for example [-5; 8]
public class Interval { public Double from; public Double to; public Interval(Double from, Double to) { this.from = from; this.to = to; } public void convertIntervalToString(){ System.out.print("["+this.from+" ; "+this.to+"]"); } public void putFrom(Double x) { this.from = x; } public void putTo(Double x) { this.to = x; } } The IntervalSet class defines 1 set of any length [-7; -6] [- 5; 0] [1; 2]
public class IntervalSet { private ArrayList<Interval> intervalSet; public IntervalSet() { this.intervalSet = new ArrayList<Interval>(); } public void convertIntervalSetToString(){ int i = this.intervalSet.size(); if (i == 0){ System.out.println("[Пустое множество]"); }; for (int j=0; j<i; j++) this.intervalSet.get(j).convertIntervalToString(); System.out.println(""); } public void addInterval(Interval x){ this.intervalSet.add(x); }//add() это метод java The class allIntervalSet defines several mathematical sets.
[-9; -8] [- 7; 0] [1; 7] [] [] []
[] [] [] [] []
[-9; 9]
[-8; 0] [] [] [] [] []
[] [] [] [] []
public class AllIntervalSet { private ArrayList<IntervalSet> allIntervalSet; public AllIntervalSet() { this.allIntervalSet = new ArrayList<IntervalSet>(); } public void convertAllIntervalSetToString(){ int i = this.allIntervalSet.size(); for (int j=0; j<i; j++) allIntervalSet.get(j).convertIntervalSetToString(); } public void addIntervalSet(IntervalSet x){ this.allIntervalSet.add(x); } }
addElement, which class should it be in, in all three, or what? And, probably, it is worth writing the interface from which all three classes will be inherited. - dirariaconvertToString. Inherit all three classes from this interface. Change the names of the methods for output in all three classes toconvertToString. And here in each class there is a method which, roughly speaking, isoverridefor the interface method - diraria