Compare commits

..

No commits in common. "a4a3e09735e283276e47511352f07f3d85721207" and "8100129706de8cd553c5fa44217785b0f99ad8c5" have entirely different histories.

4 changed files with 74 additions and 54 deletions

3
.gitignore vendored
View File

@ -1,3 +0,0 @@
Java/Lab 1/Lab_1.class
.gitignore
Java/Lab 2/Lab_2.class

View File

@ -1,38 +1,63 @@
import java.util.Scanner; import java.util.Scanner;
public class Lab_1 { public class Lab_1 {
public static void main(String[] args) {
int n, m, a, b;
float s = 0;
public static int protectedInput(String variable_to_read, Scanner input) { Scanner input = new Scanner(System.in);
int read_variable;
do { do {
try { try {
System.out.printf("Enter %s: ", variable_to_read); System.out.print("Enter n: ");
read_variable = input.nextInt(); n = input.nextInt();
break; break;
} catch (Exception e) { } catch (Exception e) {
System.out.printf("%s must be an integer.\n", variable_to_read.toUpperCase()); System.out.println("N must be an integer.");
input.nextLine(); input.nextLine();
} }
} while (true); } while (true);
return read_variable;
}
public static void main(String[] args) { do {
int n, m, a, b; 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);
Scanner input = new Scanner(System.in); 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);
n = protectedInput("n", input); do {
try {
m = protectedInput("m", input); System.out.print("Enter b: ");
b = input.nextInt();
a = protectedInput("a", input); break;
} catch (Exception e) {
b = protectedInput("b", input); System.out.println("B must be an integer.");
input.nextLine();
}
} while (true);
input.close(); input.close();
float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1); for (int i = a; i <= n; i++) {
for (int j = b; j <= m; j++) {
s += j;
}
}
System.out.println("S = " + s); System.out.println("S = " + s);
} }

View File

@ -2,25 +2,6 @@ import java.util.Scanner;
public class Lab_2 { 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) { public static String format(int number) {
int width = String.valueOf(number).length() + 1; int width = String.valueOf(number).length() + 1;
String format = "|%" + width + "d "; String format = "|%" + width + "d ";
@ -43,27 +24,45 @@ public class Lab_2 {
public static void main(String[] args) { public static void main(String[] args) {
short a, rows = 0, columns = 0; short a, rows = 0, columns = 0;
String format; String format;
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
a = protectedInput("Input a constant to multipy a matrix by: ", do {
"A constant must be a short-data type integer, try again.", input); 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);
System.out.println(); System.out.println();
System.out.println("Input size of the matrix."); System.out.println("Input size of the matrix.");
do { do {
rows = protectedInput("Rows: ", try {
"A number of rows must be a short-data type integer, try again.", input); 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();
}
} while (rows <= 0); } while (rows <= 0);
do { do {
columns = protectedInput("Columns: ", try {
"A number of columns must be a short-data type integer, try again.", input); 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();
}
} while (columns <= 0); } while (columns <= 0);
input.close();
short[][] matrix_B = new short[rows][columns]; short[][] matrix_B = new short[rows][columns];
System.out.println(); System.out.println();
@ -86,8 +85,8 @@ public class Lab_2 {
format = format(rows * columns * a); format = format(rows * columns * a);
for (short i = 0; i < matrix_B.length; i++) { for (short i = 0; i < rows; i++) {
for (short j = 0; j < matrix_B[i].length; j++) { for (short j = 0; j < columns; j++) {
matrix_B[i][j] *= (short) (a); matrix_B[i][j] *= (short) (a);
System.out.printf(format, matrix_B[i][j]); System.out.printf(format, matrix_B[i][j]);
@ -102,5 +101,7 @@ public class Lab_2 {
for (short[] row : matrix_B) { for (short[] row : matrix_B) {
System.out.println(average(row)); System.out.println(average(row));
} }
input.close();
} }
} }

View File

@ -1,3 +0,0 @@
fn main() {
}