diff --git a/Lab_works.iml b/Lab_works.iml new file mode 100644 index 0000000..205584e --- /dev/null +++ b/Lab_works.iml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Exam/WeatherSystem.java b/src/Exam/WeatherSystem.java new file mode 100644 index 0000000..ffb30dc --- /dev/null +++ b/src/Exam/WeatherSystem.java @@ -0,0 +1,118 @@ +package Exam; + +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.concurrent.ThreadLocalRandom; + +public class WeatherStation { + private static WeatherStation instance; + private String stationName; + public List weatherDataHistory; + + private WeatherStation(String stationName) { + this.stationName = stationName; + this.weatherDataHistory = new ArrayList<>(); + } + + public static WeatherStation getInstance(String stationName) { + if (instance == null) { + instance = new WeatherStation(stationName); + } + return instance; + } + + public String getWeatherData() { + String currentWeather = "Weather data.\nTemperature: " + ThreadLocalRandom.current().nextInt(-20, 41); + weatherDataHistory.add(currentWeather); + return currentWeather; + } + + public List getWeatherDataHistory() { + return weatherDataHistory; + } + + public class WeatherProxy { + private WeatherStation weatherStation; + private String stationName; + private WeatherObserver observer; + + public WeatherProxy(String stationName) { + this.stationName = stationName; + } + + private void checkExists() { + if (weatherStation == null) { + this.weatherStation = WeatherStation.getInstance(stationName); + } + } + + public String getWeatherData() { + checkExists(); + + observer.notify(); + return weatherStation.getWeatherData(); + } + + public List getWeatherDataHistory() { + checkExists(); + + observer.notify(); + return weatherStation.getWeatherDataHistory(); + } + + + public WeatherIterator iterator() { + return new WeatherIterator(this.getWeatherDataHistory()); + } + } + + public class WeatherObserver { + private WeatherProxy weatherStation; + private List subscribers = new ArrayList<>(); + + public WeatherObserver(WeatherProxy weatherStation, Object... subscribers) { + this.weatherStation = weatherStation; + this.subscribers.addAll(Arrays.asList(subscribers)); + } + + public void subscribe(Object subscriber) { + this.subscribers.add(subscriber); + } + + public void unsubscribe(Object subscriber) { + this.subscribers.remove(subscriber); + } + + public void notify() { + for (Object subscriber : subscribers) { +// subscriber.update() + } + } + } + + public class WeatherIterator { + private List weatherDataHistory; + private int currentPosition = 0; + + public WeatherIterator(List weatherDataHistory) { + this.weatherDataHistory = weatherDataHistory; + } + + public boolean hasNext() { + return currentPosition < weatherDataHistory.size(); + } + + public String getNext() { + if (!this.hasNext()) { + return null; + } + + String nextString = weatherDataHistory.get(currentPosition); + currentPosition++; + + return nextString; + } + } +} + diff --git a/src/MKR2/MathFunctions.java b/src/MKR2/MathFunctions.java new file mode 100644 index 0000000..5f8beb6 --- /dev/null +++ b/src/MKR2/MathFunctions.java @@ -0,0 +1,18 @@ +package MKR2; + +public class MathFunctions { + + public static int Factorial(int n) { + if (n < 0) + throw new IllegalArgumentException("Input must be a non-negative integer."); + if (n == 0) + return 1; + int result = 1; + for (int i = 0; i < n; i++) + { + result *=i; + } + return result; + } +} + diff --git a/src/MKR2/MathFunctionsTest.java b/src/MKR2/MathFunctionsTest.java new file mode 100644 index 0000000..0ec0891 --- /dev/null +++ b/src/MKR2/MathFunctionsTest.java @@ -0,0 +1,23 @@ +package MKR2; + +import org.junit.Test; +import org.testng.Assert; +//import org.testng.annotations.Test; + +public class MathFunctionsTest { + + @Test + public void calculates1to5() { + Assert.assertEquals(1, MathFunctions.Factorial(0)); + Assert.assertEquals(1, MathFunctions.Factorial(1)); + Assert.assertEquals(2, MathFunctions.Factorial(2)); + Assert.assertEquals(6, MathFunctions.Factorial(3)); + Assert.assertEquals(24, MathFunctions.Factorial(4)); + Assert.assertEquals(120, MathFunctions.Factorial(5)); + } + + @Test(expected = IllegalArgumentException.class) + public void testInputException() { + MathFunctions.Factorial(-10); + } +} diff --git a/src/MKR2/SimpleList.java b/src/MKR2/SimpleList.java new file mode 100644 index 0000000..63f2842 --- /dev/null +++ b/src/MKR2/SimpleList.java @@ -0,0 +1,39 @@ +package MKR2; + +import java.util.Iterator; + +public class SimpleList implements Iterable { + private Object[] elements; + private int size; + + public void Simplelist(int size) { + this.elements = new Object[size]; + this.size = size; + } + + public void add(E element) { + elements[size++] = element; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < elements.length - 1; + } + + @Override + public E next() { + if (hasNext()) { + return (E) elements[currentIndex++]; + } + + return null; + } + }; + } +} + diff --git a/src/MKR2/TaskControl.java b/src/MKR2/TaskControl.java new file mode 100644 index 0000000..f9ae26a --- /dev/null +++ b/src/MKR2/TaskControl.java @@ -0,0 +1,129 @@ +package MKR2; + +import java.util.ArrayList; +import java.util.List; + +class Task { + private String name; + private String description; + private boolean completed; + + public Task(String name, String description) { + this.name = name; + this.description = description; + this.completed = false; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public boolean isCompleted() { + return completed; + } + + public void complete() { + completed = true; + } +} + +class TaskFactory { + public static Task createTask(String name, String description) { + return new Task(name, description); + } +} + +class PriorityTaskDecorator extends Task { + private int priority; + + public PriorityTaskDecorator(Task task, int priority) { + super(task.getName(), task.getDescription()); + this.priority = priority; + } + + public int getPriority() { + return priority; + } +} + +class TaskManager { + private static TaskManager instance; + private List tasks; + private List users; + + private TaskManager() { + tasks = new ArrayList<>(); + } + + public static TaskManager getInstance() { + if (instance == null) { + instance = new TaskManager(); + } + return instance; + } + + public void subscribe(User user) { + this.users.add(user); + } + + private void notify(String message) { + for (User user : users) { + user.update(message); + } + } + + public void addTask(Task task) { + tasks.add(task); + notify("Task added: " + task.getDescription()); + } + + public void completeTask(Task task) { + task.complete(); + notify("Task completed: " + task.getDescription()); + } + + public List getTasks() { + return tasks; + } +} + +class User { + private String name; + + public User(String name) { + this.name = name; + } + + public void update(String message) { + System.out.println(name + " received notification: " + message); + } +} + +// Examples of use +class Main { + public static void main(String[] args) { + // Creating a user and subscribing to the Observer + User user = new User("User1"); + TaskManager.getInstance().subscribe(user); + + // Creating a task and adding to a task manager + Task task1 = TaskFactory.createTask("Work", "Do something productive, idk."); + TaskManager.getInstance().addTask(task1); + + Task task2 = TaskFactory.createTask("Rest", "Do something else, idk."); + TaskManager.getInstance().addTask(task2); + + // Completing a task + TaskManager.getInstance().completeTask(task1); + + // Displaying a list of tasks for a user + System.out.println("User's tasks:"); + for (Task task : TaskManager.getInstance().getTasks()) { + System.out.println(task.getDescription() + " - Completed: " + task.isCompleted()); + } + } +} \ No newline at end of file diff --git a/src/labwork3/Image.java b/src/labwork3/Image.java new file mode 100644 index 0000000..b3244db --- /dev/null +++ b/src/labwork3/Image.java @@ -0,0 +1,36 @@ +package labwork3; + +/** + * The labwork3.Image interface represents an image with methods for selecting pixels, drawing pixels, finding colour by coordinates, and displaying the image. + */ +public interface Image { + + /** + * Selects a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + void selectPixel(int x, int y); + + /** + * Draws a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + void drawPixel(int x, int y); + + /** + * Finds the colour of the pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + void findColourByCoordinates(int x, int y); + + /** + * Displays the image. + */ + void display(); +} diff --git a/src/labwork3/ImageProxy.java b/src/labwork3/ImageProxy.java new file mode 100644 index 0000000..bfb657b --- /dev/null +++ b/src/labwork3/ImageProxy.java @@ -0,0 +1,95 @@ +package labwork3; + +/** + * The labwork3.ImageProxy class is an implementation of the labwork3.Image interface that acts as a proxy for the labwork3.RealImage class. + * It provides methods to handle image operations such as selecting pixels, drawing pixels, finding colour by coordinates, and displaying the image. + */ +public class ImageProxy implements Image { + private int width, height; + private RealImage realImage; + + /** + * Constructs an labwork3.ImageProxy with the specified width and height. + * + * @param width the width of the image + * @param height the height of the image + */ + public ImageProxy(int width, int height) { + this.width = width; + this.height = height; + } + + /** + * Checks if the realImage is initialized, and initializes it if it's null. + */ + private void checkRealImage() { + if (realImage == null) { + realImage = new RealImage(width, height); + } + } + + /** + * Checks if the provided coordinates are within the image bounds. + * + * @param x the x-coordinate + * @param y the y-coordinate + */ + private void checkCoordinates(int x, int y) { + if (x < 0 || x > width || y < 0 || y > height) { + System.out.println("Coordinates out of image size."); + System.exit(1); + } + } + + /** + * Selects a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void selectPixel(int x, int y) { + checkRealImage(); + checkCoordinates(x, y); + + realImage.selectPixel(x, y); + } + + /** + * Draws a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void drawPixel(int x, int y) { + checkRealImage(); + checkCoordinates(x, y); + + realImage.drawPixel(x, y); + } + + /** + * Finds the colour of the pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void findColourByCoordinates(int x, int y) { + checkRealImage(); + checkCoordinates(x, y); + + realImage.findColourByCoordinates(x, y); + } + + /** + * Displays the image along with its width and height. + */ + @Override + public void display() { + checkRealImage(); + + realImage.display(); + } +} diff --git a/src/labwork3/Main.java b/src/labwork3/Main.java new file mode 100644 index 0000000..1344e8b --- /dev/null +++ b/src/labwork3/Main.java @@ -0,0 +1,12 @@ +package labwork3; + +public class Main { + public static void main(String[] args) { + ImageProxy imageProxy = new ImageProxy(1920, 1080); + imageProxy.selectPixel(10, 50); + imageProxy.drawPixel(50, 138); + imageProxy.findColourByCoordinates(15, 47); + imageProxy.drawPixel(2000, 1400); + imageProxy.display(); + } +} \ No newline at end of file diff --git a/src/labwork3/RealImage.java b/src/labwork3/RealImage.java new file mode 100644 index 0000000..26990f1 --- /dev/null +++ b/src/labwork3/RealImage.java @@ -0,0 +1,61 @@ +package labwork3; + +/** + * The labwork3.RealImage class is an implementation of the labwork3.Image interface representing an actual image. + * It provides methods to handle image properties such as width and height, and image operations such as selecting pixels, drawing pixels, finding colour by coordinates, and displaying the image. + */ +public class RealImage implements Image { + private int width, height; + + /** + * Constructs a labwork3.RealImage with the specified width and height. + * + * @param width the width of the image + * @param height the height of the image + */ + public RealImage(int width, int height) { + this.width = width; + this.height = height; + } + + /** + * Selects a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void selectPixel(int x, int y) { + System.out.printf("Selected a pixel at (%d, %d).\n", x, y); + } + + /** + * Draws a pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void drawPixel(int x, int y) { + System.out.printf("Drawn a pixel at (%d, %d).\n", x, y); + } + + /** + * Finds the colour of the pixel at the specified coordinates. + * + * @param x the x-coordinate of the pixel + * @param y the y-coordinate of the pixel + */ + @Override + public void findColourByCoordinates(int x, int y) { + System.out.printf("Found colour at (%d, %d).\n", x, y); + } + + /** + * Displays the image along with its width and height. + */ + @Override + public void display() { + System.out.printf("Displaying image. Width: %d pixels, height %d pixels.\n", width, height); + } +} diff --git a/src/labwork4/API1.java b/src/labwork4/API1.java new file mode 100644 index 0000000..b6964b1 --- /dev/null +++ b/src/labwork4/API1.java @@ -0,0 +1,18 @@ +package labwork4; + +/** + * The labwork4.API1 interface represents a first API for rendering vector objects. + * + * @author rhinemann + */ +public interface API1 extends GenericAPI { + + /** + * Renders a given vector shape with the first API. + * + * @param shape a vector object to render + * @author rhinemann + */ + @Override + void render(VectorObject shape); +} diff --git a/src/labwork4/API1Concrete.java b/src/labwork4/API1Concrete.java new file mode 100644 index 0000000..4dc4c6b --- /dev/null +++ b/src/labwork4/API1Concrete.java @@ -0,0 +1,20 @@ +package labwork4; + +/** + * The labwork4.API1Concrete class is an implementation of labwork4.API1 interface for rendering vector objects with a first API. + * + * @author rhinemann + */ +public class API1Concrete implements API1 { + + /** + * Renders a given vector shape with the first API. + * + * @param shape a vector object to render + * @author rhinemann + */ + @Override + public void render(VectorObject shape) { + System.out.println("Rendered shape through API1"); + } +} diff --git a/src/labwork4/API2.java b/src/labwork4/API2.java new file mode 100644 index 0000000..5f813f8 --- /dev/null +++ b/src/labwork4/API2.java @@ -0,0 +1,18 @@ +package labwork4; + +/** + * The labwork4.API2 interface represents a second API for rendering vector objects. + * + * @author rhinemann + */ +public interface API2 extends GenericAPI { + + /** + * Renders a given vector shape with the second API. + * + * @param shape a vector object to render + * @author rhinemann + */ + @Override + void render(VectorObject shape); +} diff --git a/src/labwork4/API2Concrete.java b/src/labwork4/API2Concrete.java new file mode 100644 index 0000000..9772091 --- /dev/null +++ b/src/labwork4/API2Concrete.java @@ -0,0 +1,20 @@ +package labwork4; + +/** + * The labwork4.API2Concrete class is an implementation of labwork4.API2 interface for rendering vector objects with a first API. + * + * @author rhinemann + */ +public class API2Concrete implements API2 { + + /** + * Renders a given vector shape with the second API. + * + * @param shape a vector object to render + * @author rhinemann + */ + @Override + public void render(VectorObject shape) { + System.out.println("Rendered shape through API2"); + } +} diff --git a/src/labwork4/GenericAPI.java b/src/labwork4/GenericAPI.java new file mode 100644 index 0000000..48958ed --- /dev/null +++ b/src/labwork4/GenericAPI.java @@ -0,0 +1,17 @@ +package labwork4; + +/** + * The labwork4.GenericAPI interface represents a generic API for rendering vector objects. + * + * @author rhinemann + */ +public interface GenericAPI { + + /** + * Renders a given vector shape. + * + * @param shape a vector object to render + * @author rhinemann + */ + void render(VectorObject shape); +} diff --git a/src/labwork4/Main.java b/src/labwork4/Main.java new file mode 100644 index 0000000..849d711 --- /dev/null +++ b/src/labwork4/Main.java @@ -0,0 +1,18 @@ +package labwork4; + +import java.awt.*; + +public class Main { + public static void main(String[] args) { + Point origin = new Point(0, 0); + Point end = new Point(10, 13); + API1Concrete api1Concrete = new API1Concrete(); + API2Concrete api2Concrete = new API2Concrete(); + + Rectangle rectangle1 = new Rectangle(origin, end, api1Concrete); + Rectangle rectangle2 = new Rectangle(origin, end, api2Concrete); + + rectangle1.draw(); + rectangle2.draw(); + } +} diff --git a/src/labwork4/Rectangle.java b/src/labwork4/Rectangle.java new file mode 100644 index 0000000..e71c7a4 --- /dev/null +++ b/src/labwork4/Rectangle.java @@ -0,0 +1,38 @@ +package labwork4; + +import java.awt.*; + +/** + * labwork4.Rectangle class is an implementation of VectorObject representing a rectangle in vector graphics. + * + * @author rhinemann + */ +public class Rectangle implements VectorObject { + Point originPoint; + Point endPoint; + GenericAPI api; + + /** + * Constructs labwork4.Rectangle with the specified origin and end points and render API. + * + * @param originPoint beginning corner of a rectangle + * @param endPoint end corner of a rectangle + * @param api rendering API + * @author rhinemann + */ + public Rectangle(Point originPoint, Point endPoint, GenericAPI api) { + this.originPoint = originPoint; + this.endPoint = endPoint; + this.api = api; + } + + /** + * Draws the image on the canvas with a given render API. + * + * @author rhinemann + */ + @Override + public void draw() { + api.render(this); + } +} diff --git a/src/labwork4/VectorObject.java b/src/labwork4/VectorObject.java new file mode 100644 index 0000000..2675cf9 --- /dev/null +++ b/src/labwork4/VectorObject.java @@ -0,0 +1,16 @@ +package labwork4; + +/** + * The labwork4.VectorObject interface represents any vector object. + * + * @author rhinemann + */ +public interface VectorObject { + + /** + * Draws the vector object on a canvas. + * + * @author rhinemann + */ + void draw(); +} diff --git a/src/labwork5/Button.java b/src/labwork5/Button.java new file mode 100644 index 0000000..833c442 --- /dev/null +++ b/src/labwork5/Button.java @@ -0,0 +1,50 @@ +package labwork5; + +/** + * The Button class is an implementation of the InteractiveElement interface representing a button in a GUI. + * It can send and receive updates through a Mediator. + */ +public class Button implements InteractiveElement{ + private String id; + private final Mediator mediator; + + /** + * Constructs a Button with a specified ID and associated Mediator. + * + * @param id the unique identifier for the button + * @param mediator the Mediator to communicate with + */ + public Button(String id, Mediator mediator) { + this.id = id; + this.mediator = mediator; + } + + /** + * Retrieves the ID of the button. + * + * @return the ID of the button + */ + public String getId() { + return id; + } + + /** + * Sends an update message to the associated Mediator. + * + * @param updateMessage the update message to be sent + */ + @Override + public void sendUpdate(String updateMessage) { + mediator.update(this, updateMessage); + } + + /** + * Receives an update message from the Mediator and prints a message. + * + * @param updateMessage the received update message + */ + @Override + public void receiveUpdate(String updateMessage) { + System.out.printf("%s has has received an update with a message:\"%s\"\n", id, updateMessage); + } +} diff --git a/src/labwork5/Canvas.java b/src/labwork5/Canvas.java new file mode 100644 index 0000000..c755f94 --- /dev/null +++ b/src/labwork5/Canvas.java @@ -0,0 +1,51 @@ +package labwork5; + +/** + * The Canvas class is an implementation of the InteractiveElement interface representing a canvas in a GUI. + * It can send and receive updates through a Mediator. + */ +public class Canvas implements InteractiveElement{ + + private String id; + private Mediator mediator; + + /** + * Constructs a Canvas with a specified ID and associated Mediator. + * + * @param id the unique identifier for the canvas + * @param mediator the Mediator to communicate with + */ + public Canvas(String id, Mediator mediator) { + this.id = id; + this.mediator = mediator; + } + + /** + * Retrieves the ID of the canvas. + * + * @return the ID of the canvas + */ + public String getId() { + return id; + } + + /** + * Sends an update message to the associated Mediator. + * + * @param updateMessage the update message to be sent + */ + @Override + public void sendUpdate(String updateMessage) { + mediator.update(this, updateMessage); + } + + /** + * Receives an update message from the Mediator and prints a message. + * + * @param updateMessage the received update message + */ + @Override + public void receiveUpdate(String updateMessage) { + System.out.printf("%s has has received an update with a message:\"%s\"\n", id, updateMessage); + } +} diff --git a/src/labwork5/ConcreteMediator.java b/src/labwork5/ConcreteMediator.java new file mode 100644 index 0000000..31d413f --- /dev/null +++ b/src/labwork5/ConcreteMediator.java @@ -0,0 +1,40 @@ +package labwork5; + +import java.util.ArrayList; +import java.util.List; + +/** + * The ConcreteMediator class implements the Mediator interface and manages the communication between InteractiveElements. + */ +public class ConcreteMediator implements Mediator{ + private List interactiveElements; + + public ConcreteMediator() { + interactiveElements = new ArrayList<>(); + } + + /** + * Adds an InteractiveElement to the list of managed elements. + * + * @param element the InteractiveElement to be added to the mediator + */ + @Override + public void addElement(InteractiveElement element) { + interactiveElements.add(element); + } + + /** + * Updates all InteractiveElements except the source with the provided update message. + * + * @param source the source InteractiveElement that triggered the update + * @param update the update message or information + */ + @Override + public void update(InteractiveElement source, String update) { + for (InteractiveElement element : interactiveElements) { + if (element != source) { + element.receiveUpdate(update); + } + } + } +} diff --git a/src/labwork5/InteractiveElement.java b/src/labwork5/InteractiveElement.java new file mode 100644 index 0000000..7fa38e0 --- /dev/null +++ b/src/labwork5/InteractiveElement.java @@ -0,0 +1,20 @@ +package labwork5; + +/** + * The InteractiveElement interface defines the contract for all GUI elements that can interact with a Mediator. + */ +public interface InteractiveElement { + /** + * Sends an update message to the associated Mediator. + * + * @param updateMessage the update message to be sent + */ + void sendUpdate(String updateMessage); + + /** + * Receives an update message from the Mediator. + * + * @param updateMessage the received update message + */ + void receiveUpdate(String updateMessage); +} diff --git a/src/labwork5/Main.java b/src/labwork5/Main.java new file mode 100644 index 0000000..e990421 --- /dev/null +++ b/src/labwork5/Main.java @@ -0,0 +1,32 @@ +package labwork5; + +public class Main { + public static void main(String[] args) { + ConcreteMediator mediator = new ConcreteMediator(); + Button copyButton1 = new Button("Copy Button 1", mediator); + + TextField textField1 = new TextField("Text Field 1", mediator, "amet est placerat in egestas erat imperdiet sed euismod nisi"); + TextField textField2 = new TextField("Text Field 2", mediator, "auctor urna nunc id cursus metus aliquam eleifend"); + TextField textField3 = new TextField("Text Field 3", mediator, "integer eget aliquet nibh praesent tristique magna sit"); + + Canvas canvas = new Canvas("Canvas 1", mediator); + + mediator.addElement(copyButton1); + mediator.addElement(textField1); + mediator.addElement(textField2); + mediator.addElement(textField3); + mediator.addElement(canvas); + + copyButton1.sendUpdate("Copied to clipboard with " + copyButton1.getId()); + System.out.println(); + + textField1.sendUpdate("Cleared text from " + textField1.getId()); + System.out.println(); + textField2.sendUpdate("Changed text in " + textField2.getId()); + System.out.println(); + textField3.sendUpdate("Added text to " + textField3.getId()); + System.out.println(); + + canvas.sendUpdate("Moved a shape inside " + canvas.getId()); + } +} diff --git a/src/labwork5/Mediator.java b/src/labwork5/Mediator.java new file mode 100644 index 0000000..a5a5e79 --- /dev/null +++ b/src/labwork5/Mediator.java @@ -0,0 +1,21 @@ +package labwork5; + +/** + * The Mediator interface defines the contract for a mediator responsible for managing communication between InteractiveElements. + */ +public interface Mediator { + /** + * Adds an InteractiveElement to the mediator. + * + * @param element the InteractiveElement to be added to the mediator + */ + void addElement(InteractiveElement element); + + /** + * Updates the InteractiveElements based on a change from a source element. + * + * @param source the source InteractiveElement that triggered the update + * @param update the update message or information + */ + void update(InteractiveElement source, String update); +} diff --git a/src/labwork5/TextField.java b/src/labwork5/TextField.java new file mode 100644 index 0000000..cc85b09 --- /dev/null +++ b/src/labwork5/TextField.java @@ -0,0 +1,54 @@ +package labwork5; + +/** + * The TextField class is an implementation of the InteractiveElement interface representing a text field in a GUI. + * It can send and receive updates through a Mediator. + */ +public class TextField implements InteractiveElement{ + + private String id; + private Mediator mediator; + private String text; + + /** + * Constructs a TextField with a specified ID, associated Mediator, and initial text. + * + * @param id the unique identifier for the text field + * @param mediator the Mediator to communicate with + * @param text the initial text in the text field + */ + public TextField(String id, Mediator mediator, String text) { + this.id = id; + this.mediator = mediator; + this.text = text; + } + + /** + * Retrieves the ID of the text field. + * + * @return the ID of the text field + */ + public String getId() { + return id; + } + + /** + * Sends an update message to the associated Mediator. + * + * @param updateMessage the update message to be sent + */ + @Override + public void sendUpdate(String updateMessage) { + mediator.update(this, updateMessage); + } + + /** + * Receives an update message from the Mediator and prints a message. + * + * @param updateMessage the received update message + */ + @Override + public void receiveUpdate(String updateMessage) { + System.out.printf("%s has has received an update with a message:\"%s\"\n", id, updateMessage); + } +} diff --git a/src/labwork6/CartesianSystem.java b/src/labwork6/CartesianSystem.java new file mode 100644 index 0000000..766ae34 --- /dev/null +++ b/src/labwork6/CartesianSystem.java @@ -0,0 +1,17 @@ +package labwork6; + +/** + * The CartesianSystem class implements the CoordinateSystem interface and graphs equations in Cartesian coordinates. + */ +public class CartesianSystem implements CoordinateSystem { + + /** + * Graphs a mathematical equation in Cartesian coordinates. + * + * @param equation the equation to graph + */ + @Override + public void graph(Equation equation) { + System.out.println("Graphing " + equation.getExpression() + " in Cartesian coordinates."); + } +} diff --git a/src/labwork6/CoordinateSystem.java b/src/labwork6/CoordinateSystem.java new file mode 100644 index 0000000..d2f9ca8 --- /dev/null +++ b/src/labwork6/CoordinateSystem.java @@ -0,0 +1,13 @@ +package labwork6; + +/** + * The CoordinateSystem interface is a strategy that defines the contract for different coordinate systems to graph mathematical equations. + */ +public interface CoordinateSystem { + /** + * Graphs a mathematical equation in the coordinate system. + * + * @param equation the equation to graph + */ + void graph(Equation equation); +} \ No newline at end of file diff --git a/src/labwork6/Equation.java b/src/labwork6/Equation.java new file mode 100644 index 0000000..450118c --- /dev/null +++ b/src/labwork6/Equation.java @@ -0,0 +1,45 @@ +package labwork6; + +/** + * The Equation class represents a mathematical equation and associates it with a specific coordinate system for graphing. + */ +public class Equation { + private final String expression; + private CoordinateSystem coordinateSystem; + + /** + * Constructs an Equation with the specified expression and coordinate system. + * + * @param expression the mathematical expression to represent + * @param coordinateSystem the coordinate system to use for graphing + */ + public Equation(String expression, CoordinateSystem coordinateSystem) { + this.expression = expression; + this.coordinateSystem = coordinateSystem; + } + + /** + * Retrieves the mathematical expression associated with the equation. + * + * @return the mathematical expression + */ + public String getExpression() { + return expression; + } + + /** + * Sets the coordinate system for graphing the equation. + * + * @param coordinateSystem the coordinate system to use for graphing + */ + public void setCoordinateSystem(CoordinateSystem coordinateSystem) { + this.coordinateSystem = coordinateSystem; + } + + /** + * Displays the graph of the equation using the associated coordinate system. + */ + public void display() { + coordinateSystem.graph(this); + } +} diff --git a/src/labwork6/Main.java b/src/labwork6/Main.java new file mode 100644 index 0000000..11c4f36 --- /dev/null +++ b/src/labwork6/Main.java @@ -0,0 +1,16 @@ +package labwork6; + +public class Main { + public static void main(String[] args) { + CartesianSystem cartesianSystem = new CartesianSystem(); + PolarSystem polarSystem = new PolarSystem(); + + Equation equation = new Equation("(x - 1)^2 + (y - 1)^2 = 1", cartesianSystem); + equation.display(); + + equation.setCoordinateSystem(polarSystem); + equation.display(); + + + } +} diff --git a/src/labwork6/PolarSystem.java b/src/labwork6/PolarSystem.java new file mode 100644 index 0000000..cf65a41 --- /dev/null +++ b/src/labwork6/PolarSystem.java @@ -0,0 +1,17 @@ +package labwork6; + +/** + * The PolarSystem class implements the CoordinateSystem interface and graphs equations in polar coordinates. + */ +public class PolarSystem implements CoordinateSystem { + + /** + * Graphs a mathematical equation in polar coordinates. + * + * @param equation the equation to graph + */ + @Override + public void graph(Equation equation) { + System.out.println("Graphing " + equation.getExpression() + " in polar coordinates."); + } +} diff --git a/src/labwork7/Brush.java b/src/labwork7/Brush.java new file mode 100644 index 0000000..cf31935 --- /dev/null +++ b/src/labwork7/Brush.java @@ -0,0 +1,18 @@ +package labwork7; + +/** + * The Brush class is an implementation of the PressedButton interface representing a drawing brush. + * It defines behavior for left and right clicks, switching to the eraser on right-click. + */ +public class Brush implements PressedButton { + @Override + public void leftClick(Mouse mouse) { + System.out.println("Drawing freehand."); + } + + @Override + public void rightClick(Mouse mouse) { + System.out.println("Switching to Eraser."); + mouse.setPressedButton(new Eraser()); + } +} diff --git a/src/labwork7/Eraser.java b/src/labwork7/Eraser.java new file mode 100644 index 0000000..54307d8 --- /dev/null +++ b/src/labwork7/Eraser.java @@ -0,0 +1,18 @@ +package labwork7; + +/** + * The Eraser class is an implementation of the PressedButton interface representing an eraser. + * It defines behavior for left and right clicks, switching to the brush on right-click. + */ +public class Eraser implements PressedButton { + @Override + public void leftClick(Mouse mouse) { + System.out.println("Erasing freehand."); + } + + @Override + public void rightClick(Mouse mouse) { + System.out.println("Switching to Brush."); + mouse.setPressedButton(new Brush()); + } +} diff --git a/src/labwork7/Fill.java b/src/labwork7/Fill.java new file mode 100644 index 0000000..af58264 --- /dev/null +++ b/src/labwork7/Fill.java @@ -0,0 +1,18 @@ +package labwork7; + +/** + * The Fill class is an implementation of the PressedButton interface representing a fill tool. + * It defines behavior for left and right clicks, unpressing the button on right-click. + */ +public class Fill implements PressedButton { + @Override + public void leftClick(Mouse mouse) { + System.out.println("Filling in."); + } + + @Override + public void rightClick(Mouse mouse) { + System.out.println("Unpressing the button."); + mouse.setPressedButton(new Nothing()); + } +} diff --git a/src/labwork7/Main.java b/src/labwork7/Main.java new file mode 100644 index 0000000..abe706a --- /dev/null +++ b/src/labwork7/Main.java @@ -0,0 +1,19 @@ +package labwork7; + +public class Main { + + private static void clickTwo(Mouse mouse) { + mouse.leftClick(); + mouse.rightClick(); + } + + public static void main(String[] args) { + Mouse mouse = new Mouse(); + clickTwo(mouse); + mouse.setPressedButton(new Fill()); + clickTwo(mouse); + mouse.setPressedButton(new Brush()); + clickTwo(mouse); + clickTwo(mouse); + } +} diff --git a/src/labwork7/Mouse.java b/src/labwork7/Mouse.java new file mode 100644 index 0000000..463e9c9 --- /dev/null +++ b/src/labwork7/Mouse.java @@ -0,0 +1,48 @@ +package labwork7; + +/** + * The Mouse class represents a mouse device and maintains the current state of the pressed button. + * It provides methods for left and right clicks, delegating the behavior to the current pressed button state. + */ +public class Mouse { + private PressedButton pressedButton; + + /** + * Constructs a Mouse with an initial state of "Nothing" (no button pressed). + */ + public Mouse() { + this.pressedButton = new Nothing(); + } + + /** + * Retrieves the currently pressed button state. + * + * @return the current pressed button state + */ + public PressedButton getPressedButton() { + return pressedButton; + } + + /** + * Sets the pressed button state. + * + * @param pressedButton the new pressed button state + */ + public void setPressedButton(PressedButton pressedButton) { + this.pressedButton = pressedButton; + } + + /** + * Handles a left-click event, delegating the behavior to the current pressed button state. + */ + public void leftClick() { + pressedButton.leftClick(this); + } + + /** + * Handles a right-click event, delegating the behavior to the current pressed button state. + */ + public void rightClick() { + pressedButton.rightClick(this); + } +} diff --git a/src/labwork7/Nothing.java b/src/labwork7/Nothing.java new file mode 100644 index 0000000..aa1087e --- /dev/null +++ b/src/labwork7/Nothing.java @@ -0,0 +1,17 @@ +package labwork7; + +/** + * The Nothing class is an implementation of the PressedButton interface representing an inactive state. + * It defines behavior for left and right clicks, doing nothing in both cases. + */ +public class Nothing implements PressedButton { + @Override + public void leftClick(Mouse mouse) { + System.out.println("Doing nothing."); + } + + @Override + public void rightClick(Mouse mouse) { + System.out.println("Doing even more nothing."); + } +} diff --git a/src/labwork7/PressedButton.java b/src/labwork7/PressedButton.java new file mode 100644 index 0000000..0ba4018 --- /dev/null +++ b/src/labwork7/PressedButton.java @@ -0,0 +1,21 @@ +package labwork7; + +/** + * The PressedButton interface represents the state of a button when pressed. + * Different implementations define behavior for left and right clicks. + */ +public interface PressedButton { + /** + * Handles a left-click event on the mouse. + * + * @param mouse the Mouse instance associated with the button + */ + void leftClick(Mouse mouse); + + /** + * Handles a right-click event on the mouse. + * + * @param mouse the Mouse instance associated with the button + */ + void rightClick(Mouse mouse); +} diff --git a/src/labwork8/GameWorld.java b/src/labwork8/GameWorld.java new file mode 100644 index 0000000..d59c3d9 --- /dev/null +++ b/src/labwork8/GameWorld.java @@ -0,0 +1,67 @@ +package labwork8; + +import java.util.Arrays; + +/** + * The GameWorld class is an implementation of the GameWorldComponent and GameWorldPrototype interfaces. + * It represents a composite of GameWorldComponents and can create shallow and deep copies of itself as per Prototype pattern. + */ +public class GameWorld implements GameWorldComponent, GameWorldPrototype { + private GameWorldComponent[] gameWorldComponents; + private String name; + + /** + * Constructs a GameWorld with a specified name and an array of GameWorldComponents. + * + * @param name the name of the game world + * @param gameWorldComponents the components that make up the game world + */ + public GameWorld(String name, GameWorldComponent... gameWorldComponents) { + this.name = name; + this.gameWorldComponents = gameWorldComponents; + } + + /** + * Retrieves the array of GameWorldComponents in the game world. + * + * @return the array of GameWorldComponents + */ + public GameWorldComponent[] getGameWorldComponents() { + return gameWorldComponents; + } + + /** + * Retrieves the name of the game world. + * + * @return the name of the game world + */ + public String getName() { + return name; + } + + /** + * Creates a shallow copy of the GameWorld. + * + * @return a shallow copy of the GameWorld + */ + @Override + public GameWorld shallowCopy() { + return new GameWorld( + name, + gameWorldComponents + ); + } + + /** + * Creates a deep copy of the GameWorld. + * + * @return a deep copy of the GameWorld + */ + @Override + public GameWorld deepCopy() { + return new GameWorld( + new String(name), + Arrays.copyOf(gameWorldComponents, gameWorldComponents.length) + ); + } +} diff --git a/src/labwork8/GameWorldComponent.java b/src/labwork8/GameWorldComponent.java new file mode 100644 index 0000000..e6123fd --- /dev/null +++ b/src/labwork8/GameWorldComponent.java @@ -0,0 +1,7 @@ +package labwork8; + +/** + * The GameWorldComponent interface is a marker interface indicating that a class can be part of the GameWorld composite. + */ +public interface GameWorldComponent { +} diff --git a/src/labwork8/GameWorldPrototype.java b/src/labwork8/GameWorldPrototype.java new file mode 100644 index 0000000..6611a66 --- /dev/null +++ b/src/labwork8/GameWorldPrototype.java @@ -0,0 +1,20 @@ +package labwork8; + +/** + * The GameWorldPrototype interface declares methods for creating shallow and deep copies of a GameWorld. + */ +public interface GameWorldPrototype { + /** + * Creates a shallow copy of the GameWorld. + * + * @return a shallow copy of the GameWorld + */ + GameWorld shallowCopy(); + + /** + * Creates a deep copy of the GameWorld. + * + * @return a deep copy of the GameWorld + */ + GameWorld deepCopy(); +} diff --git a/src/labwork8/Location.java b/src/labwork8/Location.java new file mode 100644 index 0000000..cfb9f24 --- /dev/null +++ b/src/labwork8/Location.java @@ -0,0 +1,17 @@ +package labwork8; + +/** + * The Location class is an implementation of the GameWorldComponent interface representing a location in the game world. + */ +public class Location implements GameWorldComponent { + private String name; + + /** + * Constructs a Location with a specified name. + * + * @param name the name of the location + */ + public Location(String name) { + this.name = name; + } +} diff --git a/src/labwork8/Main.java b/src/labwork8/Main.java new file mode 100644 index 0000000..5747512 --- /dev/null +++ b/src/labwork8/Main.java @@ -0,0 +1,30 @@ +package labwork8; + +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + GameWorld golarion = new GameWorld("Golarion", new NPC("Ezren"), new Location("Absalom")); + + copyAndCheck(golarion); + } + + public static void copyAndCheck(GameWorld world) { + GameWorld shallowCopy = world.shallowCopy(); + GameWorld deepCopy = world.deepCopy(); + + System.out.println("Checking shallow copy."); + checkCopies(world, shallowCopy); + System.out.println(); + + System.out.println("Checking deep copy."); + checkCopies(world, deepCopy); + } + + public static void checkCopies(GameWorld world, GameWorld copy) { + System.out.println("Name equal by reference: " + (world.getName() == copy.getName())); + System.out.println("Name equal by value: " + world.getName().equals(copy.getName())); + System.out.println("Components equal by reference: " + (world.getGameWorldComponents() == copy.getGameWorldComponents())); + System.out.println("Components equal by value: " + Arrays.equals(world.getGameWorldComponents(), copy.getGameWorldComponents())); + } +} diff --git a/src/labwork8/NPC.java b/src/labwork8/NPC.java new file mode 100644 index 0000000..027dabe --- /dev/null +++ b/src/labwork8/NPC.java @@ -0,0 +1,17 @@ +package labwork8; + +/** + * The NPC class is an implementation of the GameWorldComponent interface representing a non-player character. + */ +public class NPC implements GameWorldComponent { + public String name; + + /** + * Constructs an NPC with a specified name. + * + * @param name the name of the NPC + */ + public NPC(String name) { + this.name = name; + } +} diff --git a/src/labwork9/App.java b/src/labwork9/App.java new file mode 100644 index 0000000..68aced8 --- /dev/null +++ b/src/labwork9/App.java @@ -0,0 +1,30 @@ +package labwork9; + +/** + * The App class represents an application that uses a GUIFactory to create a set of GUI elements. + */ +public class App { + private Button button; + private Canvas canvas; + private TextField textField; + + /** + * Constructs an App using the provided GUIFactory to create GUI elements. + * + * @param guiFactory the GUIFactory to create GUI elements + */ + public App(GUIFactory guiFactory) { + this.button = guiFactory.createButton(); + this.canvas = guiFactory.createCanvas(); + this.textField = guiFactory.createTextField(); + } + + /** + * Renders the GUI elements created by the App. + */ + public void render() { + button.render(); + canvas.render(); + textField.render(); + } +} diff --git a/src/labwork9/Button.java b/src/labwork9/Button.java new file mode 100644 index 0000000..310daac --- /dev/null +++ b/src/labwork9/Button.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The Button interface declares a method for rendering a button. + */ +public interface Button { + /** + * Renders the button. + */ + void render(); +} diff --git a/src/labwork9/Canvas.java b/src/labwork9/Canvas.java new file mode 100644 index 0000000..4f11c26 --- /dev/null +++ b/src/labwork9/Canvas.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The Canvas interface declares a method for rendering a canvas. + */ +public interface Canvas { + /** + * Renders the canvas. + */ + void render(); +} diff --git a/src/labwork9/GTKButton.java b/src/labwork9/GTKButton.java new file mode 100644 index 0000000..5bcb023 --- /dev/null +++ b/src/labwork9/GTKButton.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The GTKButton class is an implementation of the Button interface for GTK. + */ +public class GTKButton implements Button { + @Override + public void render() { + System.out.println("Created a Button with GTK."); + } +} diff --git a/src/labwork9/GTKCanvas.java b/src/labwork9/GTKCanvas.java new file mode 100644 index 0000000..48fb8e1 --- /dev/null +++ b/src/labwork9/GTKCanvas.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The GTKCanvas class is an implementation of the Canvas interface for GTK. + */ +public class GTKCanvas implements Canvas { + @Override + public void render() { + System.out.println("Created a Canvas with GTK."); + } +} diff --git a/src/labwork9/GTKFactory.java b/src/labwork9/GTKFactory.java new file mode 100644 index 0000000..a0d05b2 --- /dev/null +++ b/src/labwork9/GTKFactory.java @@ -0,0 +1,21 @@ +package labwork9; + +/** + * The GTKFactory class is an implementation of the GUIFactory interface for GTK. + */ +public class GTKFactory implements GUIFactory { + @Override + public Button createButton() { + return new GTKButton(); + } + + @Override + public Canvas createCanvas() { + return new GTKCanvas(); + } + + @Override + public TextField createTextField() { + return new GTKTextField(); + } +} diff --git a/src/labwork9/GTKTextField.java b/src/labwork9/GTKTextField.java new file mode 100644 index 0000000..516a096 --- /dev/null +++ b/src/labwork9/GTKTextField.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The GTKTextField class is an implementation of the TextField interface for GTK. + */ +public class GTKTextField implements TextField { + @Override + public void render() { + System.out.println("Created a Text Field with GTK."); + } +} diff --git a/src/labwork9/GUIFactory.java b/src/labwork9/GUIFactory.java new file mode 100644 index 0000000..b40c0fa --- /dev/null +++ b/src/labwork9/GUIFactory.java @@ -0,0 +1,27 @@ +package labwork9; + +/** + * The GUIFactory interface declares methods for creating buttons, canvases, and text fields. + */ +public interface GUIFactory { + /** + * Creates a button. + * + * @return the created button + */ + Button createButton(); + + /** + * Creates a canvas. + * + * @return the created canvas + */ + Canvas createCanvas(); + + /** + * Creates a text field. + * + * @return the created text field + */ + TextField createTextField(); +} diff --git a/src/labwork9/Main.java b/src/labwork9/Main.java new file mode 100644 index 0000000..394bcac --- /dev/null +++ b/src/labwork9/Main.java @@ -0,0 +1,34 @@ +package labwork9; + +/** + * The Main class configures the App based on the operating system and demonstrates the Abstract Factory pattern. + */ +public class Main { + /** + * Configures and creates an App based on the operating system. + * + * @return the configured App + */ + private static App configureApp() { + GUIFactory guiFactory; + + String osName = System.getProperty("os.name").toLowerCase(); + if (osName.contains("windows")) { + guiFactory = new WinAPIFactory(); + } else { + guiFactory = new GTKFactory(); + } + + return new App(guiFactory); + } + + /** + * The entry point of the program. + * + * @param args the command-line arguments + */ + public static void main(String[] args) { + App app = configureApp(); + app.render(); + } +} diff --git a/src/labwork9/TextField.java b/src/labwork9/TextField.java new file mode 100644 index 0000000..0c02873 --- /dev/null +++ b/src/labwork9/TextField.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The TextField interface declares a method for rendering a text field. + */ +public interface TextField { + /** + * Renders the text field. + */ + void render(); +} diff --git a/src/labwork9/WinAPIButton.java b/src/labwork9/WinAPIButton.java new file mode 100644 index 0000000..73d56aa --- /dev/null +++ b/src/labwork9/WinAPIButton.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The WinAPIButton class is an implementation of the Button interface for WinAPI. + */ +public class WinAPIButton implements Button { + @Override + public void render() { + System.out.println("Created a Button with WinAPI."); + } +} diff --git a/src/labwork9/WinAPICanvas.java b/src/labwork9/WinAPICanvas.java new file mode 100644 index 0000000..042fb13 --- /dev/null +++ b/src/labwork9/WinAPICanvas.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The WinAPICanvas class is an implementation of the Canvas interface for WinAPI. + */ +public class WinAPICanvas implements Canvas { + @Override + public void render() { + System.out.println("Created a Canvas with WinAPI."); + } +} diff --git a/src/labwork9/WinAPIFactory.java b/src/labwork9/WinAPIFactory.java new file mode 100644 index 0000000..939262d --- /dev/null +++ b/src/labwork9/WinAPIFactory.java @@ -0,0 +1,21 @@ +package labwork9; + +/** + * The WinAPIFactory class is an implementation of the GUIFactory interface for WinAPI. + */ +public class WinAPIFactory implements GUIFactory { + @Override + public Button createButton() { + return new WinAPIButton(); + } + + @Override + public Canvas createCanvas() { + return new WinAPICanvas(); + } + + @Override + public TextField createTextField() { + return new WinAPITextField(); + } +} diff --git a/src/labwork9/WinAPITextField.java b/src/labwork9/WinAPITextField.java new file mode 100644 index 0000000..3b25113 --- /dev/null +++ b/src/labwork9/WinAPITextField.java @@ -0,0 +1,11 @@ +package labwork9; + +/** + * The WinAPITextField class is an implementation of the TextField interface for WinAPI. + */ +public class WinAPITextField implements TextField { + @Override + public void render() { + System.out.println("Created a Text Field with WinAPI."); + } +}