Module 3 LinearEquationSolver#8
Module 3 LinearEquationSolver#8niskhakov wants to merge 3 commits intohyperskill:masterfrom niskhakov:master
Conversation
| Row normalizeRow(int index) { | ||
| try { | ||
| return divide(row[index]); | ||
| } catch (ArithmeticException e) { |
There was a problem hiding this comment.
Use the word "ignore" after the ArithmeticException keyword.
|
|
||
| Row divide(double v) { | ||
| if(v == 0){ | ||
| throw new java.lang.ArithmeticException("Division by zero"); |
There was a problem hiding this comment.
you can simply write: throw new ArithmeticException("Division by zero");
| * Example: > java Solver -in in.txt -out out.txt | ||
| * @param args | ||
| */ | ||
| public static void main(String[] args) { |
There was a problem hiding this comment.
Method main is very big. If your method does two or three different things at a time then you should consider splitting the functionality of this method into other methods (single responsibility principle) and just try to make your methods as small as possible.
| public class Main { | ||
|
|
||
| static String getHelp() { | ||
| String helpMsg = "This program is intended to solve Linear Equations with any amount of variables using Gauss-Jordan Elimination. \n\n" + |
There was a problem hiding this comment.
User StringBuilder (of course the compiler will use StringBuilder automatically, but the usage of it is more look like Builder pattern).
| AugmentedMatrix() {} | ||
|
|
||
| public void readMatrix(Scanner scanner) throws InputMismatchException { | ||
| int n = scanner.nextInt(); |
There was a problem hiding this comment.
Never use only one letter in the name. Variable should represent the value it was created for.
| private void transformToUpperTriangularForm() { | ||
| Row currentRow; | ||
| int n = matrix.size()[0]; | ||
| for(int i = 0; i < n; i++) { |
| for(int i = 0; i < n; i++) { | ||
| currentRow = matrix.getRow(i); | ||
| normalizeRowsForTransforming(i, currentRow); | ||
| for(int j = i + 1; j < n; j++) { |
| double coefficient = matrix.getRow(j).get(i); | ||
| if(coefficient != 1.0) { | ||
| matrix.getRow(j).normalizeRow(i); | ||
| logMessage(String.format("R%d / %s - R%d -> R%d", j, new DecimalFormat("#.###").format(coefficient), i, j)); |
There was a problem hiding this comment.
So you are better off having static String variable with "#.###"
| */ | ||
| private void subtractRowsForTransforming(int i, int j, Row currentRow) { | ||
| double coefficient = matrix.getRow(j).get(i); | ||
| if(coefficient != 1.0) { |
| */ | ||
| private void normalizeRowsForTransforming(int i, Row currentRow) { | ||
| double coefficient = currentRow.get(i); | ||
| if(coefficient != 1.0) { |
|
|
||
| public String getResultString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| for(double coefficient: result) { |
| sb.append(new DecimalFormat("#.####").format(coefficient)); | ||
| sb.append("\n"); | ||
| } | ||
| sb.deleteCharAt(sb.length()-1); |
There was a problem hiding this comment.
Need spaces after "sb.length()" and before "-1"
| int n = this.matrix.size()[0]; | ||
| int m = this.matrix.size()[1]; | ||
| result = new double[n]; | ||
| for(int i = 0; i < n; i++) { |
| System.out.println("The solution is: (" + res.replaceAll("\n", ", ") + ")"); | ||
|
|
||
| // Output writing | ||
| try(FileWriter fileWriter = new FileWriter(outputFile)) { |
edvardyanushkevich
left a comment
There was a problem hiding this comment.
The solution contains quite a few remarks on formatting, so I would like to give some advices:
IDE has hotkeys for auto-formatting. (For example IDEA: Ctrl+Alt+L, Eclipse: Ctrl + Shift + F, NetBeans: Alt + Shift + F)
Java Code Conventions: https://www.oracle.com/technetwork/java/codeconventions-150003.pdf
Single Responsibility - one method should be responsible only for one action. If your method does two or three different things at a time then you should consider splitting the functionality of this method into other methods.
Small Methods - there is no a standard pattern for method length among the developers. Just try to make your methods as small as possible.
Hi!
Check out my realization, plz)