2009 Number Cube
<< Stats | APQuestionsTrailIndex | 2009StockpileCritter >>
NumberCube.java
/**
* Fr C's solution to Number Cube
*
* @author Chris Thiel, OFMCap
* @version 11 May 2009
*/
public class NumberCube
{
public NumberCube()
{
}
/**
* toss
* @return an integer value between 1 and 6, inclusive
*/
public int toss()
{
return 1+(int)(6*Math.random());
}
/** Returns an array of the values obtained by tossing a
* number cube numTosses times.
* @param cube a NumberCube
* @param numTosses the number of tosses to be recorded
* Precondition: numTosses > 0
* @return an array of numTosses values
*/
public static int[] getCubeTosses(NumberCube cube, int numTosses)
{
//part a solution
int[] result=new int[numTosses];
for(int i=0; i < numTosses; i++)
{
result[i]=cube.toss();
}
return result;
}
/** Returns the starting index of a longest run of two or more
* consecutive repeated values
* in the array values.
* @param values an array of integer values representing a series of number cube tosses
* Precondition: values.length > 0
@return the starting index of a run of maximum size;
* -1 if there is no run
*/
public static int getLongestRun(int[] values)
{
//part b solution:
int result=-1;//presume no runs
int count=1;
for(int i=0; i< values.length-1; i++)
{
if(values[i]==values[i+1])
{//we have a match
count++;
} else {
if (count>result) result=count;
count=1; //reset
}
}
if (count>result) result=count;//in case the last entry is part of the longest run
if (result==1) return -1; // a run of one isn't much of a run
return result;
}
}
NumberCubeTester.java
public class NumberCubeTester
{
public static void main(String[]args)
{
NumberCube c= new NumberCube();
int[] v= new int[18];
//part a test
v=c.getCubeTosses(c, 18);
System.out.println("The result from getCubeTosses:");
for(int i:v) System.out.print(i+" ");
System.out.println();
//part b test
int[] b = {1,5,5,4,3,1,2,2,2,2,6,1,3,3,5,5,5,5};
for(int i:b) System.out.print(i+" ");
System.out.println("\nLongest run should be 4:");
System.out.println(c.getLongestRun(b));
System.out.println("\nLongest run of the random one:");
for(int i:v) System.out.print(i+" ");
System.out.print("\nis :");
System.out.println(c.getLongestRun(v));
int[] zilch = {1,2,3};
System.out.println("\nLongest run of zilch should be -1:");
System.out.println(c.getLongestRun(zilch));
}
}
Marvin of Schway's solution
Number one
public class NumberCube
{
/**@return and integer value between 1 and 6, inclusive
*/
public int toss()
{return (Math.random()*5)+1}
/** Returns an array of the values obtained by tossing a number cube numTosses
* times.
* @param cube a NumberCube
* @param numTosses the number of tosses to be recorded
* Precondition: numTosses > 0
* @return an array of numTosses values
*/
public static int[] getCubeTosses (NumberCube cube, int numTosses)
{ int [] chocolate = new int [numTosses];
for (int g = 0; g< numTosses; g++0
chocolate [g] = cube.toss();
return chocolate;
}
/** Returns the starting index of the longest run of two or more consecutive
* repeated values in the array values
* @param values an array of integer values representing a series of number
* cube tosses
* Precondition: values.length > 0
* @return the starting index of a run of maximum size;
* -1 if there is no run
*/
public static int getLongestRun (int[] values)
{ int runDex = -1;
int runlong = 0;
int q,m;
for (int h = 0; h<values.length; h++)
{ q=h;
if (value[h] = value[h+1])
{ m=2;
while (value[q] = value [q+1])
{ m++;
q++;
}
if(m>rundex)
rundex = m;
}
}
return rundex;
}
