|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-23-2008, 09:35 AM
|
|
Member
|
|
Join Date: Jun 2008
Location: Australia
Posts: 19
|
|
|
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, 10:15 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,047
|
|
Why don't you use something like this.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 11:52 AM
|
 |
Senior Member
|
|
Join Date: May 2008
Posts: 282
|
|
|
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, 12:27 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,047
|
|
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.
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);
}
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 12:31 PM
|
|
Member
|
|
Join Date: Jun 2008
Location: Australia
Posts: 19
|
|
|
Geee. Thanks a lot. Such a big help. ^^
|
|

06-23-2008, 12:38 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,047
|
|
Help is on your way. Best thing is learn something from it. 
Still you can reduce a lot within your code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-23-2008, 12:58 PM
|
|
Member
|
|
Join Date: Jun 2008
Location: Australia
Posts: 19
|
|
|
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, 01:22 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,047
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-24-2008, 04:02 PM
|
|
Member
|
|
Join Date: Jun 2008
Location: Australia
Posts: 19
|
|
|
Still. I can't think of any algorithm statements here. ='(
|
|

06-25-2008, 05:48 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,047
|
|
There is no algorithm there, just a simple logic. The logic is what you have done in your code. It should know yourself.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|