diff --git a/Lesson2/HomeWork/Task1/Adapter.java b/Lesson2/HomeWork/Task1/Adapter.java new file mode 100644 index 0000000..c04b5d1 --- /dev/null +++ b/Lesson2/HomeWork/Task1/Adapter.java @@ -0,0 +1,36 @@ +package task1sensors; + +import java.time.LocalDateTime; + +public class Adapter implements MeteoSensor { + ST500Info sensor; + + public Adapter(ST500Info sensor) { + this.sensor = sensor; + } + + @Override + public int getId() { + return sensor.getData().identifier(); + } + + @Override + public Float getTemperature() { + return (float)sensor.getData().temperature(); + } + + @Override + public Float getHumidity() { + return 0f; + } + + @Override + public Float getPressure() { + return 0f; + } + + @Override + public LocalDateTime getDateTime() { + return LocalDateTime.now(); + } +} diff --git a/Lesson2/HomeWork/Task1/MS200.java b/Lesson2/HomeWork/Task1/MS200.java index 067d7b8..18745f0 100644 --- a/Lesson2/HomeWork/Task1/MS200.java +++ b/Lesson2/HomeWork/Task1/MS200.java @@ -1,25 +1,26 @@ +package task1sensors; import java.time.*; public class MS200 implements MeteoSensor{ - int id; - - public MS200(int id){ - this.id = id; - } - - public int getId(){ - return id; - } - public Float getTemperature(){ - return (float)20; - } - public Float getHumidity(){ - return (float)60; - } - public Float getPressure(){ - return (float)752.4; - } - public LocalDateTime getDateTime(){ - return LocalDateTime.now(); - } + int id; + + public MS200(int id){ + this.id = id; + } + + public int getId(){ + return id; + } + public Float getTemperature(){ + return (float)20; + } + public Float getHumidity(){ + return (float)60; + } + public Float getPressure(){ + return (float)752.4; + } + public LocalDateTime getDateTime(){ + return LocalDateTime.now(); + } } \ No newline at end of file diff --git a/Lesson2/HomeWork/Task1/MeteoSensor.java b/Lesson2/HomeWork/Task1/MeteoSensor.java index 0f3b0c4..fe4fd87 100644 --- a/Lesson2/HomeWork/Task1/MeteoSensor.java +++ b/Lesson2/HomeWork/Task1/MeteoSensor.java @@ -1,9 +1,11 @@ -import java.time.*; +package task1sensors; -public interface MeteoSensor{ - int getId(); // идентификатор датчика - Float getTemperature(); // температура датчика - Float getHumidity(); // влажность - Float getPressure(); // давление - LocalDateTime getDateTime(); // время чтения данных датчика -} \ No newline at end of file +import java.time.LocalDateTime; + +public interface MeteoSensor { + int getId(); // идентификатор датчика + Float getTemperature(); // температура датчика + Float getHumidity(); // влажность + Float getPressure(); // давление + LocalDateTime getDateTime(); // время чтения данных датчика +} diff --git a/Lesson2/HomeWork/Task1/MeteoStation.java b/Lesson2/HomeWork/Task1/MeteoStation.java index 822f885..72ac71d 100644 --- a/Lesson2/HomeWork/Task1/MeteoStation.java +++ b/Lesson2/HomeWork/Task1/MeteoStation.java @@ -1,10 +1,17 @@ -public class MeteoStation{ - public static void main(String[] args){ - MeteoStore meteoDb = new MeteoStore(); - - MeteoSensor ms200_1 = new MS200(1); - meteoDb.save(ms200_1); - - // Здесь надо вызвать метод getData у класса ST500Info. Полученные данные отправить в метод save объекта meteoDb - } -} \ No newline at end of file +package task1sensors; + +public class MeteoStation { + public static void main(String[] args){ + MeteoStore meteoDb = new MeteoStore(); + + MeteoSensor ms200_1 = new MS200(1); + ST500Info st500_2 = new ST500Info(); + + Adapter adapter = new Adapter(st500_2); + + meteoDb.save(ms200_1); + meteoDb.save(adapter); + + // Здесь надо вызвать метод getData у класса ST500Info. Полученные данные отправить в метод save объекта meteoDb + } +} diff --git a/Lesson2/HomeWork/Task1/MeteoStore.java b/Lesson2/HomeWork/Task1/MeteoStore.java index 09aca6e..91c4ad2 100644 --- a/Lesson2/HomeWork/Task1/MeteoStore.java +++ b/Lesson2/HomeWork/Task1/MeteoStore.java @@ -1,7 +1,8 @@ -class MeteoStore{ - // сохранение данных в базу - boolean save(MeteoSensor meteoSensor){ - System.out.format("Saving data from sensor [%d] at %s%n temperature - %f ; humidity - %f ; pressure - %f \n\r", meteoSensor.getId(), meteoSensor.getDateTime(), meteoSensor.getTemperature(), meteoSensor.getHumidity(), meteoSensor.getPressure()); - return true; - } -} \ No newline at end of file +package task1sensors; + +public class MeteoStore { + boolean save(MeteoSensor meteoSensor){ + System.out.format("Saving data from sensor [%d] at %s%n temperature - %f ; humidity - %f ; pressure - %f \n\r", meteoSensor.getId(), meteoSensor.getDateTime(), meteoSensor.getTemperature(), meteoSensor.getHumidity(), meteoSensor.getPressure()); + return true; + } +} diff --git a/Lesson2/HomeWork/Task1/ST500Info.java b/Lesson2/HomeWork/Task1/ST500Info.java index 72d2bff..965da0a 100644 --- a/Lesson2/HomeWork/Task1/ST500Info.java +++ b/Lesson2/HomeWork/Task1/ST500Info.java @@ -1,24 +1,26 @@ -import java.time.*; +package task1sensors; -class ST500Info{ - public SensorTemperature getData(){ - return new SensorTemperature(){ - public int identifier(){ - return 1; - } - public double temperature(){ - return 22.0; - } - public int year(){ - return LocalDateTime.now().getYear(); - } - public int day(){ - return LocalDateTime.now().getDayOfYear(); - } - public int second(){ - LocalDateTime now = LocalDateTime.now(); - return now.getHour()*3600 + now.getMinute()*60 + now.getSecond(); - } - }; - } -} \ No newline at end of file +import java.time.LocalDateTime; + +public class ST500Info { + public SensorTemperature getData(){ + return new SensorTemperature(){ + public int identifier(){ + return 1; + } + public double temperature(){ + return 22.0; + } + public int year(){ + return LocalDateTime.now().getYear(); + } + public int day(){ + return LocalDateTime.now().getDayOfYear(); + } + public int second(){ + LocalDateTime now = LocalDateTime.now(); + return now.getHour()*3600 + now.getMinute()*60 + now.getSecond(); + } + }; + } +} diff --git a/Lesson2/HomeWork/Task1/SensorTemperature.java b/Lesson2/HomeWork/Task1/SensorTemperature.java index 62d8a85..4acb502 100644 --- a/Lesson2/HomeWork/Task1/SensorTemperature.java +++ b/Lesson2/HomeWork/Task1/SensorTemperature.java @@ -1,8 +1,10 @@ -interface SensorTemperature{ - int identifier(); // идентификатор датчика - double temperature(); // температура датчика - int year(); // Год - int day(); // День года - int second(); // Секунда дня +package task1sensors; + +interface SensorTemperature { + int identifier(); // идентификатор датчика + double temperature(); // температура датчика + int year(); // Год + int day(); // День года + int second(); // Секунда дня + } - \ No newline at end of file diff --git a/Lesson2/HomeWork/Task2/PrintDecorator.java b/Lesson2/HomeWork/Task2/PrintDecorator.java new file mode 100644 index 0000000..71e238e --- /dev/null +++ b/Lesson2/HomeWork/Task2/PrintDecorator.java @@ -0,0 +1,19 @@ +package task2report_print; + +import java.util.List; + +public class PrintDecorator extends Report { + void showOnMonitor(){ + System.out.println("Output to monitor"); + for(ReportItem item : items){ + System.out.format("* %s - %f \n\r", item.getDescription(), item.getAmount()); + } + } + + void saveToFile(String file){ + System.out.println("Saving to file: " + file); + for(ReportItem item : items){ + System.out.format("%s - %f \n\r", item.getDescription(), item.getAmount()); + } + } +} diff --git a/Lesson2/HomeWork/Task2/PrintReport.java b/Lesson2/HomeWork/Task2/PrintReport.java index 0946320..af07237 100644 --- a/Lesson2/HomeWork/Task2/PrintReport.java +++ b/Lesson2/HomeWork/Task2/PrintReport.java @@ -1,9 +1,12 @@ -import java.util.*; -class PrintReport{ - public void output(List items){ - System.out.println("Output to printer"); - for(ReportItem item : items){ - System.out.format("printer %s - %f \n\r", item.getDescription(), item.getAmount()); - } - } -} \ No newline at end of file +package task2report_print; + +import java.util.List; + +public class PrintReport { + public void output(List items){ + System.out.println("Output to printer"); + for(ReportItem item : items){ + System.out.format("printer %s - %f \n\r", item.getDescription(), item.getAmount()); + } + } +} diff --git a/Lesson2/HomeWork/Task2/Programm.java b/Lesson2/HomeWork/Task2/Programm.java index e124be3..f278dad 100644 --- a/Lesson2/HomeWork/Task2/Programm.java +++ b/Lesson2/HomeWork/Task2/Programm.java @@ -1,7 +1,19 @@ -class Programm{ - public static void main(String[] args){ - Report report = new Report(); - report.calculate(); - report.output(); - } -} \ No newline at end of file +package task2report_print; + +import java.time.LocalDateTime; + +public class Programm { + public static void main(String[] args){ + + +// Report report = new Report(); +// report.calculate(); +// report.output(); + + PrintDecorator report = new PrintDecorator(); + report.calculate(); + report.output(); + report.saveToFile("C:/reports/"+ LocalDateTime.now().toString()+".txt"); + report.showOnMonitor(); + } +} diff --git a/Lesson2/HomeWork/Task2/Report.java b/Lesson2/HomeWork/Task2/Report.java index c6bf1ec..425a93d 100644 --- a/Lesson2/HomeWork/Task2/Report.java +++ b/Lesson2/HomeWork/Task2/Report.java @@ -1,16 +1,20 @@ -import java.util.*; -class Report{ - private List items; // Отчетные данные - - // расчет отчетных данных - public void calculate(){ - items = new ArrayList(); - items.add(new ReportItem("First", (float)5)); - items.add(new ReportItem("Second", (float)6)); - } - - public void output(){ - PrintReport reportPrint = new PrintReport(); - reportPrint.output(items); - } -} \ No newline at end of file +package task2report_print; + +import java.util.ArrayList; +import java.util.List; + +public class Report { + protected List items; // Отчетные данные + + // расчет отчетных данных + public void calculate(){ + items = new ArrayList(); + items.add(new ReportItem("First", (float)5)); + items.add(new ReportItem("Second", (float)6)); + } + + public void output(){ + PrintReport reportPrint = new PrintReport(); + reportPrint.output(items); + } +} diff --git a/Lesson2/HomeWork/Task2/ReportItem.java b/Lesson2/HomeWork/Task2/ReportItem.java index 3c5cc4d..290667b 100644 --- a/Lesson2/HomeWork/Task2/ReportItem.java +++ b/Lesson2/HomeWork/Task2/ReportItem.java @@ -1,15 +1,17 @@ -class ReportItem{ - private String description; - private float amount; - - public ReportItem(String description, float amount){ - this.description = description; - this.amount = amount; - } - public String getDescription(){ - return description; - } - public float getAmount(){ - return amount; - } -} \ No newline at end of file +package task2report_print; + +public class ReportItem { + private String description; + private float amount; + + public ReportItem(String description, float amount){ + this.description = description; + this.amount = amount; + } + public String getDescription(){ + return description; + } + public float getAmount(){ + return amount; + } +} diff --git a/Lesson3/Algorithms/homework/MainClass.java b/Lesson3/Algorithms/homework/MainClass.java new file mode 100644 index 0000000..4758f0b --- /dev/null +++ b/Lesson3/Algorithms/homework/MainClass.java @@ -0,0 +1,53 @@ +package lesson3; +import java.util.Arrays; + +public class MainClass { + + public static void main(String[] args) { + // Задача №1 + int[] unsorted = {9,3,6,8,4,2,3,9,56,3,2,67,9,4}; + AlghoritmUtils.insertSort(unsorted); + System.out.println(Arrays.toString(unsorted)); + + // Задача №2 + AlghoritmUtils.findFibonachi(20); + + } + +} + +class AlghoritmUtils { + + static void insertSort(int[] array){ + for (int i = 0; i < array.length;i++){ + for (int j = 0; j < array.length; j++) { + if (array[i] < array[j]){ + swapElements(array, j, i); + } + } + } + } + + private static void swapElements(int[] array, int j, int i) { + int tmp = array[j]; + array[j] = array[i]; + array[i] = tmp; + } + + // без сохранения, только вывод данныхб сложность ~O(n) + static void findFibonachi(int toNum){ + int prev = 0; + int curr = 1; + System.out.println(prev); + System.out.println(curr); + + int i = 2; + while (i <= toNum){ + int next = prev + curr; + System.out.println(next); + prev = curr; + curr = next; + i++; + } + } +}