1 / 1100%
// File: Mystery.java
public class Mystery
{
private int[] x;
private int n;
//constructor
public Mystery()
{
x = new int[10];
x[0] = 15;
x[1] = 5;
x[2] = 4;
x[3] = 10;
x[4] = 7;
x[5] = 2;
n = 6;
}
// reverses the array
public void rearrange( )
{
int first = 0;
int last = n - 1;
while( first < last )
{
System.out.println ( first + " " + last );
int temp = x[first];
x[first] = x[last];
x[last] = temp;
first++;
last--;
}
}
// This method returns the index where searchingNum is located.
// It returns -1 if it is not found.
public int indexOf(int searchingNum)
{
for (int i = 0; i < n; i++)
{
if (x[i] == searchingNum)
return i;
}
return -1;
}
public String toString( )
{
String result = "{ ";
for( int j = 0; j < n; j++ )
result += x[j] + " ";
return result += "}";
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed