Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 37f8e9dbf6 | |||
| f2703fb43f | |||
| 090c8dfc28 | |||
| 3f15c4c724 | |||
| 00fba12eb2 | |||
| ca37003f32 | |||
| 1c70783d22 | |||
| 8f131a0129 |
+75
-12
@@ -9,12 +9,13 @@ import java.net.URL;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
public class Main {
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
boolean help_enqueued = false;
|
||||
boolean allow_networking = true;
|
||||
int verbosity = 1;
|
||||
String matrix_file_name = "";
|
||||
|
||||
int p = 0;
|
||||
while (p < args.length) {
|
||||
@@ -23,17 +24,12 @@ public class Main {
|
||||
help_enqueued = true;
|
||||
p += 1;
|
||||
}
|
||||
else if (Objects.equals(args[p], "-v")) {
|
||||
verbosity = Integer.parseInt(args[p+1]);
|
||||
p += 2;
|
||||
}
|
||||
else if (Objects.equals(args[p], "--offline")) {
|
||||
allow_networking = false;
|
||||
p += 2;
|
||||
}
|
||||
else {
|
||||
System.err.println("[ERROR] Unknown argument found: " + args[p] + "\n[ERROR] Please, use 'java Main -h' for help");
|
||||
System.exit(1);
|
||||
matrix_file_name = args[p++];
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -47,6 +43,38 @@ public class Main {
|
||||
printHelp(allow_networking);
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
if (Objects.equals("", matrix_file_name)) {
|
||||
System.err.println("[WARN] No filename specified to read from, using default one");
|
||||
matrix_file_name = "random_matrix.txt";
|
||||
}
|
||||
|
||||
File matrix_file = new File(matrix_file_name);
|
||||
if (!matrix_file.exists()) {
|
||||
if (allow_networking) {
|
||||
System.err.println("[WARN] File is missing, downloading random matrix from the server");
|
||||
fetchResource("http://lab2.kpi.dev:16554/matrix.py", matrix_file_name);
|
||||
} else {
|
||||
System.err.println("[WARN] File is missing; networking is disabled, not recovering");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// loading matrix data from file
|
||||
int[][] raw_m = loadMatrixFromFile(matrix_file);
|
||||
|
||||
// creating original matrix object
|
||||
System.out.println("Original matrix:");
|
||||
Matrix m = new Matrix();
|
||||
m.init(raw_m);
|
||||
|
||||
// transposing
|
||||
m.transpose();
|
||||
System.out.println("\nTransposed matrix:");
|
||||
m.print();
|
||||
|
||||
// avg
|
||||
System.out.println("\nAverage value is " + m.getAvg());
|
||||
}
|
||||
|
||||
private static void fetchResource(String remote_url, String output_filename) {
|
||||
@@ -72,7 +100,7 @@ public class Main {
|
||||
System.err.println("[WARN] Help file is missing.");
|
||||
if (allow_net) {
|
||||
System.err.println("[INFO] Trying to recover it from the git server");
|
||||
fetchResource("http://139.162.162.130:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt");
|
||||
fetchResource("http://lab2.kpi.dev:3000/dymik739/oop-labs-collection/raw/branch/lab2-dev/labs/2/src/help.txt", "src/help.txt");
|
||||
} else {
|
||||
System.err.println("[INFO] Networking is disabled, not recovering");
|
||||
System.exit(1);
|
||||
@@ -80,17 +108,52 @@ public class Main {
|
||||
}
|
||||
|
||||
Scanner help_file_scanner = new Scanner(help_file);
|
||||
String help_text = "";
|
||||
|
||||
while (help_file_scanner.hasNextLine()) {
|
||||
help_text += help_file_scanner.nextLine();
|
||||
System.out.println(help_file_scanner.nextLine());
|
||||
}
|
||||
|
||||
help_file_scanner.close();
|
||||
System.out.println(help_text);
|
||||
} catch (Exception e) {
|
||||
System.out.println("[ERROR] Failed to read help due to the following exception: " + e);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private static int[][] loadMatrixFromFile(File matrix_file) {
|
||||
try {
|
||||
Scanner matrix_file_reader = new Scanner(matrix_file);
|
||||
|
||||
ArrayList<String> raw_matrix_lines = new ArrayList<String>();
|
||||
while (matrix_file_reader.hasNextLine()) {
|
||||
raw_matrix_lines.add(matrix_file_reader.nextLine());
|
||||
}
|
||||
|
||||
ArrayList<String[]> baked_matrix_lines = new ArrayList<String[]>();
|
||||
|
||||
for (String s : raw_matrix_lines) {
|
||||
baked_matrix_lines.add(s.split(" "));
|
||||
}
|
||||
|
||||
int ref_length = baked_matrix_lines.get(0).length;
|
||||
int[][] baked_matrix = new int[baked_matrix_lines.size()][ref_length];
|
||||
|
||||
for (int i = 0; i < baked_matrix_lines.size(); i++) {
|
||||
if (baked_matrix_lines.get(i).length != ref_length) {
|
||||
System.err.println("[ERROR] Matrix lines have different length! Assuming the matrix was corrupted");
|
||||
System.exit(2);
|
||||
}
|
||||
|
||||
for (int j = 0; j < baked_matrix_lines.get(i).length; j++) {
|
||||
baked_matrix[i][j] = Integer.parseInt(baked_matrix_lines.get(i)[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return baked_matrix;
|
||||
} catch (Exception e) {
|
||||
System.err.println("[ERROR] Failed to read matrix from file " + matrix_file.getName());
|
||||
System.exit(1);
|
||||
return new int[1][1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
public class Matrix {
|
||||
private int[][] m;
|
||||
|
||||
public void init(int[][] base_m) {
|
||||
this.m = base_m;
|
||||
}
|
||||
|
||||
public int[][] getMatrix() {
|
||||
return this.m;
|
||||
}
|
||||
|
||||
public void print() {
|
||||
for (int[] ml : this.m) {
|
||||
for (int e : ml) {
|
||||
System.out.printf("%4d ", e);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public void transpose() {
|
||||
print();
|
||||
if (this.m[0].length != this.m.length) {
|
||||
int new_h = this.m[0].length;
|
||||
int new_w = this.m.length;
|
||||
|
||||
int[][] new_m = new int[new_h][new_w];
|
||||
|
||||
for (int i = 0; i < this.m.length; i++) {
|
||||
for (int j = 0; j < this.m[i].length; j++) {
|
||||
new_m[j][i] = this.m[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
this.m = new_m;
|
||||
} else {
|
||||
// if a matrix is square, there is no need to
|
||||
// make a new one
|
||||
|
||||
int temp_value_holder;
|
||||
|
||||
for (int i = 0; i < this.m.length; i++) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
temp_value_holder = this.m[i][j];
|
||||
this.m[i][j] = this.m[j][i];
|
||||
this.m[j][i] = temp_value_holder;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double getAvg() {
|
||||
double total = 0;
|
||||
|
||||
for (int[] ml : this.m) {
|
||||
for (int e : ml) {
|
||||
total += e;
|
||||
}
|
||||
}
|
||||
|
||||
return total / (this.m.length * this.m[0].length);
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -1 +1,4 @@
|
||||
Dummy help file
|
||||
Usage: java Main [options] [matrix_file]
|
||||
Options:
|
||||
-h Output this help text
|
||||
--offline Prevent any possible attempt to fetch missing files from the server
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
sed -i 's/int/double/g;s/prdouble/print/g;s/Integer.parseInt/Double.parseDouble/g;s/for (double i/for (int i/g;s/for (double j/for (int j/g;s/matrix.py/double-matrix.py/g;s/double p/int p/g;s/double bytesRead/int bytesRead/g;s/double ref_length/int ref_length/g;s/double new/int new/g;s/%4d/%4.3f/g' Matrix.java Main.java
|
||||
|
||||
javac Main.java Matrix.java
|
||||
rm random_matrix.txt
|
||||
@@ -0,0 +1,2 @@
|
||||
import random
|
||||
print("Content-Type: text/plain\n\n" + "\n".join([" ".join(list(map(str, [round((random.random()-0.5)*200, 4) for i in range(7)]))) for i in range(7)]))
|
||||
@@ -0,0 +1,2 @@
|
||||
import random
|
||||
print("Content-Type: text/plain\n\n" + "\n".join([" ".join(list(map(str, [random.randint(-40, 40) for i in range(7)]))) for i in range(7)]))
|
||||
Reference in New Issue
Block a user