
Patrick B. answered 03/25/21
Math and computer tutor/teacher
import java.util.Random;
class AlanJ
{
protected String A[];
protected int count;
protected String tempA[];
AlanJ()
{
A = new String[1];
count=0;
tempA=null;
}
public int GetCount() { return(count); }
public void InsertAddNew( String newStr)
{
if (count==0)
{
A[0] = new String(newStr);
count=1;
}
else
{
tempA = new String[count+1];
for (int iLoop=0; iLoop<count; iLoop++)
{
tempA[iLoop] = new String(A[iLoop]);
// System.out.println("----- inserting " + tempA[iLoop]);
}
tempA[count++]=new String(newStr); //System.out.println(tempA[count-1]);
A = tempA;
tempA = null;
}
}
public int RemoveDelete(int iIndexPos)
{
int iReturn=0;
if (count<=0) //list is empty...no can do
{
iReturn = -1;
}
else if (count==1) //the list is now empty as the only remaining element is GONE!!!
{
A=null; count=0;
}
else // array is sufficiently populated
{
if ((iIndexPos>=0) && (iIndexPos<count)) //index is valid
{
tempA = new String[count-1];
// transfers everything EXCEPT the one at index position
for (int iLoop=0; iLoop<iIndexPos; iLoop++)
{
tempA[iLoop] = new String(A[iLoop]);
}
//deletion occurs here
for (int iLoop=iIndexPos+1; iLoop<count; iLoop++)
{
tempA[iLoop-1] = new String(A[iLoop]);
}
count--;
A = tempA;
tempA = null;
}
else //invalid index
{
iReturn = -1;
}
}
return(iReturn);
}
public void Dump(String strMsg)
{
if (strMsg!=null)
{
System.out.println("**********************************************************");
System.out.println(strMsg);
}
System.out.println("**********************************************************");
for (int iLoop=0; iLoop<A.length; iLoop++)
{
System.out.println(">" + A[iLoop] + "<");
}
}
public void LeftShift()
{
String tempFirstStr = A[0];
int N = A.length;
for (int iLoop=0; iLoop<A.length-1; iLoop++)
{
A[iLoop] = new String(A[iLoop+1]);
}
A[N-1]= new String (tempFirstStr);
}
public void RightShift()
{
int N = A.length-1;
String tempLastStr = A[N];
for (int iLoop=N; iLoop>0; iLoop--)
{
A[iLoop] = new String (A[iLoop-1]);
}
A[0] = new String(tempLastStr);
}
public String IndexerGetAtIndex(int iIndexPos)
{
String strReturn=null;
if ((iIndexPos>=0) && (iIndexPos<count))
{
strReturn = new String(A[iIndexPos]);
}
return(strReturn);
}
public void Go()
{
AlanJ x = new AlanJ();
x.InsertAddNew("Amy Shannon");
x.InsertAddNew("Carmen");
x.InsertAddNew("Darlene");
x.InsertAddNew("Heather");
x.InsertAddNew("Jennifer");
x.InsertAddNew("Katie");
x.InsertAddNew("Rachel");
x.InsertAddNew("Vicky");
x.Dump("Original Participants");
int N = x.GetCount();
Random myRandom = new Random();
for (int iLoop=0; iLoop<N-1; iLoop++)
{
int iRandomInt=-1;
while (iRandomInt != 1)
{
iRandomInt = myRandom.nextInt()%4;
if (iRandomInt !=1)
{
x.RightShift();
x.Dump("Music playing....");
}
else
{
int midIndexPos = x.GetCount()/2;
String strName = x.IndexerGetAtIndex(midIndexPos);
x.RemoveDelete(midIndexPos);
x.Dump( strName + " has been eliminated !!!!");
}
} //while
} //for iLoop
System.out.println(" Winner is " + x.IndexerGetAtIndex(0));
}
public static void main (String args[])
{
AlanJ x = new AlanJ();
x.Go();
}
}