Results 1 to 10 of 10
  1. #1
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Post Make Java codes more simplier (Multidimensional Arrays)

    Hello everyone.

    Right here, I have my codes used in JC.

    I'm trying to figure out if there's a possible way I could reduce the length/size of my codes, since I'm kinda new in programming in Java.

    Here it is.


    public class MArrays {

    /**
    Function Name: Main
    Description: This program is designed to get the inputs of the
    user and let the program multiply the numbers form
    the lowest number up to the highest number, using
    two arguments.
    Arguments: String args[]
    Return Type: none

    */


    public static void main(String args[]) {

    if(args.length > 2) {

    System.out.println("Invalid Arguments");

    } else if(args.length < 2) {

    System.out.println("Invalid Arguments");

    } else if(args.length == 2) {

    System.out.println(args[0] + " " + args[1] + "\n");

    int iArray1 = Integer.parseInt(args[0]);
    int iArray2 = Integer.parseInt(args[1]);

    int i1stIndex = iArray1 + 1;
    int i2ndIndex = iArray2 + 1;

    int aiNumbers[][] = new int[i1stIndex][i2ndIndex];

    if(args[0].equals("0") || args[1].equals("0")) {
    System.out.println("0");
    } else {
    for(int iCounter=1;iCounter < aiNumbers.length;
    iCounter++) {

    for(int iCounter2=1;iCounter2 <
    aiNumbers[iCounter].length; iCounter2++) {

    aiNumbers[iCounter][iCounter2] =
    iCounter * iCounter2;
    }
    }


    for(int iCounter=1;iCounter < aiNumbers.length;
    iCounter++) {

    for(int iCounter2=1;iCounter2 <
    aiNumbers[iCounter].length; iCounter2++) {

    System.out.print(aiNumbers[iCounter]
    [iCounter2] + " ");
    }
    System.out.println("");
    }
    }
    }
    System.exit(1);
    }
    }


    I'm trying to lessen my codes but I dont where should I start. Usage of multidimensional arrays should be intact and not to be removed.



    Would somebody help me? Thanks.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Why don't you use something like this.

    Java Code:
    if(args.length != 2) {
       System.out.println("Invalid");
    }
    else {
       // Do the processing
    }
    You can remove around four lines from your code.

    And also, here in your code you just handling commandline arguments. User can't dynamically change values.

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Yes i do agree with eranga.

    the 2 other things what i felt can be reduced are:

    Instead of using iArray1,iArray2,i1stIndex,i2ndIndex that consumes memory
    "int aiNumbers[][] = new int[i1stIndex][i2ndIndex];" can be written as
    int aiNumbers[][] = new int[Integer.parseInt(args[0]) + 1][Integer.parseInt(args[1]) +1];

    Cant the for loops be combined instaed of using 2 for loops one for doing the logic and another for sysout's.

    Both of these will help u to reduce ur code lines.
    To finish sooner, take your own time....
    Nivedithaaaa

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Ya, that's a good tip. Each additional declaration consume memory. So you can avoid as Niveditha says.

    And also you no need to use separate for loops for calculation and print the result. You can do it within the same loop. So here is all the changes until now you have.

    Java Code:
    public class MArrays {
    
    /**
    Function Name: Main
    Description: This program is designed to get the inputs of the
    user and let the program multiply the numbers form
    the lowest number up to the highest number, using
    two arguments.
    Arguments: String args[]
    Return Type: none
    
    */
    
        public static void main(String args[]) {
    
            if(args.length != 2) {
                System.out.println("Invalid Arguments");
            } 
    
            else {
                System.out.println(args[0] + " " + args[1] + "\n");
    
    
                int aiNumbers[][] = new int[Integer.parseInt(args[0])+ 1]
    			[Integer.parseInt(args[1]) + 1];
    
                if(args[0].equals("0") || args[1].equals("0")) {
                    System.out.println("0");
                } 
                else {
                    for(int iCounter=1;iCounter < aiNumbers.length; iCounter++) {
                        for(int iCounter2=1;iCounter2 <
                        aiNumbers[iCounter].length; iCounter2++) {
                            aiNumbers[iCounter][iCounter2] = iCounter * iCounter2;
    			System.out.print(aiNumbers[iCounter][iCounter2] + " ");
                        }
    		    System.out.println("");
                    }
                }
            }
            //System.exit(1);
        }
    }

  5. #5
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    Geee. Thanks a lot. Such a big help. ^^

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Help is on your way. Best thing is learn something from it. ;)

    Still you can reduce a lot within your code.

  7. #7
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    I was thinking how can I make statements out of those 'revised' codes. I was thinking about the 'algorithm' used.

    Would someone tell me the algorithm in a statement form? It would be a great help for my documentation in this program. Thanks.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Just think about the logic in the code use. That's the algorithm(actually this is not a ordinary algorithm) here used. Not much difficult at all.

  9. #9
    javanewbie is offline Member
    Join Date
    Jun 2008
    Location
    Australia
    Posts
    43
    Rep Power
    0

    Default

    Still. I can't think of any algorithm statements here. ='(

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    There is no algorithm there, just a simple logic. The logic is what you have done in your code. It should know yourself.

    Java Code:
    Check the number of command line parameters.
    
       If 2 command line parameters not found, notify it to the user,
    
       Else display command line parameters.
    
          Convert parameters to integer values and add one to each.
    
          Set the array size equal to calculated values.
    
          If any parameter is zero, display the value zero.
    
          Else, for each array element add values of multiplication of each index.
    
    Exit.
    This is the very simple logic in the code.

Similar Threads

  1. How to get error codes using java program
    By kasipandian in forum Web Frameworks
    Replies: 10
    Last Post: 05-25-2008, 05:00 PM
  2. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 AM
  3. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:11 AM
  4. Multidimensional arrays
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:07 PM
  5. Arrays in Java
    By hiranya in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •