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.
}
}
Peach2010 0 Newbie Poster
Peach2010 0 Newbie Poster
apines 116 Practically a Master Poster Featured Poster
Peach2010 0 Newbie Poster
apines 116 Practically a Master Poster Featured Poster
Peach2010 0 Newbie Poster
apines 116 Practically a Master Poster Featured Poster
Peach2010 0 Newbie Poster
apines 116 Practically a Master Poster Featured Poster
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.