Results 1 to 10 of 10
- 06-23-2008, 08:35 AM #1
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
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.
- 06-23-2008, 09:15 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Why don't you use something like this.
You can remove around four lines from your code.Java Code:if(args.length != 2) { System.out.println("Invalid"); } else { // Do the processing }
And also, here in your code you just handling commandline arguments. User can't dynamically change values.
- 06-23-2008, 10:52 AM #3
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
- 06-23-2008, 11:27 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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); } }
- 06-23-2008, 11:31 AM #5
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
Geee. Thanks a lot. Such a big help. ^^
- 06-23-2008, 11:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Help is on your way. Best thing is learn something from it. ;)
Still you can reduce a lot within your code.
- 06-23-2008, 11:58 AM #7
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
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.
- 06-23-2008, 12:22 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-24-2008, 03:02 PM #9
Member
- Join Date
- Jun 2008
- Location
- Australia
- Posts
- 43
- Rep Power
- 0
Still. I can't think of any algorithm statements here. ='(
- 06-25-2008, 04:48 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
There is no algorithm there, just a simple logic. The logic is what you have done in your code. It should know yourself.
This is the very simple logic in the code.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.
Similar Threads
-
How to get error codes using java program
By kasipandian in forum Web FrameworksReplies: 10Last Post: 05-25-2008, 05:00 PM -
Arrays Problem (Advanced Java...Need Help)
By Zebra in forum New To JavaReplies: 9Last Post: 05-02-2008, 01:26 AM -
Help needed with java arrays code
By d24706 in forum New To JavaReplies: 2Last Post: 03-07-2008, 01:11 AM -
Multidimensional arrays
By Java Tip in forum Java TipReplies: 0Last Post: 11-05-2007, 05:07 PM -
Arrays in Java
By hiranya in forum New To JavaReplies: 3Last Post: 07-30-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks