Hello!
I'm having difficulty figuring out how to begin solving some simple problems involving arrays. I've only just begun arrays and I am not sure how to structure my program.
My task is simply to create a few methods, such as a count() method that uses a for_each loop to count & return the number of elements for any array of int.
Another example of a method I am required to write would be sum(), a method that calculates and returns the sum of elements in an int array.
I'm required to use a constructor to call these new methods, using parameters and return values as appropriate.
I really appreciate any help or guidance to start me off. I apologize that I have no code or output to paste. I'm just not sure where to even begin other than the basic creation of a main method.
Simple array methods; beginner to Java...
Page 1 of 14 Replies - 1216 Views - Last Post: 19 October 2011 - 12:31 PM
Replies To: Simple array methods; beginner to Java...
#2
Re: Simple array methods; beginner to Java...
Posted 18 October 2011 - 03:32 PM
The loop counter is typically used as the index of the array to check or operate on each element of the array as needed:
A similar approach would be used to search the array, but an 'if' statement or some other search tool would be used to inspect each array element.
As for calling your methods from a constructor - a constructor is just a method with a specific name and no return type. You can call methods as necessary from it as you would from any other method.
This should get you started at writing some code, even if it's just a few skeletons. Come back when you need more help.
int arraySum = 0;
for ( int i = 0; i < array.length; i++ )
{
arraySum += array(i);
}
A similar approach would be used to search the array, but an 'if' statement or some other search tool would be used to inspect each array element.
As for calling your methods from a constructor - a constructor is just a method with a specific name and no return type. You can call methods as necessary from it as you would from any other method.
This should get you started at writing some code, even if it's just a few skeletons. Come back when you need more help.
#3
Re: Simple array methods; beginner to Java...
Posted 19 October 2011 - 12:21 PM
Thank you for the response!
I've created a constructor that houses some code for just one count() method so far...
I'm having a tough time figuring out how to make that count() method with a for_each loop, however, and have it count and return the number of elements in an array that is {7, 8, 9, 9, 8, 7}.
Here's something of what I have so far...
I'm not sure if I'm even going in the right direction at this point. I get an error that highlights "return counter;" and states that it is an invalid type. I'm assuming that means I cannot return that value.
Thank you for any additional help you can send my way.
I've created a constructor that houses some code for just one count() method so far...
I'm having a tough time figuring out how to make that count() method with a for_each loop, however, and have it count and return the number of elements in an array that is {7, 8, 9, 9, 8, 7}.
Here's something of what I have so far...
public class Arrays
{
public Arrays()
{
int array1[] = {7, 8, 9, 9, 8, 7};
System.out.println("count(): ");
count(array1);
}
/**
* counts number of elements in an array
*
* @param a[] int array to count
*/
public int[] count(int a[])
{
int counter = 0;
for (int e : a)
{
counter++;
}
return counter;
}
}
I'm not sure if I'm even going in the right direction at this point. I get an error that highlights "return counter;" and states that it is an invalid type. I'm assuming that means I cannot return that value.
Thank you for any additional help you can send my way.
#4
Re: Simple array methods; beginner to Java...
Posted 19 October 2011 - 12:30 PM
You are having it return an array of ints, so if you want to return one value, make it just an int, not int[].
Also, for the counter i think you want to count the numbers, so the letter e represents each value of the array, so just add e to the counter every iteration. counter += e;
Also, for the counter i think you want to count the numbers, so the letter e represents each value of the array, so just add e to the counter every iteration. counter += e;
#5
Re: Simple array methods; beginner to Java...
Posted 19 October 2011 - 12:31 PM
Two things, your method should return int and not array of ints, this is because you need to return number of elements in array: so public int count(int a[])
Then you have to get the returned result by displaying it or assign its value, like:
Then you have to get the returned result by displaying it or assign its value, like:
System.out.println("count(): " + count(array1) );
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|