13 Commits

9 changed files with 390 additions and 18 deletions
+1
View File
@@ -0,0 +1 @@
*.class
+79 -14
View File
@@ -9,12 +9,13 @@ import java.net.URL;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
public class Main { import java.util.ArrayList;
public class Main {
public static void main(String[] args) { public static void main(String[] args) {
boolean help_enqueued = false; boolean help_enqueued = false;
boolean allow_networking = true; boolean allow_networking = true;
int verbosity = 1; String matrix_file_name = "";
int p = 0; int p = 0;
while (p < args.length) { while (p < args.length) {
@@ -23,17 +24,12 @@ public class Main {
help_enqueued = true; help_enqueued = true;
p += 1; p += 1;
} }
else if (Objects.equals(args[p], "-v")) {
verbosity = Integer.parseInt(args[p+1]);
p += 2;
}
else if (Objects.equals(args[p], "--offline")) { else if (Objects.equals(args[p], "--offline")) {
allow_networking = false; allow_networking = false;
p += 2; p += 1;
} }
else { else {
System.err.println("[ERROR] Unknown argument found: " + args[p] + "\n[ERROR] Please, use 'java Main -h' for help"); matrix_file_name = args[p++];
System.exit(1);
} }
} }
catch (Exception e) { catch (Exception e) {
@@ -47,6 +43,39 @@ public class Main {
printHelp(allow_networking); printHelp(allow_networking);
System.exit(0); 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);
m.print();
// 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) { private static void fetchResource(String remote_url, String output_filename) {
@@ -61,6 +90,7 @@ public class Main {
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e); System.out.println("[ERROR] Failed to fetch resource " + output_filename + "from " + remote_url + " due to the following exception: " + e);
System.exit(1);
} }
} }
@@ -71,8 +101,8 @@ public class Main {
if (!help_file.exists()) { if (!help_file.exists()) {
System.err.println("[WARN] Help file is missing."); System.err.println("[WARN] Help file is missing.");
if (allow_net) { if (allow_net) {
System.err.println("[INFO] Trying to recover it from the git server"); System.err.println("[INFO] Trying to recover it from the CDN 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:16554/help.txt", "src/help.txt");
} else { } else {
System.err.println("[INFO] Networking is disabled, not recovering"); System.err.println("[INFO] Networking is disabled, not recovering");
System.exit(1); System.exit(1);
@@ -80,17 +110,52 @@ public class Main {
} }
Scanner help_file_scanner = new Scanner(help_file); Scanner help_file_scanner = new Scanner(help_file);
String help_text = "";
while (help_file_scanner.hasNextLine()) { while (help_file_scanner.hasNextLine()) {
help_text += help_file_scanner.nextLine(); System.out.println(help_file_scanner.nextLine());
} }
help_file_scanner.close(); help_file_scanner.close();
System.out.println(help_text);
} catch (Exception e) { } catch (Exception e) {
System.out.println("[ERROR] Failed to read help due to the following exception: " + e); System.out.println("[ERROR] Failed to read help due to the following exception: " + e);
System.exit(1); 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];
}
}
} }
+62
View File
@@ -0,0 +1,62 @@
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() {
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
View File
@@ -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
+6
View File
@@ -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
+2
View File
@@ -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)]))
+2
View File
@@ -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)]))
+100
View File
@@ -0,0 +1,100 @@
/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import java.util.Arrays;
/**
* Group class defines a representation of a university group with students
* that can be used as a convenient structure to look at students' statistics.
*
* @version 0.1 20 May 2023
* @author Dymik739
*/
public class Group {
/**
* Main method which should be executed at the start of this application.
*
* @since 0.1
*/
public static void main() {
Student[] studentList = {
// name, results, motivation%, ability%, course№
new Student("Davie", 16.3, 93.5, 98.9, 5),
new Student("Terry", 99.4, 49.3, 73.2, 7),
new Student("Mark", 67.9, 15.5, 7.8, 4),
new Student("Rand", 85.5, 82.6, 99.9, 12),
new Student("Steve", 1.2, 99.8, 99.2, 1)
};
System.out.println("Original students array:");
printStudents(studentList);
Arrays.sort(studentList, (o1, o2) -> o1.getName().compareTo(o2.getName()));
System.out.println("\nArray, sorted by students' name:");
printStudents(studentList);
Arrays.sort(studentList, (o1, o2) ->
compareDouble(o2.getResults(), o1.getResults()));
System.out.println("\nArray, sorted by the reverse of students' " +
"score results:");
printStudents(studentList);
System.out.println("\nAs we can clearly see, " +
studentList[studentList.length-1].getName() + " with " +
studentList[studentList.length-1].getMotivationPercent() +
"% of motivation and " +
studentList[studentList.length-1].getLearningAbilitiesPercent() +
"% ability to learn will be the one who is kicked " +
"from this university, because such is our life.");
}
/**
* Outputs to stdout a given array of students in a fancy way.
*
* @param array array to print out
*/
private static void printStudents(Student[] array) {
for (Student s : array) {
System.out.println(s);
}
}
/**
* Compares numbers of type Double
*
* @param i1 first number
* @param i2 second number to compare
* @return if the first argument is greater, 1 is returned
* if the second argument is greater, -1 is returned
* if the arguments are equal, 0 is returned
*/
private static int compareDouble(double i1, double i2) {
if (i1 > i2) {
return 1;
} else if (i1 < i2) {
return -1;
} else {
return 0;
}
}
}
+131
View File
@@ -0,0 +1,131 @@
/*
* %W% %E% Dymik739
* Email: dymik739@109.86.70.81
*
* Copyright (C) 2023 FIOT Dev Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* represents a student with generic terms and phrases to let programmers
* write more readable code.
*
* @author Dymik739
* @version 0.1
* @since 0.1
*/
public class Student {
/**
* name of the student
*/
private String name;
/**
* overall ranking the student gets after finishing all courses
*/
private double results;
/**
* a reasonable variable showing student motivation. The higher this
* number is, the better this student studies.
*/
private double motivationPercent;
/**
* general ability to learn which directly affects learning speed.
*/
private double learningAbilities;
/**
* shows the amount of subjects this student is studying.
*/
private int chosenCoursesAmount;
/**
* This constructor lets you create the Student class right away
*
* @param name the name of this student
* @param results starting point for the overall ranking score of this
* student
* @param motivationPercent this student motivation level. Higher values
* allow for more jobs taken consecutively.
* @param learningAbilities directly affect learning speed of this student
* @param chosenCoursesAmount amount of courses chosen by this student.
* Roughly shows the load being put on them
* @since 0.1
*/
public Student(String name, double results, double motivationPercent,
double learningAbilities, int chosenCoursesAmount) {
this.name = name;
this.results = results;
this.motivationPercent = motivationPercent;
this.learningAbilities = learningAbilities;
this.chosenCoursesAmount = chosenCoursesAmount;
}
/**
* This method allows you to print out the student object in a nice way.
*
* @since 0.1
*/
@Override
public String toString() {
return "Student(name = '" + this.name + "', results = " + this.results +
", motivationPercent = " + this.motivationPercent +
", learningAbilities = " + this.learningAbilities +
", chosenCoursesAmount = " + this.chosenCoursesAmount + ")";
}
/**
* Getter for the private name field of this class
*
* @return String containing this student's name
* @since 0.1
*/
public String getName() {
return this.name;
}
/**
* Getter for the private results field of this class
*
* @return double representing final score of the student
* @since 0.1
*/
public double getResults() {
return this.results;
}
/**
* Getter for the private motivationPercent field of this class.
*
* @return double representing strenght of this student's motivation to
* learn
* @since 0.1
*/
public double getMotivationPercent() {
return this.motivationPercent;
}
/**
* Getter for the private motivationPercent field of this class.
*
* @return double representing how quickly this student can learn
* @since 0.1
*/
public double getLearningAbilitiesPercent() {
return this.learningAbilities;
}
}