Skip to content

Conversation

@niskhakov
Copy link

Hi!
Check out my realization, plz)

Row normalizeRow(int index) {
try {
return divide(row[index]);
} catch (ArithmeticException e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the word "ignore" after the ArithmeticException keyword.


Row divide(double v) {
if(v == 0){
throw new java.lang.ArithmeticException("Division by zero");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" +
Copy link

@edvardyanushkevich edvardyanushkevich Feb 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "for"

for(int i = 0; i < n; i++) {
currentRow = matrix.getRow(i);
normalizeRowsForTransforming(i, currentRow);
for(int j = i + 1; j < n; j++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "for"

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));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "if"

*/
private void normalizeRowsForTransforming(int i, Row currentRow) {
double coefficient = currentRow.get(i);
if(coefficient != 1.0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "if"


public String getResultString() {
StringBuilder sb = new StringBuilder();
for(double coefficient: result) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "for"

sb.append(new DecimalFormat("#.####").format(coefficient));
sb.append("\n");
}
sb.deleteCharAt(sb.length()-1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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++) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "for"

System.out.println("The solution is: (" + res.replaceAll("\n", ", ") + ")");

// Output writing
try(FileWriter fileWriter = new FileWriter(outputFile)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need space after "try"

Copy link

@edvardyanushkevich edvardyanushkevich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants