# Arrays and Methods

• 03-27-2012, 12:33 AM
jmscarlet9
Arrays and Methods
Oh great Java Programmers,

I am so clueless on my homework assignments. I thought I might be on the right track but alas, I really have no idea. Perhaps if one of you fantastic people will help me with this problem, I might be able to figure out the second??

Here's the assignment:
Write a program that initializes an array with ten random integers and then prints four lines of output, containing:
1) Every element at an even index. 2) Every even element. 3) All elements in reverse order. 4) Only the first and last element.

Here is the code I have so far: (Please note that I don't really get the return statements of methods and how I would pull all of this together to get my final 4 lines of print)

Code:

import java.util.Arrays;
import java.util.Scanner;

public class FourLines
{
public static void main(String[] args)
{
}
/** Read a series of inputs.
@return an array continaing the numbers.
*/
{
final int LENGTH = 500;
double[] data = new double[LENGTH];
int currentSize = 0;

System.out.println("Please enter values. Enter Q to quit:");
Scanner in = new Scanner(System.in);
while (in.hasNextDouble())
{
if (currentSize >= data.length)
{
data = Arrays.copyOf(data, 2 * data.length);
}
data[currentSize] = in.nextDouble();
currentSize++;
}
return Arrays.copyOf(data, currentSize);
}
public static double[] evenIndex(double data)
{
for (int i = 0; i <= data.length; i = i + 2)
{
System.out.println(i);
i++;
}
}
public static double[] printEven(double data)
{
for (int i = 0; i <= data.length; i++)
if (data / 2 == 0)
{
System.out.println(data);
i++;
}
else
{
i++;
}
}
public static double[] printReverse(a[])
{
for (int i = data.length - 1; i >= 0; i--)
{
System.out.println(data[i] + " ");
}
System.out.println();
}
}

~Jaime
• 03-27-2012, 01:14 AM
sunde887
Re: Arrays and Methods
Besides the methods which get input and initialize the array, you can just return void. In your method to print even elements you have a logical error. What is 4 /2? Is 4 even? will 4 satisfy the condition in that method?
• 03-27-2012, 01:32 AM
jmscarlet9
Re: Arrays and Methods
Quote:

Originally Posted by sunde887
In your method to print even elements you have a logical error. What is 4 /2? Is 4 even? will 4 satisfy the condition in that method?

It should be % instead of / right? Because I want things that have a remainder of 0.

As for the void statements, I think I get that, but would I still have the println statements in each of the methods?

Also, I'm getting a lot of "cannot find symbol" errors which I think is because I have things defined within the loops. However, I'm not sure where to put them so they go across all the loops.
• 03-27-2012, 02:29 AM
sunde887
Re: Arrays and Methods
If you want something accessible from other methods you may want to consider wider variable scoping, although you should aim to keep scoping to a minimum. Generally,if a method returns something, you must return something. If a method returns void you can just print stuff and not bother with return statements. Also, using % instead of / is correct.