public class ReverseArray
{
    //declare an array variable
    //do not call new yet!

	int[] array = new int[100];
	int a;
	int[] getCopy = new int[array.length];


    /**
     * Constructor
     *
     * Reads in int values and stores them in an array.
     *
      The first int in the input is the number of values to follow.
      Use that value to create a new array of that size. Read in
     only that many values.
     *
     * The rest of the input should be stored in the array.
     */
    public ReverseArray(Reader input)
    {



		for (int i = 0; i < array.length; i++);






    }

    /**
     * Reverses the contents of the array.
     *
     * When reversed the array elements are changed so that the first
     * element becomes the last element, the second element becomes
     * the second to last element, and so on, with the old last element
     * now first.
     *
      You must change the way they are stored in the array. Do not
      create a second array; just rearrange the elements within the
    array you have. (Hint: Swap elements that need to change places.)
     *
     * HINT: you only need to loop over half of the array.
     * HINT: Think about what the index of the last element of the array.
     *       You want to swap that index with the first element.
     *       What is the index of the element you want to swap with the
     *       second element?  See a pattern.  Use the length and a simple
     *       formula to calculate the index being used.
     *
     */
    public void reverse()
    {
        for (int a = array.length -1; a >= 0; a--)
        {

       System.out.print(array[a] + "");
    }
    }
    /**
     * Creates and returns a copy of the integer array.
     *
     * NOTE: This is a copy of the array, that is, a new
     * array object with the same values stored in it.
     *
     * DO NOT return a reference to the array.
     *
     * @return a copy of the array
     */
    public int[] getCopy()
    {
      for( a = 0; a <array.length;a++);

      {
      getCopy[a]=array[a];
      }

        return getCopy;
    }

    /**
     * Swaps the elements in position a and position b in the array.
     *
     * @param a
     * @param b
     */
    private void swap(int a, int b)
    {
        //HINT:  You will need to use a temporary variable to
        //swap the values correctly.  Think carefully about the
        //order in which the operations must occur.
    	
    	
    
    
    
    
    
    
    }

  }

Recommended Answers

All 8 Replies

Sorry i forgot this in my previous code i do not need a main function because there is a junit test file that goes along with it

int temp;

    		temp = array[a];
    		array[a] = array[b];
    		array[b] = temp;

You didn't finish implementing all the methods yet, how do you expect it to work?

i thought i did i forgot to add the swap part but i replied to the code with it so... can you help

Sure, let's start with the constructor:

public ReverseArray(Reader input)
{
   for (int i = 0; i < array.length; i++);
}

It's counting from - to array.length, and is doing nothing. What exactly are you trying to do?

i am trying to read in int values and stores them in an array

Doing an empty for loop will not do that. You need to use the Reader input parameter that was given to you in that method.

ok i dont know how to do that so let me look it up

A good start will be here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.