Results 1 to 4 of 4
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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)

    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();
    	}
    }
    Thank you in advance for your help!!!
    ~Jaime

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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?

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default Re: Arrays and Methods

    Quote Originally Posted by sunde887 View Post
    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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

  1. Question about arrays and methods
    By BenH in forum New To Java
    Replies: 3
    Last Post: 01-31-2012, 02:45 AM
  2. Using Arrays and Overloading Methods.
    By dalek in forum New To Java
    Replies: 5
    Last Post: 10-25-2011, 04:52 AM
  3. Instancing methods in arrays possible?
    By KReative in forum New To Java
    Replies: 9
    Last Post: 10-09-2011, 01:54 PM
  4. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 09:43 AM
  5. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •