From 8013099c3608a00ce7ca4cd89392964a93820974 Mon Sep 17 00:00:00 2001 From: Rhinemann Date: Wed, 3 May 2023 10:32:23 +0300 Subject: [PATCH] All sorts of corrections. --- .gitignore | 3 ++ Java/Lab 1/Lab_1.java | 69 ++++++++++++++----------------------------- Java/Lab 2/Lab_2.java | 60 ++++++++++++++++++------------------- Rust/Lab 2/Lab_2.rs | 3 ++ 4 files changed, 56 insertions(+), 79 deletions(-) create mode 100644 Rust/Lab 2/Lab_2.rs diff --git a/.gitignore b/.gitignore index e69de29..c9ba2e5 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,3 @@ +Java/Lab 1/Lab_1.class +.gitignore +Java/Lab 2/Lab_2.class diff --git a/Java/Lab 1/Lab_1.java b/Java/Lab 1/Lab_1.java index c0c7286..6f3b6db 100644 --- a/Java/Lab 1/Lab_1.java +++ b/Java/Lab 1/Lab_1.java @@ -1,63 +1,38 @@ import java.util.Scanner; public class Lab_1 { + + public static int protectedInput(String variable_to_read, Scanner input) { + int read_variable; + do { + try { + System.out.printf("Enter %s: ", variable_to_read); + read_variable = input.nextInt(); + break; + } catch (Exception e) { + System.out.printf("%s must be an integer.\n", variable_to_read.toUpperCase()); + input.nextLine(); + } + } while (true); + return read_variable; + } + public static void main(String[] args) { int n, m, a, b; - float s = 0; Scanner input = new Scanner(System.in); - - do { - try { - System.out.print("Enter n: "); - n = input.nextInt(); - break; - } catch (Exception e) { - System.out.println("N must be an integer."); - input.nextLine(); - } - } while (true); - do { - try { - System.out.print("Enter m: "); - m = input.nextInt(); - break; - } catch (Exception e) { - System.out.println("M must be an integer."); - input.nextLine(); - } - } while (true); + n = protectedInput("n", input); - do { - try { - System.out.print("Enter a: "); - a = input.nextInt(); - break; - } catch (Exception e) { - System.out.println("A must be an integer."); - input.nextLine(); - } - } while (true); + m = protectedInput("m", input); - do { - try { - System.out.print("Enter b: "); - b = input.nextInt(); - break; - } catch (Exception e) { - System.out.println("B must be an integer."); - input.nextLine(); - } - } while (true); + a = protectedInput("a", input); + + b = protectedInput("b", input); input.close(); - for (int i = a; i <= n; i++) { - for (int j = b; j <= m; j++) { - s += j; - } - } + float s = ((float)(b + m) / 2) * (m - b + 1) * (n - a + 1); System.out.println("S = " + s); } diff --git a/Java/Lab 2/Lab_2.java b/Java/Lab 2/Lab_2.java index a999443..8e6c59d 100644 --- a/Java/Lab 2/Lab_2.java +++ b/Java/Lab 2/Lab_2.java @@ -2,6 +2,25 @@ 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; + } + public static String format(int number) { int width = String.valueOf(number).length() + 1; String format = "|%" + width + "d "; @@ -27,41 +46,20 @@ public class Lab_2 { Scanner input = new Scanner(System.in); - do { - try { - System.out.println(); - System.out.print("Input a constant to multipy a matrix by: "); - - a = input.nextShort(); - break; - } catch (Exception e) { - System.out.println("A constant must be a short-data type integer, try again."); - input.nextLine(); - } - } while (true); - + a = protectedInput("Input a constant to multipy 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 { - try { - System.out.print("Rows: "); - rows = input.nextShort(); - } catch (Exception e) { - System.out.println("A number of rows must be a short-data type integer, try again."); - input.nextLine(); - } + rows = protectedInput("Rows: ", "A number of rows must be a short-data type integer, try again.", input); } while (rows <= 0); - + do { - try { - System.out.print("Columns: "); - columns = input.nextShort(); - } catch (Exception e) { - System.out.println("A number of columns must be a short-data type integer, try again."); - input.nextLine(); - } + 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]; @@ -85,8 +83,8 @@ public class Lab_2 { format = format(rows * columns * a); - for (short i = 0; i < rows; i++) { - for (short j = 0; j < columns; j++) { + for (short i = 0; i < matrix_B.length; i++) { + for (short j = 0; j < matrix_B[i].length; j++) { matrix_B[i][j] *= (short) (a); System.out.printf(format, matrix_B[i][j]); @@ -101,7 +99,5 @@ public class Lab_2 { for (short[] row : matrix_B) { System.out.println(average(row)); } - - input.close(); } } diff --git a/Rust/Lab 2/Lab_2.rs b/Rust/Lab 2/Lab_2.rs new file mode 100644 index 0000000..8a55e80 --- /dev/null +++ b/Rust/Lab 2/Lab_2.rs @@ -0,0 +1,3 @@ +fn main() { + +} \ No newline at end of file