Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Компоненти (40 поени) Problem 4 (3 / 47)
- Да се дефинира класа Component во која се чуваат:
- бојата
- тежината
- колекција од внатрешни компоненти (референци од класата Component).
- Во оваа класа да се дефинираат методите:
- Component(String color, int weight) - конструктор со аргументи боја и тежина
- void addComponent(Component component) - за додавање нова компонента во внатрешната колекција (во оваа колекција компонентите секогаш се подредени според тежината во растечки редослед, ако имаат иста тежина подредени се алфабетски според бојата).
- Да се дефинира класа Window во која се чуваат:
- име
- компоненти.
- Во оваа класа да се дефинираат следните методи:
- Window(String) - конструктор
- void addComponent(int position, Component component) - додава нова компонента на дадена позиција (цел број). На секоја позиција може да има само една компонента, ако се обидеме да додадеме компонента на зафатена позиција треба да се фрли исклучок од класата InvalidPositionException со порака Invalid position [pos], alredy taken!. Компонентите се подредени во растечки редослед според позицијата.
- String toString() - враќа стринг репрезентација на објектот (дадена во пример излезот)
- void changeColor(int weight, String color) - ја менува бојата на сите компоненти со тежина помала од проследената
- void swichComponents(int pos1, int pos2) - ги заменува компонените од проследените позиции.
- =============================================================================================================
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.TreeMap;
- import java.util.TreeSet;
- @SuppressWarnings("serial")
- class InvalidPositionException extends RuntimeException {
- InvalidPositionException(String msg) {
- super(msg);
- }
- public String getMessage() {
- return super.getMessage();
- }
- }
- class Component {
- String colour;
- int weight;
- TreeSet<Component> inner;
- final Comparator<Component> innerComparator = (c1, c2) -> {
- if (Integer.compare(c1.weight, c2.weight) != 0)
- return Integer.compare(c1.weight, c2.weight);
- else
- return c1.colour.compareTo(c2.colour);
- };
- void changeColours(String colour, int weight) {
- if (this.weight < weight)
- this.colour = colour;
- for (Component comp : inner)
- comp.changeColours(colour, weight);
- }
- Component(String colour, int weight) {
- this.colour = colour;
- this.weight = weight;
- this.inner = new TreeSet<>(innerComparator);
- }
- void addComponent(Component component) {
- inner.add(component);
- }
- @Override
- public String toString() {
- return String.format("%d:%s", weight, colour);
- }
- }
- class Window {
- final String name;
- TreeMap<Integer, Component> components;
- Window(String name) {
- this.name = name;
- this.components = new TreeMap<>();
- }
- void addComponent(int position, Component component) {
- if (components.containsKey(position))
- throw new InvalidPositionException
- ("Invalid position " + position + ", alredy taken!");
- components.put(position, component);
- }
- @Override
- public String toString() {
- StringBuilder str = new StringBuilder();
- str.append("WINDOW " + name + "\n" );
- for (Integer position : components.keySet()) {
- str.append(position + ":");
- createString(components.get(position), str, 0);
- }
- return str.toString();
- }
- void createString(Component component, StringBuilder str, int depth) {
- str.append(crticki(depth) + component.toString() + "\n");
- for (Component comp : component.inner)
- createString(comp, str, depth + 1);
- }
- private String crticki(int depth) {
- String str = "";
- while (depth > 0) {
- str += "---";
- depth--;
- }
- return str;
- }
- void changeColor(int weight, String colour) {
- components.entrySet()
- .stream()
- .forEach(t -> t.getValue().changeColours(colour, weight));
- }
- void swichComponents(int position1, int position2) {
- Component component = components.get(position1);
- components.put(position1, components.get(position2));
- components.put(position2, component);
- }
- }
- public class ComponentTest {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String name = scanner.nextLine();
- Window window = new Window(name);
- Component prev = null;
- while (true) {
- try {
- int what = scanner.nextInt();
- scanner.nextLine();
- if (what == 0) {
- int position = scanner.nextInt();
- window.addComponent(position, prev);
- } else if (what == 1) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev = component;
- } else if (what == 2) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev.addComponent(component);
- prev = component;
- } else if (what == 3) {
- String color = scanner.nextLine();
- int weight = scanner.nextInt();
- Component component = new Component(color, weight);
- prev.addComponent(component);
- } else if(what == 4) {
- break;
- }
- } catch (InvalidPositionException e) {
- System.out.println(e.getMessage());
- }
- scanner.nextLine();
- }
- System.out.println("=== ORIGINAL WINDOW ===");
- System.out.println(window);
- int weight = scanner.nextInt();
- scanner.nextLine();
- String color = scanner.nextLine();
- window.changeColor(weight, color);
- System.out.println(String.format("=== CHANGED COLOR (%d, %s) ===", weight, color));
- System.out.println(window);
- int pos1 = scanner.nextInt();
- int pos2 = scanner.nextInt();
- System.out.println(String.format("=== SWITCHED COMPONENTS %d <-> %d ===", pos1, pos2));
- window.swichComponents(pos1, pos2);
- System.out.println(window);
- }
- }
- ===============================================================================================================
- Sample input
- FIREFOX
- 1
- RED
- 30
- 3
- MAGENTA
- 90
- 0
- 1
- 2
- GREEN
- 40
- 3
- RED
- 50
- 2
- BLUE
- 50
- 2
- CYAN
- 60
- 1
- YELLOW
- 80
- 3
- WHITE
- 35
- 0
- 2
- 4
- 60
- BLACK
- 1 2
- ===============================================================================================================
- Sample output
- === ORIGINAL WINDOW ===
- WINDOW FIREFOX
- 1:30:RED
- ---40:GREEN
- ------50:BLUE
- ---------60:CYAN
- ------50:RED
- ---90:MAGENTA
- 2:80:YELLOW
- ---35:WHITE
- === CHANGED COLOR (60, BLACK) ===
- WINDOW FIREFOX
- 1:30:BLACK
- ---40:BLACK
- ------50:BLACK
- ---------60:CYAN
- ------50:BLACK
- ---90:MAGENTA
- 2:80:YELLOW
- ---35:BLACK
- === SWITCHED COMPONENTS 1 <-> 2 ===
- WINDOW FIREFOX
- 1:80:YELLOW
- ---35:BLACK
- 2:30:BLACK
- ---40:BLACK
- ------50:BLACK
- ---------60:CYAN
- ------50:BLACK
- ---90:MAGENTA
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement