Results 1 to 4 of 4
Thread: Arrays and Methods
- 03-27-2012, 12:33 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
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)
Thank you in advance for your help!!!Java 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. */ public static double[] readInputs() { 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 #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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 #3
Member
- Join Date
- Feb 2012
- Posts
- 57
- Rep Power
- 0
Re: Arrays and Methods
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 #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
Similar Threads
-
Question about arrays and methods
By BenH in forum New To JavaReplies: 3Last Post: 01-31-2012, 02:45 AM -
Using Arrays and Overloading Methods.
By dalek in forum New To JavaReplies: 5Last Post: 10-25-2011, 04:52 AM -
Instancing methods in arrays possible?
By KReative in forum New To JavaReplies: 9Last Post: 10-09-2011, 01:54 PM -
Arrays and methods
By namie in forum New To JavaReplies: 3Last Post: 10-05-2009, 09:43 AM -
Arrays & Methods
By TheRocket in forum New To JavaReplies: 1Last Post: 12-10-2008, 07:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks