Initial commit
This commit is contained in:
parent
7795ae537c
commit
b7332ec094
|
@ -0,0 +1,45 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/24.0.0/annotations-24.0.0.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="JUnit4">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="testng">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/testng/testng/7.1.0/testng-7.1.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/beust/jcommander/1.72/jcommander-1.72.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/google/inject/guice/4.1.0/guice-4.1.0-no_aop.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/javax/inject/javax.inject/1/javax.inject-1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/aopalliance/aopalliance/1.0/aopalliance-1.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/com/google/guava/guava/19.0/guava-19.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/yaml/snakeyaml/1.21/snakeyaml-1.21.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
</component>
|
||||||
|
</module>
|
|
@ -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<String> 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<String> 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<String> getWeatherDataHistory() {
|
||||||
|
checkExists();
|
||||||
|
|
||||||
|
observer.notify();
|
||||||
|
return weatherStation.getWeatherDataHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public WeatherIterator iterator() {
|
||||||
|
return new WeatherIterator(this.getWeatherDataHistory());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class WeatherObserver {
|
||||||
|
private WeatherProxy weatherStation;
|
||||||
|
private List<Object> 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<String> weatherDataHistory;
|
||||||
|
private int currentPosition = 0;
|
||||||
|
|
||||||
|
public WeatherIterator(List<String> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
package MKR2;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class SimpleList<E> implements Iterable<E> {
|
||||||
|
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<E> iterator() {
|
||||||
|
return new Iterator<E>() {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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<Task> tasks;
|
||||||
|
private List<User> 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<Task> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<InteractiveElement> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
|
@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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()));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package labwork9;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Button interface declares a method for rendering a button.
|
||||||
|
*/
|
||||||
|
public interface Button {
|
||||||
|
/**
|
||||||
|
* Renders the button.
|
||||||
|
*/
|
||||||
|
void render();
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
package labwork9;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Canvas interface declares a method for rendering a canvas.
|
||||||
|
*/
|
||||||
|
public interface Canvas {
|
||||||
|
/**
|
||||||
|
* Renders the canvas.
|
||||||
|
*/
|
||||||
|
void render();
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue