mirror of
https://github.com/ASDjonok/OOP_IO-2x_2023.git
synced 2026-04-01 21:21:50 +03:00
Compare commits
7 Commits
ІО-23/30-Ш
...
ІО-24/12-З
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2874cce9b6 | ||
|
|
0ec39254b8 | ||
|
|
d7edabbf00 | ||
|
|
71b6763c9d | ||
|
|
33f8aa39e1 | ||
|
|
f70028323e | ||
|
|
bcb73f3a13 |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Project exclude paths
|
||||||
|
/out/
|
||||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="openjdk-18" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/OOP_IO-2x_2023.iml" filepath="$PROJECT_DIR$/OOP_IO-2x_2023.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
124
.idea/uiDesigner.xml
generated
Normal file
124
.idea/uiDesigner.xml
generated
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Palette2">
|
||||||
|
<group name="Swing">
|
||||||
|
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Button" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="RadioButton" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="CheckBox" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
|
||||||
|
<initial-values>
|
||||||
|
<property name="text" value="Label" />
|
||||||
|
</initial-values>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
|
||||||
|
<preferred-size width="150" height="-1" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
|
||||||
|
<preferred-size width="150" height="50" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
|
||||||
|
<preferred-size width="200" height="200" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
|
||||||
|
<preferred-size width="-1" height="20" />
|
||||||
|
</default-constraints>
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
|
||||||
|
</item>
|
||||||
|
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
|
||||||
|
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
|
||||||
|
</item>
|
||||||
|
</group>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package OOP.Java.lab_1;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class lab_1 {
|
|
||||||
|
|
||||||
public static int protectedInput(String variableToRead, Scanner input) {
|
|
||||||
do {
|
|
||||||
try {
|
|
||||||
System.out.printf("Enter %s: ", variableToRead);
|
|
||||||
return input.nextInt();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase());
|
|
||||||
input.nextLine();
|
|
||||||
}
|
|
||||||
} while (true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
Scanner input = new Scanner(System.in);
|
|
||||||
|
|
||||||
final int n = protectedInput("n", input);
|
|
||||||
final int m = protectedInput("m", input);
|
|
||||||
final int a = protectedInput("a", input);
|
|
||||||
final int b = protectedInput("b", input);
|
|
||||||
|
|
||||||
input.close();
|
|
||||||
|
|
||||||
final float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1);
|
|
||||||
|
|
||||||
System.out.println("S = " + s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package OOP.Java.lab_1
|
|
||||||
|
|
||||||
fun protectedInput(variableName: String): Int {
|
|
||||||
do {
|
|
||||||
try {
|
|
||||||
print("Enter $variableName: ")
|
|
||||||
return readln().toInt()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
println("${variableName.uppercase()} must be an integer!")
|
|
||||||
}
|
|
||||||
} while (true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val n: Int = protectedInput("n")
|
|
||||||
val m: Int = protectedInput("m")
|
|
||||||
val a: Int = protectedInput("a")
|
|
||||||
val b: Int = protectedInput("b")
|
|
||||||
|
|
||||||
val s: Float = (b + m).toFloat() / 2 * (m - b + 1) * (n - a + 1)
|
|
||||||
|
|
||||||
println("S = $s")
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
package OOP.Java.lab_2;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class lab_2 {
|
|
||||||
|
|
||||||
public static short protectedInput(String inputPrompt, String errorMessage, Scanner input) {
|
|
||||||
short read_variable;
|
|
||||||
|
|
||||||
do {
|
|
||||||
try {
|
|
||||||
System.out.println();
|
|
||||||
System.out.print(inputPrompt);
|
|
||||||
|
|
||||||
read_variable = input.nextShort();
|
|
||||||
break;
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(errorMessage);
|
|
||||||
input.nextLine();
|
|
||||||
}
|
|
||||||
} while (true);
|
|
||||||
|
|
||||||
return read_variable;
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public static String format(int number) {
|
|
||||||
int width = String.valueOf(number).length() + 1;
|
|
||||||
|
|
||||||
return "|%" + width + "d ";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(pure = true)
|
|
||||||
public static double average(@NotNull short[] row) {
|
|
||||||
short sum = 0;
|
|
||||||
|
|
||||||
for (short element : row) {
|
|
||||||
sum += element;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (double) sum / row.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
short rows, columns;
|
|
||||||
|
|
||||||
Scanner input = new Scanner(System.in);
|
|
||||||
|
|
||||||
final short a = protectedInput("Input a constant to multiply a matrix by: ",
|
|
||||||
"A constant must be a short-data type integer, try again.", input);
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Input size of the matrix.");
|
|
||||||
|
|
||||||
do {
|
|
||||||
rows = protectedInput("Rows: ",
|
|
||||||
"A number of rows must be a short-data type integer, try again.", input);
|
|
||||||
} while (rows <= 0);
|
|
||||||
|
|
||||||
do {
|
|
||||||
columns = protectedInput("Columns: ",
|
|
||||||
"A number of columns must be a short-data type integer, try again.", input);
|
|
||||||
} while (columns <= 0);
|
|
||||||
|
|
||||||
input.close();
|
|
||||||
|
|
||||||
short[][] matrix_B = new short[rows][columns];
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Matrix B:");
|
|
||||||
|
|
||||||
String format = format(rows * columns);
|
|
||||||
|
|
||||||
for (short i = 0; i < rows; i++) {
|
|
||||||
for (short j = 0; j < columns; j++) {
|
|
||||||
matrix_B[i][j] = (short) ((i + 1) * (j + 1));
|
|
||||||
|
|
||||||
System.out.printf(format, matrix_B[i][j]);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("|");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Matrix a×B:");
|
|
||||||
|
|
||||||
format = format(rows * columns * a);
|
|
||||||
|
|
||||||
for (short i = 0; i < matrix_B.length; i++) {
|
|
||||||
for (short j = 0; j < matrix_B[i].length; j++) {
|
|
||||||
matrix_B[i][j] *= (a);
|
|
||||||
|
|
||||||
System.out.printf(format, matrix_B[i][j]);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("|");
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println();
|
|
||||||
System.out.println("Averages of each row:");
|
|
||||||
|
|
||||||
for (short[] row : matrix_B) {
|
|
||||||
System.out.println(average(row));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package OOP.Java.lab_3;
|
|
||||||
|
|
||||||
public class lab_3 {
|
|
||||||
static int maxStrLength;
|
|
||||||
static String result;
|
|
||||||
|
|
||||||
public static void cSubUtil(StringBuilder stringbuilder, int leftBoundary, int rightBoundary) {
|
|
||||||
String string = stringbuilder.toString().toLowerCase().replaceAll("[^a-z]","");
|
|
||||||
// check if the indices lie in the range of string
|
|
||||||
// and also if it is palindrome
|
|
||||||
while (leftBoundary >= 0 && rightBoundary < string.length() && string.toLowerCase().charAt(leftBoundary) == string.toLowerCase().charAt(rightBoundary)) {
|
|
||||||
// expand the boundary
|
|
||||||
leftBoundary--;
|
|
||||||
rightBoundary++;
|
|
||||||
}
|
|
||||||
// if it's length is greater than maxStrLength update
|
|
||||||
// maxLength and result
|
|
||||||
if (rightBoundary - leftBoundary - 1 >= maxStrLength) {
|
|
||||||
result = string.substring(leftBoundary + 1, rightBoundary);
|
|
||||||
maxStrLength = rightBoundary - leftBoundary - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int longestPalSubstr(StringBuilder string)
|
|
||||||
{
|
|
||||||
result = "";
|
|
||||||
maxStrLength = 1;
|
|
||||||
// for every index in the string check palindromes
|
|
||||||
// starting from that index
|
|
||||||
for (int i = 0; i < string.length(); i++) {
|
|
||||||
// check for odd length palindromes
|
|
||||||
cSubUtil(string, i, i);
|
|
||||||
// check for even length palindromes
|
|
||||||
cSubUtil(string, i, i + 1);
|
|
||||||
}
|
|
||||||
System.out.println("Longest palindrome substring is: " + compareStrings(string));
|
|
||||||
return compareStrings(string).length();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String compareStrings(StringBuilder builder) {
|
|
||||||
for (int leftBoundary = 0; leftBoundary <= builder.length(); leftBoundary++) {
|
|
||||||
for (int rightBoundary = builder.length(); rightBoundary >= leftBoundary; rightBoundary--) {
|
|
||||||
if (result.equals(builder.substring(leftBoundary, rightBoundary).toLowerCase().replaceAll("[^a-z]",""))) {
|
|
||||||
return builder.substring(leftBoundary, rightBoundary);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
StringBuilder stringToDetect = new StringBuilder("Eva, can I see bees in a cave?");
|
|
||||||
|
|
||||||
System.out.println("Initial string: " + stringToDetect);
|
|
||||||
System.out.println("Length is: " + longestPalSubstr(stringToDetect));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package OOP.Java.lab_4
|
|
||||||
|
|
||||||
class Furniture(val name: String, val material: String, val price: Int, val length: Int, val width: Int, val height: Int) {
|
|
||||||
fun print(nameWidth: Int, materialWidth: Int, priceWidth: Int){
|
|
||||||
print("Furniture stats: {")
|
|
||||||
print("Name: ${this.name.padEnd(nameWidth)} ")
|
|
||||||
print("Material: ${this.material.padEnd(materialWidth)} ")
|
|
||||||
print("Price: ${(this.price.toString() + " cu").padEnd(priceWidth + 3)} ")
|
|
||||||
print("Size: ${this.length}×${this.width}×${this.height}};\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package OOP.Java.lab_4
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
val furnitureArray = arrayOf(
|
|
||||||
Furniture("Chair", "Wood", 10, 5, 5, 10),
|
|
||||||
Furniture("Counter-top", "Marble", 1_000, 10, 5, 1),
|
|
||||||
Furniture("Dinner table", "Glass", 500, 15, 10, 1),
|
|
||||||
Furniture("Office table", "Wood", 200, 10, 7, 1),
|
|
||||||
Furniture("Refrigerator", "Stainless steel", 20_000, 8, 4, 10)
|
|
||||||
)
|
|
||||||
|
|
||||||
val maxNameWidth = furnitureArray.maxWith(Comparator.comparingInt { it.name.length }).name.length
|
|
||||||
val maxMaterialWidth = furnitureArray.maxWith(Comparator.comparingInt { it.material.length }).material.length
|
|
||||||
val maxPriceWidth = furnitureArray.maxWith(Comparator.comparingInt { it.price }).price.toString().length
|
|
||||||
|
|
||||||
println("\nUnsorted array:")
|
|
||||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
|
||||||
|
|
||||||
println("\nSorted alphabetically by name:")
|
|
||||||
furnitureArray.sortBy { it.name }
|
|
||||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
|
||||||
|
|
||||||
println("\nSorted alphabetically by material:")
|
|
||||||
furnitureArray.sortBy { it.material }
|
|
||||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package OOP.Java.lab_5
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class representing a single letter in a word.
|
|
||||||
*
|
|
||||||
* @property character a character value of a Letter.
|
|
||||||
*/
|
|
||||||
class Letter(private val character: Char) {
|
|
||||||
override fun toString(): String {
|
|
||||||
return this.character.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates whether the two objects of class Letter are "equal".
|
|
||||||
*
|
|
||||||
* @param letter a letter to compare.
|
|
||||||
* @param ignoreCase if set to "true" will ignore the case of Letters compared.
|
|
||||||
* @return "true" if letters are equal and "false" if they aren't.
|
|
||||||
*/
|
|
||||||
fun letterEquals(letter: Letter, ignoreCase: Boolean): Boolean {
|
|
||||||
return this.character.toString().equals(letter.toString(), ignoreCase)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
package OOP.Java.lab_5
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class representing a punctuation mark following a word or a sentence.
|
|
||||||
*
|
|
||||||
* @property punctuationMark a [String] value of a punctuation mark.
|
|
||||||
*/
|
|
||||||
class Punctuation(var punctuationMark: String) {
|
|
||||||
|
|
||||||
init {
|
|
||||||
if (!"\\p{Punct}".toRegex().containsMatchIn(punctuationMark)) {
|
|
||||||
this.punctuationMark = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return punctuationMark
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package OOP.Java.lab_5
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class representing a single sentence is a text.
|
|
||||||
*
|
|
||||||
* @property sentenceArray a [Pair] of [Array]s of [Word] and [Punctuation] objects that make up a sentence.
|
|
||||||
*
|
|
||||||
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Word] and [Punctuation] objects, a secondary one accepts a string representing the entire sentence.
|
|
||||||
*/
|
|
||||||
class Sentence(var sentenceArray: Pair<Array<Word>, Array<Punctuation>>) {
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
sentenceString: String
|
|
||||||
) : this(
|
|
||||||
Pair(
|
|
||||||
sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray(),
|
|
||||||
sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an array of all [Letter] objects in a sentence.
|
|
||||||
*/
|
|
||||||
fun getAllLetters(): Array<Letter> {
|
|
||||||
var allLetters = arrayOf<Letter>()
|
|
||||||
this.sentenceArray.first.forEach { allLetters += it.letters }
|
|
||||||
return allLetters
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
var sentence = arrayOf<String>()
|
|
||||||
this.sentenceArray.first.indices.forEach {sentence += this.sentenceArray.first[it].toString() + this.sentenceArray.second[it].toString() }
|
|
||||||
return sentence.joinToString(" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package OOP.Java.lab_5
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class representing the entire text.
|
|
||||||
*
|
|
||||||
* @property textArray a [Pair] of [Array]s of [Sentence] and [Punctuation] objects that make up a text.
|
|
||||||
*
|
|
||||||
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Sentence] and [Punctuation] objects, a secondary one accepts a string representing the entire text.
|
|
||||||
*/
|
|
||||||
class Text(var textArray: Pair<Array<Sentence>, Array<Punctuation>>) {
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
textString: String
|
|
||||||
) : this(
|
|
||||||
Pair(
|
|
||||||
textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray(),
|
|
||||||
textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an array of all [Letter] objects in a sentence.
|
|
||||||
*/
|
|
||||||
private fun getAllLetters(): Array<Letter> {
|
|
||||||
var allLetters = arrayOf<Letter>()
|
|
||||||
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
|
||||||
return allLetters
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searches for the longest palindromic substring in a given text.
|
|
||||||
*
|
|
||||||
* @return the longest palindromic substring found.
|
|
||||||
*/
|
|
||||||
fun palindromeSearch(): String {
|
|
||||||
var result = " "
|
|
||||||
|
|
||||||
val letters = this.getAllLetters()
|
|
||||||
|
|
||||||
for (leftBoundary in letters.indices) {
|
|
||||||
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
|
|
||||||
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
|
|
||||||
|
|
||||||
if (subToC.first().letterEquals(subToC.last(), true) && this.checkReverse(subToC) && subToC.size > result.length) {
|
|
||||||
result = subToC.joinToString("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if a given substring is a palindrome.
|
|
||||||
*
|
|
||||||
* Is not case-sensitive.
|
|
||||||
*
|
|
||||||
* @param substring an [Array] of [Letter] objects representing a substring.
|
|
||||||
*
|
|
||||||
* @return "true" is a substring given is a palindrome, "false" if it isn't.
|
|
||||||
*/
|
|
||||||
private fun checkReverse(substring: Array<Letter>): Boolean {
|
|
||||||
var leftBoundary = 0
|
|
||||||
var rightBoundary = substring.lastIndex
|
|
||||||
var result = false
|
|
||||||
val correction = substring.size % 2
|
|
||||||
|
|
||||||
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
|
|
||||||
leftBoundary++
|
|
||||||
rightBoundary--
|
|
||||||
result = substring[leftBoundary].letterEquals(substring[rightBoundary], true)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
var text = arrayOf<String>()
|
|
||||||
this.textArray.first.indices.forEach { text += this.textArray.first[it].toString() + this.textArray.second[it].toString() }
|
|
||||||
return text.joinToString(" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package OOP.Java.lab_5
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A class representing a single word is a sentence.
|
|
||||||
*
|
|
||||||
* @property letters an array of [Letter] objects that make up a word.
|
|
||||||
*
|
|
||||||
* @constructor a primary constructor accepts an array of [Letter] objects, a secondary one accepts a string representing the entire word.
|
|
||||||
*/
|
|
||||||
class Word(var letters: Array<Letter>) {
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
val wordString = StringBuilder()
|
|
||||||
this.letters.forEach { wordString.append(it) }
|
|
||||||
|
|
||||||
return wordString.toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
word: String
|
|
||||||
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an album by a specific artist, containing multiple tracks.
|
|
||||||
*
|
|
||||||
* @property albumName The name of the album.
|
|
||||||
* @property artist The artist or band associated with the album.
|
|
||||||
* @property tracks An [Array] of tracks included on the album.
|
|
||||||
*/
|
|
||||||
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns an array of tracks within the specified duration range.
|
|
||||||
*/
|
|
||||||
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
|
|
||||||
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the total duration of all tracks on the album.
|
|
||||||
*/
|
|
||||||
fun totalDuration(): Duration {
|
|
||||||
var totalDuration: Duration = Duration.ZERO
|
|
||||||
this.tracks.forEach { totalDuration += it.duration }
|
|
||||||
return totalDuration
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a track in the Dance-pop style.
|
|
||||||
*
|
|
||||||
* @param numberInAlbum The position of the track on the album.
|
|
||||||
* @param trackName The name of the track.
|
|
||||||
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
|
||||||
* @param duration The [Duration] of the track.
|
|
||||||
*/
|
|
||||||
class Dancepop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Dance-pop"
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a track in the Electropop style.
|
|
||||||
*
|
|
||||||
* @param numberInAlbum The position of the track on the album.
|
|
||||||
* @param trackName The name of the track.
|
|
||||||
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
|
||||||
* @param duration The [Duration] of the track.
|
|
||||||
*/
|
|
||||||
class Electropop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Electropop"
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an interlude track.
|
|
||||||
*
|
|
||||||
* @param numberInAlbum The position of the track on the album.
|
|
||||||
* @param trackName The name of the track.
|
|
||||||
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
|
||||||
* @param duration The [Duration] of the track.
|
|
||||||
*/
|
|
||||||
class Interlude(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Interlude"
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a track in the Synth-pop style.
|
|
||||||
*
|
|
||||||
* @param numberInAlbum The position of the track on the album.
|
|
||||||
* @param trackName The name of the track.
|
|
||||||
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
|
||||||
* @param duration The [Duration] of the track.
|
|
||||||
*/
|
|
||||||
class Synthpop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Synth-pop"
|
|
||||||
}
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a track on an album.
|
|
||||||
*
|
|
||||||
* @param numberInAlbum The position of the track on the album.
|
|
||||||
* @param trackName The name of the track.
|
|
||||||
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
|
||||||
* @param duration The [Duration] of the track.
|
|
||||||
*/
|
|
||||||
open class Track(val numberInAlbum: Int, val trackName: String, val feature: String?, val duration: Duration) {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The style or genre of the track.
|
|
||||||
*/
|
|
||||||
open val style = ""
|
|
||||||
|
|
||||||
override fun toString(): String {
|
|
||||||
return if (this.feature == null) {
|
|
||||||
"#${this.numberInAlbum}: ${this.trackName} (${this.duration})"
|
|
||||||
} else {
|
|
||||||
"#${this.numberInAlbum}: ${this.trackName} ft. ${this.feature} (${this.duration})"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration.Companion.minutes
|
|
||||||
import kotlin.time.Duration.Companion.seconds
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
|
|
||||||
val chromatica = Album(
|
|
||||||
"Chromatica",
|
|
||||||
"Lady Gaga",
|
|
||||||
arrayOf(
|
|
||||||
Interlude(1, "Chromatica I", null, 1.minutes),
|
|
||||||
Synthpop(2, "Alice", null, 2.minutes + 57.seconds),
|
|
||||||
Dancepop(3, "Stupid Love", null, 3.minutes + 13.seconds),
|
|
||||||
Dancepop(4, "Rain On Me", "Ariana Grande", 3.minutes + 2.seconds),
|
|
||||||
Synthpop(5, "Free Woman", null, 3.minutes + 11.seconds),
|
|
||||||
Electropop(6, "Fun Tonight", null, 2.minutes + 53.seconds),
|
|
||||||
Interlude(7, "Chromatica II", null, 41.seconds),
|
|
||||||
Synthpop(8, "911", null, 2.minutes + 52.seconds),
|
|
||||||
Electropop(9, "Plastic Doll", null, 3.minutes + 41.seconds),
|
|
||||||
Dancepop(10, "Sour Candy", "Blackpink", 2.minutes + 37.seconds),
|
|
||||||
Dancepop(11, "Enigma", null, 2.minutes + 59.seconds),
|
|
||||||
Synthpop(12, "Replay", null, 3.minutes + 6.seconds),
|
|
||||||
Interlude(13, "Chromatica III", null, 27.seconds),
|
|
||||||
Electropop(14, "Sine From Above", "Elton John", 4.minutes + 4.seconds),
|
|
||||||
Synthpop(15, "1000 Doves", null, 3.minutes + 35.seconds),
|
|
||||||
Dancepop(16, "Babylon", null, 2.minutes + 41.seconds)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
println("$chromatica\n")
|
|
||||||
|
|
||||||
println("${chromatica.albumName} tracks sorted by musical style:\n${chromatica.tracks.sortedBy { it.style }.joinToString("\n")}\n")
|
|
||||||
|
|
||||||
val durationRange = 1.minutes.. 3.minutes + 30.seconds
|
|
||||||
|
|
||||||
println(
|
|
||||||
"${chromatica.albumName} tracks in a duration range ($durationRange):\n${chromatica.tracksInDurationRange(durationRange).joinToString("\n")}"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
11
OOP_IO-2x_2023.iml
Normal file
11
OOP_IO-2x_2023.iml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?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" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
16
Rust/lab_1/Cargo.lock
generated
16
Rust/lab_1/Cargo.lock
generated
@@ -1,16 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lab_1"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"text_io",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "text_io"
|
|
||||||
version = "0.1.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46"
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "lab_1"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
text_io = "0.1.12"
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
fn protected_read(variable_name: &str) -> i32 {
|
|
||||||
use text_io::try_read;
|
|
||||||
loop {
|
|
||||||
print!("Enter {}: ", variable_name);
|
|
||||||
let read_result: Result<i32, _> = try_read!();
|
|
||||||
match read_result {
|
|
||||||
Ok(read_integer) => return read_integer,
|
|
||||||
Err(_e) => println!("{} must be an integer!", variable_name.to_uppercase()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let n: i32 = protected_read("n");
|
|
||||||
let m: i32 = protected_read("m");
|
|
||||||
let a: i32 = protected_read("a");
|
|
||||||
let b: i32 = protected_read("b");
|
|
||||||
|
|
||||||
let s: f32 = ((b + m) as f32 / 2f32) * ((m - b + 1) * (n - a + 1)) as f32;
|
|
||||||
|
|
||||||
println!("S = {}", s);
|
|
||||||
}
|
|
||||||
16
Rust/lab_2/Cargo.lock
generated
16
Rust/lab_2/Cargo.lock
generated
@@ -1,16 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lab_2"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"text_io",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "text_io"
|
|
||||||
version = "0.1.12"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46"
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "lab_2"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
text_io = "0.1.12"
|
|
||||||
@@ -1,140 +0,0 @@
|
|||||||
// #[allow(dead_code)]
|
|
||||||
fn capitalise(s: &str) -> String {
|
|
||||||
let mut c = s.chars();
|
|
||||||
match c.next() {
|
|
||||||
None => String::new(),
|
|
||||||
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_row() -> Vec<i16> {
|
|
||||||
loop {
|
|
||||||
print!("Row (separated by spaces): ");
|
|
||||||
let raw_row: String = text_io::read!("{}\n");
|
|
||||||
let trimmed_row: &str = raw_row.trim();
|
|
||||||
match trimmed_row.split(" ").all(|value| {
|
|
||||||
value.chars().all(|char| char.is_numeric() || char == '-')
|
|
||||||
&& value.parse::<i32>().unwrap() <= i16::MAX as i32
|
|
||||||
&& value.parse::<i32>().unwrap() >= i16::MIN as i32
|
|
||||||
}) {
|
|
||||||
true => {
|
|
||||||
return trimmed_row
|
|
||||||
.split(" ")
|
|
||||||
.map(|value| value.parse::<i16>().unwrap())
|
|
||||||
.collect::<Vec<i16>>()
|
|
||||||
}
|
|
||||||
false => println!("[Error] The row must consist of short integers, try again."),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_row(row_index: u16, length: u16) -> Vec<i16> {
|
|
||||||
return (0..length)
|
|
||||||
.map(|element_index| (element_index as i16 + 1) * (row_index as i16 + 1))
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_matrix() -> Vec<Vec<i16>> {
|
|
||||||
let rows: u16 = protected_u16_read("the number of rows");
|
|
||||||
loop {
|
|
||||||
print!("Generate [s]quare or [r]ectangle matrix or [i]nput manually [s/r/i]: ");
|
|
||||||
let option: String = text_io::read!();
|
|
||||||
match option.to_lowercase().as_str() {
|
|
||||||
"s" => return (0..rows).map(|i| generate_row(i, rows)).collect(),
|
|
||||||
"r" => {
|
|
||||||
let columns: u16 = protected_u16_read("the number of columns");
|
|
||||||
return (0..rows).map(|i| generate_row(i, columns)).collect();
|
|
||||||
}
|
|
||||||
"i" => return (0..rows).map(|_| read_row()).collect(),
|
|
||||||
_ => println!("[Error] Not an option, try again."),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn protected_u16_read(variable_name: &str) -> u16 {
|
|
||||||
use text_io::try_read;
|
|
||||||
loop {
|
|
||||||
print!("Enter {}: ", variable_name);
|
|
||||||
let read_result: Result<u16, _> = try_read!();
|
|
||||||
match read_result {
|
|
||||||
Ok(read_integer) => return read_integer,
|
|
||||||
Err(_e) => println!(
|
|
||||||
"{} must be a short integer, try again.",
|
|
||||||
capitalise(variable_name)
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn multiply_matrix_by_number(matrix: &Vec<Vec<i16>>, multiplier: u16) -> Vec<Vec<i16>> {
|
|
||||||
return matrix
|
|
||||||
.iter()
|
|
||||||
.map(|row| {
|
|
||||||
row.iter()
|
|
||||||
.map(|element| element * multiplier as i16)
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn average_of_row(row: &Vec<i16>) -> f32 {
|
|
||||||
return row.iter().sum::<i16>() as f32 / row.len() as f32;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_matrix(matrix: &Vec<Vec<i16>>) -> String {
|
|
||||||
let max_width = matrix
|
|
||||||
.iter()
|
|
||||||
.map(|row| {
|
|
||||||
row.iter()
|
|
||||||
.map(|element| element.to_string().len())
|
|
||||||
.max()
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.max()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut formatted_matrix: String = matrix
|
|
||||||
.iter()
|
|
||||||
.map(|row| {
|
|
||||||
row.iter()
|
|
||||||
.map(|element| format!("| {:max_width$} ", element))
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join("")
|
|
||||||
})
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join("|\n");
|
|
||||||
formatted_matrix.push('|');
|
|
||||||
return formatted_matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn format_column(column: &Vec<f32>) -> String {
|
|
||||||
let max_width = column
|
|
||||||
.iter()
|
|
||||||
.map(|element| element.to_string().len())
|
|
||||||
.max()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
return column
|
|
||||||
.iter()
|
|
||||||
.map(|element| format!("| {:max_width$} |", element))
|
|
||||||
.collect::<Vec<String>>()
|
|
||||||
.join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let full_vec_test: Vec<Vec<i16>> = read_matrix();
|
|
||||||
println!("\nMatrix B:\n{}\n", format_matrix(&full_vec_test));
|
|
||||||
|
|
||||||
let mult_full_vec_test: Vec<Vec<i16>> =
|
|
||||||
multiply_matrix_by_number(&full_vec_test, protected_u16_read("matrix multiplier"));
|
|
||||||
println!("Matrix a×B:\n{}\n", format_matrix(&mult_full_vec_test));
|
|
||||||
|
|
||||||
let row_averages: Vec<f32> = mult_full_vec_test
|
|
||||||
.iter()
|
|
||||||
.map(|row| average_of_row(row))
|
|
||||||
.collect();
|
|
||||||
println!(
|
|
||||||
"Averages for each row in a×B:\n{}",
|
|
||||||
format_column(&row_averages)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
15
src/Main.java
Normal file
15
src/Main.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(args[2]);
|
||||||
|
System.out.println("Hello world!");
|
||||||
|
// System.out.println(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void myFunction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int myFunction2() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
30
src/_12_Закревський_Данило_Сергійович/lab1/FirstLab.java
Normal file
30
src/_12_Закревський_Данило_Сергійович/lab1/FirstLab.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class FirstLab {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
final int C = 0;
|
||||||
|
double S = 0.0;
|
||||||
|
System.out.print("Enter a: ");
|
||||||
|
int a = scn.nextInt();
|
||||||
|
System.out.print("Enter b: ");
|
||||||
|
int b = scn.nextInt();
|
||||||
|
System.out.print("Enter n: ");
|
||||||
|
int n = scn.nextInt();
|
||||||
|
System.out.print("Enter m: ");
|
||||||
|
int m = scn.nextInt();
|
||||||
|
if ((a <= 0 & n >= 0) | (b <= 0 & m >= 0)){
|
||||||
|
System.out.println("Division by 0!");
|
||||||
|
} else {
|
||||||
|
for (char i = (char) a; i <= n; i++) {
|
||||||
|
for(char j = (char) b; j <= m; j++) {
|
||||||
|
S += (double) (i % j) / (i + C);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.printf("Result - %.3f", S);
|
||||||
|
}
|
||||||
|
|
||||||
|
scn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
63
src/_12_Закревський_Данило_Сергійович/lab2/SecondLab.java
Normal file
63
src/_12_Закревський_Данило_Сергійович/lab2/SecondLab.java
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class SecondLab {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
Random rnd = new Random();
|
||||||
|
System.out.print("Enter number of rows in matrix A: ");
|
||||||
|
int mA = scn.nextInt();
|
||||||
|
System.out.print("Enter the number of columns in matrix A: ");
|
||||||
|
int nA = scn.nextInt();
|
||||||
|
System.out.print("Enter number of rows in matrix B: ");
|
||||||
|
int mB = scn.nextInt();
|
||||||
|
System.out.print("Enter the number of columns in matrix B: ");
|
||||||
|
int nB = scn.nextInt();
|
||||||
|
long largest, sum = 0L;
|
||||||
|
if(mA != mB || nA != nB) {
|
||||||
|
System.out.println("Unable to add matrices!");
|
||||||
|
} else {
|
||||||
|
long[][] matrixA = new long[mA][nA];
|
||||||
|
long[][] matrixB = new long[mB][nB];
|
||||||
|
long[][] matrixC = new long[mA][nA];
|
||||||
|
|
||||||
|
System.out.println("Matrix A: ");
|
||||||
|
for(int i = 0; i < mA; i++) {
|
||||||
|
for(int j = 0; j < nA; j++) {
|
||||||
|
matrixA[i][j] = rnd.nextLong(10, 50) + 10;
|
||||||
|
System.out.printf("|%d", matrixA[i][j]);
|
||||||
|
}
|
||||||
|
System.out.println("|");
|
||||||
|
}
|
||||||
|
System.out.println("Matrix B: ");
|
||||||
|
for(int i = 0; i < nA; i++) {
|
||||||
|
for(int j = 0; j < nB; j++) {
|
||||||
|
matrixB[i][j] = rnd.nextLong(10, 50) + 10;
|
||||||
|
System.out.printf("|%d", matrixB[i][j]);
|
||||||
|
}
|
||||||
|
System.out.println("|");
|
||||||
|
}
|
||||||
|
System.out.println("Matrix C: ");
|
||||||
|
for(int i = 0; i < mA; i++) {
|
||||||
|
for(int j = 0; j < nA; j++) {
|
||||||
|
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];
|
||||||
|
System.out.printf("|%d", matrixC[i][j]);
|
||||||
|
}
|
||||||
|
System.out.println("|");
|
||||||
|
}
|
||||||
|
for(long[] row: matrixC) {
|
||||||
|
largest = row[0];
|
||||||
|
for(long elem : row) {
|
||||||
|
if(elem > largest) {
|
||||||
|
largest = elem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sum += largest;
|
||||||
|
}
|
||||||
|
System.out.printf("Sum = %d", sum);
|
||||||
|
}
|
||||||
|
scn.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
33
src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java
Normal file
33
src/_12_Закревський_Данило_Сергійович/lab3/ThirdLab.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class ThirdLab {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
System.out.print("Enter a string: ");
|
||||||
|
String text = scn.nextLine();
|
||||||
|
StringBuilder textBuilder = new StringBuilder(text);
|
||||||
|
System.out.print("Enter a string to replace the words: ");
|
||||||
|
String replace = scn.nextLine();
|
||||||
|
System.out.print("Enter the length of the words to replace: ");
|
||||||
|
int length = scn.nextInt();
|
||||||
|
String[] words = textBuilder.toString().split("\\s+");
|
||||||
|
textBuilder.setLength(0);
|
||||||
|
for(String word : words) {
|
||||||
|
if(word.charAt(word.length()-1) == '.' && word.length() - 1 == length) {
|
||||||
|
textBuilder.append(replace + ". ");
|
||||||
|
} else if (word.charAt(word.length() - 1) == '!' && word.length() - 1 == length) {
|
||||||
|
textBuilder.append(replace + "! ");
|
||||||
|
} else if (word.charAt(word.length() - 1) == '?' && word.length() - 1 == length) {
|
||||||
|
textBuilder.append(replace + "? ");
|
||||||
|
} else if (word.length() == length) {
|
||||||
|
textBuilder.append(replace + " ");
|
||||||
|
} else {
|
||||||
|
textBuilder.append(word + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.printf("Result: %s", textBuilder);
|
||||||
|
scn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
59
src/_12_Закревський_Данило_Сергійович/lab4/Airplane.java
Normal file
59
src/_12_Закревський_Данило_Сергійович/lab4/Airplane.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class Airplane {
|
||||||
|
private String name;
|
||||||
|
private String type;
|
||||||
|
private int weight;
|
||||||
|
private int amountOfEngines;
|
||||||
|
private int year;
|
||||||
|
private String color;
|
||||||
|
private int capacity;
|
||||||
|
private int price;
|
||||||
|
|
||||||
|
|
||||||
|
public Airplane(String name, String type, int weight, int amountOfEngines, int year, String color, int capacity, int price) {
|
||||||
|
this.name = name;
|
||||||
|
this.type = type;
|
||||||
|
this.weight = weight;
|
||||||
|
this.amountOfEngines = amountOfEngines;
|
||||||
|
this.year = year;
|
||||||
|
this.color = color;
|
||||||
|
this.capacity = capacity;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getYear() {
|
||||||
|
return year;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Plane : " +
|
||||||
|
"name = " + name +
|
||||||
|
", type = " + type +
|
||||||
|
", weight = " + weight + " tons" +
|
||||||
|
", amount of engines = " + amountOfEngines +
|
||||||
|
", year = " + year +
|
||||||
|
", color = " + color +
|
||||||
|
", capacity = " + capacity +
|
||||||
|
", price = " + price + " millions dollars.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SortByPrice implements Comparator<Airplane> {
|
||||||
|
|
||||||
|
public int compare(Airplane a, Airplane b) {
|
||||||
|
return a.getPrice() - b.getPrice();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SortByYear implements Comparator<Airplane> {
|
||||||
|
|
||||||
|
public int compare(Airplane a, Airplane b) {
|
||||||
|
return a.getYear() - b.getYear();
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/_12_Закревський_Данило_Сергійович/lab4/FourthLab.java
Normal file
32
src/_12_Закревський_Данило_Сергійович/lab4/FourthLab.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class FourthLab {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final Airplane plane1 = new Airplane("Boeing 747", "passenger", 180, 4, 1970, "Lightblue", 467, 70);
|
||||||
|
final Airplane plane2 = new Airplane("Airbus A380", "passenger", 575, 4, 2003, "Orange", 850, 389);
|
||||||
|
final Airplane plane3 = new Airplane("General Dynamics F-16", "military", 9, 1, 1974, "Grey", 1, 45);
|
||||||
|
final Airplane plane4 = new Airplane("Boeing C-17 Globemaster III", "cargo", 130, 4, 1995, "Grey", 102, 340);
|
||||||
|
final Airplane plane5 = new Airplane("An-225 Mriya", "cargo", 285, 6, 1990, "White", 20, 500);
|
||||||
|
Airplane[] planes = {plane1, plane2, plane3, plane4, plane5};
|
||||||
|
|
||||||
|
System.out.println("List of planes: ");
|
||||||
|
for(Airplane plane : planes) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
Arrays.sort(planes, new SortByPrice());
|
||||||
|
System.out.println("Sorted by price: ");
|
||||||
|
for(Airplane plane : planes) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
Arrays.sort(planes, new SortByYear().reversed());
|
||||||
|
System.out.println("Sorted by year: ");
|
||||||
|
for(Airplane plane : planes) {
|
||||||
|
System.out.println(plane);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
139
src/_12_Закревський_Данило_Сергійович/lab5/FifthLab.java
Normal file
139
src/_12_Закревський_Данило_Сергійович/lab5/FifthLab.java
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class FifthLab {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
System.out.println("Enter a string: ");
|
||||||
|
String text = scn.nextLine();
|
||||||
|
StringBuilder textBuilder = new StringBuilder(text);
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
|
System.out.print("Enter a string to replace the words: ");
|
||||||
|
String replace = scn.nextLine();
|
||||||
|
System.out.print("Enter the length of the words to replace: ");
|
||||||
|
int length = scn.nextInt();
|
||||||
|
|
||||||
|
System.out.println("\nText before changes: ");
|
||||||
|
System.out.println(textBuilder);
|
||||||
|
|
||||||
|
|
||||||
|
String[] mySentence = textBuilder.toString().split("(?<=[!.?])\\s?");
|
||||||
|
for(String s: mySentence) {
|
||||||
|
StringBuilder sb = new StringBuilder(s);
|
||||||
|
Sentence sentence = new Sentence(sb, length, replace);
|
||||||
|
result.append(sentence.wordsSplit());
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\nText after changes: ");
|
||||||
|
System.out.println(result);
|
||||||
|
scn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Sentence {
|
||||||
|
|
||||||
|
private final StringBuilder sentence;
|
||||||
|
private final int length;
|
||||||
|
private final String replace;
|
||||||
|
|
||||||
|
public Sentence(StringBuilder sentence, int length, String replace) {
|
||||||
|
this.sentence = sentence;
|
||||||
|
this.length = length;
|
||||||
|
this.replace = replace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder wordsSplit() {
|
||||||
|
StringBuilder ReturnSentence = new StringBuilder();
|
||||||
|
String text = sentence.toString();
|
||||||
|
String[] words = text.split(" ");
|
||||||
|
for(String s: words) {
|
||||||
|
StringBuilder sb = new StringBuilder(s);
|
||||||
|
Word word = new Word(sb, length, replace);
|
||||||
|
ReturnSentence.append(word.lettersSplit()).append(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
return ReturnSentence;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Word {
|
||||||
|
|
||||||
|
private final StringBuilder word;
|
||||||
|
private final int length;
|
||||||
|
private final String replace;
|
||||||
|
|
||||||
|
public Word(StringBuilder word, int length, String replace) {
|
||||||
|
this.word = word;
|
||||||
|
this.length = length;
|
||||||
|
this.replace = replace;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder lettersSplit() {
|
||||||
|
StringBuilder ReturnWord = new StringBuilder();
|
||||||
|
String wordstr = word.toString();
|
||||||
|
int len = wordstr.length();
|
||||||
|
String[] letters = wordstr.split("");
|
||||||
|
String[] ReplaceLetters = replace.split("");
|
||||||
|
char last = wordstr.charAt(word.length() - 1);
|
||||||
|
if(last == '!' || last == '?' || last == '.') {
|
||||||
|
if(wordstr.charAt(word.length() - 2) == '.') {
|
||||||
|
len = wordstr.length() - 3;
|
||||||
|
} else {
|
||||||
|
len = wordstr.length() - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(len == length) {
|
||||||
|
for(String chr: ReplaceLetters) {
|
||||||
|
StringBuilder ltr = new StringBuilder(chr);
|
||||||
|
Letter letter = new Letter(ltr);
|
||||||
|
ReturnWord.append(letter.getLetter());
|
||||||
|
}
|
||||||
|
if(wordstr.length() - len == 1) {
|
||||||
|
ReturnWord.append(wordstr.charAt(wordstr.length() - 1));
|
||||||
|
} else if(wordstr.length() - len == 3) {
|
||||||
|
ReturnWord.append("...");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for(String chr: letters) {
|
||||||
|
StringBuilder ltr = new StringBuilder(chr);
|
||||||
|
if(chr.equals("!") || chr.equals("?") || chr.equals(".") || chr.equals(",") || chr.equals(":") || chr.equals(";")) {
|
||||||
|
Punctuation punctuation = new Punctuation(ltr);
|
||||||
|
ReturnWord.append(punctuation.getPunctuation());
|
||||||
|
} else {
|
||||||
|
Letter letter = new Letter(ltr);
|
||||||
|
ReturnWord.append(letter.getLetter());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ReturnWord;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Letter {
|
||||||
|
|
||||||
|
private final StringBuilder letter;
|
||||||
|
|
||||||
|
public Letter(StringBuilder letter) {
|
||||||
|
this.letter = letter;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder getLetter() {
|
||||||
|
return letter;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Punctuation {
|
||||||
|
|
||||||
|
private final StringBuilder punctuation;
|
||||||
|
|
||||||
|
public Punctuation(StringBuilder punctuation) {
|
||||||
|
this.punctuation = punctuation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringBuilder getPunctuation() {
|
||||||
|
return punctuation;
|
||||||
|
}
|
||||||
|
}
|
||||||
138
src/_12_Закревський_Данило_Сергійович/lab6/SixthLab.java
Normal file
138
src/_12_Закревський_Данило_Сергійович/lab6/SixthLab.java
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
public class SixthLab {
|
||||||
|
|
||||||
|
public static int sumOfPassangers(Train[] trains) {
|
||||||
|
int sum = 0;
|
||||||
|
for (Train train: trains) {
|
||||||
|
sum += train.getCapacity();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float sumOfLuggage(Train[] trains) {
|
||||||
|
float sum = 0.0f;
|
||||||
|
for (Train train: trains) {
|
||||||
|
sum += train.getLuggage();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scn = new Scanner(System.in);
|
||||||
|
Intercity intercity = new Intercity(2, "intercity", 57, 2.0f);
|
||||||
|
SV sv = new SV(3, "sv", 20, 1.3f);
|
||||||
|
Compartment compartment = new Compartment(2, "compartment", 40, 2.5f);
|
||||||
|
Plackart plackart = new Plackart(1, "plackart", 52, 1.8f);
|
||||||
|
Train[] trainCars = {intercity, sv, compartment, plackart};
|
||||||
|
|
||||||
|
System.out.println("List of train cars: ");
|
||||||
|
for (Train car: trainCars) {
|
||||||
|
System.out.println(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
Arrays.sort(trainCars, new sortByComfort());
|
||||||
|
System.out.println("\nSorted list by comfort level: ");
|
||||||
|
for (Train car: trainCars) {
|
||||||
|
System.out.println(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.printf("\n\nThe total number of passengers on the train - %d.\n", sumOfPassangers(trainCars));
|
||||||
|
System.out.printf("The total amount of train baggage - %.1f tons.", sumOfLuggage(trainCars));
|
||||||
|
|
||||||
|
System.out.println("\n\nFinding the train car by number of passengers");
|
||||||
|
System.out.print("Enter the lower number: ");
|
||||||
|
int lower = scn.nextInt();
|
||||||
|
System.out.print("Enter the extreme number: ");
|
||||||
|
int extreme = scn.nextInt();
|
||||||
|
boolean flag = false;
|
||||||
|
for (Train car: trainCars) {
|
||||||
|
int amount = car.getCapacity();
|
||||||
|
if(amount >= lower && amount <= extreme) {
|
||||||
|
System.out.println(car);
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!flag) {
|
||||||
|
System.out.println("There is no such train car");
|
||||||
|
}
|
||||||
|
scn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Train {
|
||||||
|
private int comfortLevel;
|
||||||
|
private String type;
|
||||||
|
private int capacity;
|
||||||
|
private float luggage;
|
||||||
|
|
||||||
|
public Train(int comfortLevel, String type, int capacity, float luggage) {
|
||||||
|
this.comfortLevel = comfortLevel;
|
||||||
|
this.type = type;
|
||||||
|
this.capacity = capacity;
|
||||||
|
this.luggage = luggage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getComfortLevel() {
|
||||||
|
return comfortLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCapacity() {
|
||||||
|
return capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getLuggage() {
|
||||||
|
return luggage;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Train : " +
|
||||||
|
" Type: " + type +
|
||||||
|
". Comfort level: " + comfortLevel +
|
||||||
|
". Capacity: " + capacity +
|
||||||
|
". Luggage: " + luggage + " tons.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class Intercity extends Train {
|
||||||
|
|
||||||
|
public Intercity(int comfortLevel, String type, int capacity, float luggage) {
|
||||||
|
super(comfortLevel, type, capacity, luggage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SV extends Train {
|
||||||
|
|
||||||
|
public SV(int comfortLevel, String type, int capacity, float luggage) {
|
||||||
|
super(comfortLevel, type, capacity, luggage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Compartment extends Train {
|
||||||
|
|
||||||
|
public Compartment(int comfortLevel, String type, int capacity, float luggage) {
|
||||||
|
super(comfortLevel, type, capacity, luggage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Plackart extends Train {
|
||||||
|
|
||||||
|
public Plackart(int comfortLevel, String type, int capacity, float luggage) {
|
||||||
|
super(comfortLevel, type, capacity, luggage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class sortByComfort implements Comparator<Train> {
|
||||||
|
|
||||||
|
public int compare(Train a, Train b){
|
||||||
|
return a.getComfortLevel() - b.getComfortLevel();
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/encapsulationInheritancePolymorphism/Encapsulation.java
Normal file
20
src/encapsulationInheritancePolymorphism/Encapsulation.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package encapsulationInheritancePolymorphism;
|
||||||
|
|
||||||
|
public class Encapsulation {
|
||||||
|
/*private*/ int field;
|
||||||
|
private int field2;
|
||||||
|
// int a = 1;
|
||||||
|
|
||||||
|
public void myMethodForTheField () {
|
||||||
|
System.out.println(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void myMethodForTheField2 () {
|
||||||
|
System.out.println(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void myMethodForTheFieldAndTheField2 () {
|
||||||
|
System.out.println(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package encapsulationInheritancePolymorphism;
|
||||||
|
|
||||||
|
public class EncapsulationInheritancePolymorphism {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
int a = 1;
|
||||||
|
// var b = 2;
|
||||||
|
// System.out.println(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/encapsulationInheritancePolymorphism/Main.java
Normal file
14
src/encapsulationInheritancePolymorphism/Main.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package encapsulationInheritancePolymorphism;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Student student = new Student();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//...
|
||||||
|
|
||||||
|
student.setFaculty("FPM", "MO-22");
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/encapsulationInheritancePolymorphism/Student.java
Normal file
18
src/encapsulationInheritancePolymorphism/Student.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package encapsulationInheritancePolymorphism;
|
||||||
|
|
||||||
|
public class Student {
|
||||||
|
private String name;
|
||||||
|
private String surname;
|
||||||
|
private String group;
|
||||||
|
private String faculty;
|
||||||
|
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFaculty(String faculty, String group) {
|
||||||
|
this.faculty = faculty;
|
||||||
|
this.group = group;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/test/A.java
Normal file
13
src/test/A.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
public class A {
|
||||||
|
/*private*/ int f/* = 3*/;
|
||||||
|
|
||||||
|
public int getF() {
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setF(int f) {
|
||||||
|
this.f = f;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/test/Main.java
Normal file
15
src/test/Main.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package test;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
A a = new A();
|
||||||
|
System.out.println(a.getF());
|
||||||
|
a.setF(1);
|
||||||
|
System.out.println(a.getF());
|
||||||
|
|
||||||
|
System.out.println(a.f);
|
||||||
|
|
||||||
|
int[] array = {1, 2, 1};
|
||||||
|
System.out.println(array.length);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user