Login
Search
Welcome to the iTechForums.
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 3
Members:
Number of Users Online:
Welcome to our newest member,
Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2008, 03:50 PM
Rita C Rita C is offline
Junior Member
 
Join Date: May 2008
Posts: 2
Default

Write a Java Method?


Write a Java method call SumArray which take in an integer array arr and sum up all the values in the array by either a for loop or a while loop and return the sum.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 05-15-2008, 08:02 PM
MadScientist MadScientist is offline
Junior Member
 
Join Date: May 2008
Posts: 1
Default

the parameter for the method should be of type int [], and the return value should be an int.

Declare an int variable that will take on the sum.

The for loop will loop according to the condition i < arr.length, in which i is a variable you declare(to be 0) in the for declaration. You then will increase i by one at the end of each iteration of the loop.

In the loop, you will add the sum variable you declared to itself and the current value of arr[i] - that is:
sum = sum + arr[i] ;

after the loop, you need to return sum.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 05-15-2008, 08:54 PM
rollandb1 rollandb1 is offline
Member
 
Join Date: May 2008
Posts: 37
Default

private int SumArray(int[] arr)
{
int total = 0;
for(int i = 0; i < arr.lenght; i++)
{
total += arr[i];
}
return total;
}
this is C# code.... but you should be able to figure it out...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive Method (Java)? Daneey?h Java, Java Servlets & JSP 3 05-30-2008 08:53 AM
Write a JAVA Applet Program? Rita C Java, Java Servlets & JSP 0 05-08-2008 09:48 PM
Write a Java program with 2 threads:? jante5000 Java, Java Servlets & JSP 0 05-07-2008 03:50 AM
How agile method works in the software development? Ems Development Software 1 05-06-2008 01:15 PM
i need to write a recursive definition for any valid java identfier.? anonymous Java, Java Servlets & JSP 0 05-06-2008 09:48 AM

Your Ad Here

All times are GMT. The time now is 05:32 AM.