Compare commits

...

13 Commits

Author SHA1 Message Date
BadOfficer
531238a935 update lab5 2023-05-31 11:13:34 +03:00
BadOfficer
879d207e0d update lab5 2023-05-31 10:49:23 +03:00
BadOfficer
fc4a97f8ea update lab5 2023-05-29 21:42:19 +03:00
BadOfficer
e1258c2542 upload lab5 2023-05-28 01:50:50 +03:00
BadOfficer
a39494a9c8 upload lab4 2023-05-17 14:03:20 +03:00
BadOfficer
0c4e5d3e89 upload lab6 2023-05-15 19:05:21 +03:00
BadOfficer
5c75c2bef1 modified lab3 2023-05-15 19:04:50 +03:00
BadOfficer
4fce8c914c bug fixes in Lab1 2023-02-23 22:40:55 +02:00
BadOfficer
71baf63a21 Refactored Lab2 2023-02-22 01:02:45 +02:00
BadOfficer
0b16cf927b Added documentation for Lab4 2023-02-22 00:46:09 +02:00
BadOfficer
0ba52b8eaf Fixed typo in Lab2 2023-02-21 20:15:21 +02:00
BadOfficer
86e06c3a5a Added lab4 2023-02-17 01:51:36 +02:00
BadOfficer
dca4d9f449 Added lab1, lab2, lab3 2023-02-17 00:16:43 +02:00
18 changed files with 469 additions and 1 deletions

2
.idea/misc.xml generated
View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@@ -0,0 +1,43 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab1;
import java.util.Scanner;
public class Lab1 {
public static void main(String[] args) {
// O1 = +
// C = 2
// O2 = %
// i, j = short(-32.768 to 32.768)
final int c = 2;
double result = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a: ");
int a = scan.nextInt();// Entering a
System.out.print("Enter b: ");
int b = scan.nextInt();// Entering b
System.out.print("Enter n: ");
int n = scan.nextInt();// Entering n
System.out.print("Enter m: ");
int m = scan.nextInt();// Entering m
scan.close();
if (a <= -c && n >= -c || b <= 0 && m >= 0) {
System.out.println("Error! Division by zero");
return;
}
else if(a > n || b > m){
result = 0;
System.out.println(result);
return;
}
for (short i = (short) a; i <= n; i++) {
for (short j = (short) b; j <= m; j++) {
result += (double) (i % j) / (i + c); //Calculation of the result
}
}
System.out.println("\ns = " + result); // Outputting the result
}
}

View File

@@ -0,0 +1,84 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab2;
import java.util.Scanner;
public class Lab2 {
public static void main(String[] args) {
// C5 = 2
// C7 = 1
// C11 = 4
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of matrix rows: ");
int m = scan.nextInt();
System.out.print("Enter the number of matrix columns: ");
int n = scan.nextInt();
System.out.println("\nFilling matrix A:");
byte[][] matrixA = new byte[m][n]; // Creating MxN matrix A
scanMatrix(matrixA, scan);
System.out.println("\nMatrix A"); // Printing matrix A
printMatrix(matrixA);
System.out.println("\nFilling matrix B:");
byte[][] matrixB = new byte[m][n];// Creating MxN matrix B
scanMatrix(matrixB, scan);
scan.close();
System.out.println("\nMatrix B:");// Printing matrix B
printMatrix(matrixB);
System.out.println("\nMatrix C:");
byte[][] matrixC = new byte[m][n];// Creating matrix C
for (byte i = 0; i < m; i++) {
for (byte j = 0; j < n; j++) {
matrixC[i][j] = (byte) (matrixA[i][j] + matrixB[i][j]);
System.out.print(matrixC[i][j] + "\t ");
}
System.out.println();
}
byte evenSum = 0; // Sum for the largest numbers in even rows
byte oddSum = 0;// Sum for the smallest numbers in even rows
for (byte i = 0; i < matrixC.length; i++) {
byte largest = Byte.MIN_VALUE; // variable for largest element
byte smallest = Byte.MAX_VALUE;// variable for smallest element
for (byte j = 0; j < matrixC[i].length; j++) {
if (i % 2 == 0) {
smallest = (byte) Math.min(smallest, matrixC[i][j]);
} else {
largest = (byte) Math.max(largest, matrixC[i][j]);
}
}
if (i % 2 == 0) {
oddSum += smallest;
} else {
evenSum += largest;
}
}
System.out.println("\nSum of the biggest elements: " + evenSum);
System.out.println("Sum of the smallest elements: " + oddSum);
}
private static void scanMatrix(byte[][] matrix, Scanner scan) { //Method for filling matrix
for (byte i = 0; i < matrix.length; i++) {
for (byte j = 0; j < matrix[i].length; j++) {
System.out.printf("Enter element [%d][%d]", i, i);
matrix[i][j] = scan.nextByte();
}
}
}
private static void printMatrix(byte[][] matrix) {//Method for printing matrix
for (byte[] row : matrix) {
for (byte el : row) {
System.out.print(el + "\t ");
}
System.out.println();
}
}
}

View File

@@ -0,0 +1,25 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab3;
import java.util.*;
public class Lab3 {
// String
// Надрукувати слова без повторень заданого тексту в алфавітному порядку за першою літерою.
public static void main(String[] args) {
String text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mauris a diam maecenas sed enim ut sem viverra. Amet est placerat in egestas erat imperdiet. Sed turpis tincidunt id aliquet risus. Amet porttitor eget dolor morbi non arcu risus quis. Elementum nibh tellus molestie nunc non blandit massa. Feugiat scelerisque varius morbi enim nunc faucibus. Ipsum faucibus vitae aliquet nec ullamcorper sit amet risus nullam. Quis enim lobortis scelerisque fermentum dui faucibus in. Sem viverra aliquet eget sit amet tellus cras adipiscing enim. Sed ullamcorper morbi tincidunt ornare. Sodales ut eu sem integer vitae justo eget magna. Mi ipsum faucibus vitae aliquet nec ullamcorper.";
String[] words = text.split("[\\p{Punct}\\s]+");
List<String> wordList = new ArrayList<>(Arrays.asList(words));//Class ArrayList implements interface List
// asList returns immutable list
Set<String> wordSet = new HashSet<>(wordList);// Class HashSet implements interface Set
wordList.clear();
wordList.addAll(wordSet);
wordList.sort(String::compareToIgnoreCase);
wordList.sort((o1, o2) -> o1.compareToIgnoreCase(o2));
for (String element : wordList) {
System.out.println(element);
}
}
}

View File

@@ -0,0 +1,34 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab4;
/**
* Class that represents a boat with different characteristics.
*/
public class Boat {
private final String name;
private final int price;
private final int mass;
private final int age;
private final String type;
public Boat(String name, int price, int mass, int age, String type) {
this.name = name;
this.price = price;
this.mass = mass;
this.age = age;
this.type = type;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
@Override
public String toString() {
return String.format("%s - %s, which was built %d years ago, with mass - %d kg, has price - %d$;",
type, name, age, mass, price);
}
}

View File

@@ -0,0 +1,35 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab4;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Lab4 {
public static void main(String[] args) {
List<Boat> boatList = Arrays.asList(
new Boat("Row V. Wave", 150000, 50000, 25, "cutter"),
new Boat("Dirty Oar", 250000, 40000, 15, "cutter"),
new Boat("Tumeric", 450000, 100000, 45, "cutter"),
new Boat("Life is Good", 350000, 20000, 5, "cutter")
);
boatList.sort(Comparator.comparing(Boat::getName));// comparing - method of Comparator interface
System.out.println("Sorted by name:");
printed(boatList);
boatList.sort(Comparator.comparing(Boat::getPrice).reversed());
System.out.println("\nSorted by price in reverse order:");
printed(boatList);
}
/**
* Print all characteristics of boats.
*
* @param boatList List of boats.
*/
public static void printed(List<Boat> boatList) {
for (Boat boat : boatList) {
System.out.println(boat);
}
}
}

View File

@@ -0,0 +1,12 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
import java.util.*;
public class Lab5 {
public static void main(String[] args) {
Text text = new Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Mauris a diam maecenas sed enim ut sem viverra. Amet est placerat in egestas erat imperdiet. Sed turpis tincidunt id aliquet risus. Amet porttitor eget dolor morbi non arcu risus quis. Elementum nibh tellus molestie nunc non blandit massa. Feugiat scelerisque varius morbi enim nunc faucibus. Ipsum faucibus vitae aliquet nec ullamcorper sit amet risus nullam. Quis enim lobortis scelerisque fermentum dui faucibus in. Sem viverra aliquet eget sit amet tellus cras adipiscing enim. Sed ullamcorper morbi tincidunt ornare. Sodales ut eu sem integer vitae justo eget magna. Mi ipsum faucibus vitae aliquet nec ullamcorper.");
System.out.println(text);
text.getSortedWordsByFirstLetter();
}
}

View File

@@ -0,0 +1,14 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
public class Letter {
private final char character;
public Letter(char character) {
this.character = character;
}
@Override
public String toString() {
return String.valueOf(character);
}
}

View File

@@ -0,0 +1,14 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
public class Punctuation implements SentenceElement {
private final String character;
public Punctuation(String character) {
this.character = character;
}
@Override
public String toString() {
return character;
}
}

View File

@@ -0,0 +1,55 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
import java.util.Arrays;
import java.util.HashSet;
public class Sentence {
private final SentenceElement[] sentenceElements;
private static final String PUNCTUATION = "\\p{Punct}";
public Sentence(String sentences) {
String[] sentenceElement = sentences.split("(?=" + PUNCTUATION + ")| ");
sentenceElements = new SentenceElement[sentenceElement.length];
for (int i = 0; i < sentenceElement.length; i++) {
if (sentenceElement[i].matches(PUNCTUATION)) {
sentenceElements[i] = new Punctuation(sentenceElement[i]);
} else {
sentenceElements[i] = new Word(sentenceElement[i]);
}
}
}
public static void getSortedWordsByFirstLetter(Sentence[] sentences) {
StringBuilder allWords = new StringBuilder();
for (Sentence sentence : sentences) {
allWords.append(sentence).append(" ");
}
String[] words = allWords.toString().split("\\W+");
HashSet<String> uniqueWords = new HashSet<>();
for (String word : words) {
uniqueWords.add(word.toLowerCase());
}
String[] uniqueSortedWords = uniqueWords.toArray(new String[0]);
Arrays.sort(uniqueSortedWords);
for (String word : uniqueSortedWords) {
System.out.println(word);
}
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for (SentenceElement se : sentenceElements) {
if (se.getClass().isAssignableFrom(Word.class)) {
out.append(" ");
}
out.append(se);
}
return out.toString();
}
}

View File

@@ -0,0 +1,5 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
public interface SentenceElement {
// Об'єднуєм два типа
}

View File

@@ -0,0 +1,26 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
public class Text {
private final Sentence[] sentences;
public Text(String text) {
String[] splitText = text.split("(?=[.!?]+)| ");
sentences = new Sentence[splitText.length];
for (int i = 0; i < splitText.length; i++) {
sentences[i] = new Sentence(splitText[i]);
}
}
public void getSortedWordsByFirstLetter() {
Sentence.getSortedWordsByFirstLetter(sentences);
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for (Sentence sentence : sentences) {
out.append(sentence.toString());
}
return out.toString().strip();
}
}

View File

@@ -0,0 +1,21 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab5;
public class Word implements SentenceElement {
private final Letter[] letters;
public Word(String word) {
letters = new Letter[word.length()];
for (int i = 0; i < word.length(); i++) {
letters[i] = new Letter(word.charAt(i));
}
}
@Override
public String toString() {
StringBuilder out = new StringBuilder();
for (Letter let : letters) {
out.append(let.toString());
}
return out.toString();
}
}

View File

@@ -0,0 +1,7 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab6;
public class Comfort extends Tariff {
public Comfort(String name, int price, int customers) {
super(name, price, customers);
}
}

View File

@@ -0,0 +1,8 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab6;
public class Economy extends Tariff {
public Economy(String name, int price, int customers) {
super(name, price, customers);
}
}

View File

@@ -0,0 +1,48 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab6;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Lab6 {
public static void main(String[] args) {
Comfort tariff1 = new Comfort("Comfort", 100, 5000);
Economy tariff2 = new Economy("Economy", 25, 10000);
Optimal tariff3 = new Optimal("Optimal", 300, 50000);
Tariff[] tariffs = {tariff1, tariff2, tariff3};
int sum = 0;
System.out.println("Тарифи мобільної мобільної компанії:");
for (int i = 0; i < tariffs.length; i++) {
System.out.printf("\t%d. %s%n", i + 1, tariffs[i].getName());
sum += tariffs[i].getCustomers();
}
System.out.println("\nЗагальна кількість користувачів: " + sum + ";");
System.out.println("\nТарифи мобільної компанії відсортовані за вартістю:");
Arrays.sort(tariffs, Comparator.comparing(Tariff::getPrice));
for (int i = 0; i < tariffs.length; i++) {
System.out.printf("\t%d. %s%n", i + 1, tariffs[i]);
}
System.out.println("\nВведіть діапазон цін, в якому бажаєте підібрати тариф: ");
Scanner scan = new Scanner(System.in);
System.out.print("\tМінімальна ціна: ");
int minSum = scan.nextInt();
System.out.print("\tМаксимальна ціна: ");
int maxSum = scan.nextInt();
scan.close();
int n2 = 0;
System.out.println("\ідібрані тарифи: ");
for (int i = 0; i < tariffs.length; i++) {
if (minSum <= tariffs[i].getPrice() && tariffs[i].getPrice() <= maxSum) {
System.out.printf("\t%d. %s%n", i + 1, tariffs[i]);
n2 += 1;
}
}
if (n2 == 0) {
System.out.println("\tНе знайдено тарифів в заданому діапазоні цін;");
}
}
}

View File

@@ -0,0 +1,7 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab6;
public class Optimal extends Tariff {
public Optimal(String name, int price, int customers) {
super(name, price, customers);
}
}

View File

@@ -0,0 +1,30 @@
package IO_24._02_Бондаренко_Тарас_Андрійович.lab6;
public class Tariff {
private final String name;
private final int price;
private final int customers;
public Tariff(String name, int price, int customers) {
this.name = name;
this.price = price;
this.customers = customers;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getCustomers() {
return customers;
}
@Override
public String toString() {
return "Тариф " + name + ", коштує " + price + " грн.";
}
}