Solution 15 - Dear Brothers and Sisters pls give your opinions critical note on my code

Back to Problem Solutions forum

shatseli     2020-01-19 20:06:46

Hi to everybody ! Dear Brothers and Sisters I will be greatly appreciating for any of your opinions ( including the critical ones ) regarding my code below for solution #15...

// 
// Problem #15 -- Linear search
//

// As far as I INTENTIONALLY did not want to use "Array.sort" method
// to get the Minimum and Maximum values so Algorythm is as following :
//
//   1. read line ( row data )
//      .nextLine() .hasnextLine

//   2. add to array via split -
//      - DOES anybody has any idea how to read DATAROW DIRECTLY to array 
//        of inetegers ?

//   3. process the array in loop and get the min and max

//   3.1 Arbitatey
import java.util.Arrays ;
import java.util.ArrayList ;
import java.util.Scanner ;

public class Modular_Calculator
 {

    public static void main(String[] args)
    {
        Scanner lineScanner = new Scanner(System.in); 
        int[] MaxMin = new int[2];

        do { 
            String DataRow= lineScanner.nextLine();

            MaxMin = cells_processing( DataRow);
            for( int tmpelm:MaxMin )
            { System.out.print(tmpelm+" ");}

          } while (lineScanner.hasNextLine());
    }

    public static int[] cells_processing( String data_record)
        {   int      tmpMaxMin[] = new int[2];

            String[] cells = data_record.split(" ");
            System.out.print("Length = "+ cells.length );
               //         [0] for Max Min value
                         tmpMaxMin[0] = Integer.parseInt(cells[0]);
                      //          [1] for Min value
                     tmpMaxMin[1] = tmpMaxMin[0];


            for( int i = 1; i<cells.length; i++)
            { int tempInt =Integer.parseInt( cells[i] );
                         // Min
              if (tmpMaxMin[1] > tempInt) tmpMaxMin[1] = tempInt ;
                         // Max
              if (tmpMaxMin[0] < tempInt) tmpMaxMin[0] = tempInt ;
            }
            return   tmpMaxMin;
        }
} 
Rodion (admin)     2020-01-20 08:55:32
User avatar

Eli, Hi!

Hopefully you won't object if I try answering, as I had some experience with java in idustry :)

First of all - please be not offended - but it is regarded as most critical. Find the document "java code conventions" - it's pretty old, but most of java programmers prefer following it. You also can find book "Clean Code" by Robert Martin (it is worth reading it's first 1/3 roughly. The matter is that most developers forgive if code has errors (we all do them), but won't forgive poor style.

Things to take care are mainly

  • names of variables and functions
  • indents and spaces
  • brackets and parentheses placement

Second thing - after you solve the problem, you can check other's solutions, filtered by language. This is probably greatest way to learn various approaches and tricks. For example I myself found that some people use BufferedReader instead of Scanner and only after googling and testing found it works much faster on large input.

read DATAROW DIRECTLY to array // of inetegers

I think you read line, use split and then functional-like "foreach" or "map" functions if you want.

Though this problem doesn't require internal array. You may record min/max as you read every number without storing them. Scanner.nextInt may be helpful.

did not want to use "Array.sort"

that would be algorithmically poor approach as it takes N*log(N) time instead of N, but for small array it's ok...

In general I dare say that your code may omit or shorten certain parts (e.g. you need not set initial values for min and max from array) - but these all things you may find out browsing people's codes and judging them :)

If you don't mind, I think it is very important exercise - to read other's code!

page to browse solutions for task is like this, linked from every task's page

Please feel free to ask if I poorly explained some points!

shatseli     2020-01-22 08:21:51

Thank you very much Rodion ! I am greatly appreciating your reply ( and do not offending at all ;-) ) thank again I will work on improving my skills ! Eli

Please login and solve 5 problems to be able to post at forum