first commit
This commit is contained in:
252
CS1322/p1/ArrayTest.java
Normal file
252
CS1322/p1/ArrayTest.java
Normal file
@@ -0,0 +1,252 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ArrayTest.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the ArrayTest class
|
||||
* 1.1 Sep. 02, 2002
|
||||
* Compiled, Tested, Coding Done
|
||||
* 1.2 Sep. 05, 2002
|
||||
* Thanks to a failed WinXP SP1 Install, had to recomment
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.2, Sep. 05, 2002
|
||||
*/
|
||||
|
||||
public class ArrayTest {
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Loop through array and return sum
|
||||
*@return sum of array elements
|
||||
*/
|
||||
public int sum(int intArray[]){
|
||||
int sum = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum");
|
||||
}
|
||||
|
||||
//function code
|
||||
for(int i=0;i<intArray.length; i++){
|
||||
sum += intArray[i];
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum");
|
||||
}
|
||||
|
||||
return sum;
|
||||
} //end of sum(int intArray[]);
|
||||
|
||||
/**
|
||||
*create array of size "size" and set set each element .1 higher
|
||||
*than the previous starting with 0.0
|
||||
*@return array with each element 0.1 larger than the previous
|
||||
*/
|
||||
public double[] buildArray(int size){
|
||||
double[] array = new double[size];
|
||||
array[0] = 0.0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration buildArray");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i = 1; i<array.length;i++){
|
||||
array[i] = array[i-1] + 0.1;
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration buildArray");
|
||||
}
|
||||
return array;
|
||||
} //end of buildArray(int size)
|
||||
|
||||
/**
|
||||
*Create an array containing the smallest and largest element of intArray
|
||||
*@return the array containing the smallest and largest element of intArray
|
||||
*/
|
||||
public int[] bounds(int intArray[]){
|
||||
int min = intArray[0], max = intArray[0];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration bounds");
|
||||
}
|
||||
|
||||
//actual code, again
|
||||
for(int i = 0; i<intArray.length;i++){
|
||||
if(intArray[i] > max){
|
||||
max = intArray[i];
|
||||
}
|
||||
if(intArray[i] < min){
|
||||
min = intArray[i];
|
||||
}
|
||||
} //end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration bounds");
|
||||
}
|
||||
|
||||
int bounds[] = new int[2];
|
||||
bounds[0] = min;
|
||||
bounds[1] = max;
|
||||
return bounds;
|
||||
} //end bounds(int intArray[])
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/////////////////
|
||||
//From Phase IV//
|
||||
/////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
/**
|
||||
*Traverse through twoDArray
|
||||
*@return the sum of the elements of twoDArray
|
||||
*/
|
||||
public static int sum(int twoDArray[][]){
|
||||
int sum=0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum(twoDArray)");
|
||||
}
|
||||
|
||||
for(int row=0; row<twoDArray.length; row++){
|
||||
for(int col=0; col<twoDArray[row].length; col++){
|
||||
sum += twoDArray[row][col];
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum(twoDArray)");
|
||||
}
|
||||
return sum;
|
||||
}//end of sum(int twoDArray[][])
|
||||
|
||||
/**
|
||||
*Traverse grid
|
||||
*@return the number of "true's"
|
||||
*/
|
||||
public static int[] getRowCounts(boolean grid[][]){
|
||||
int[] total = new int[grid.length];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration getRowCounts");
|
||||
}
|
||||
|
||||
for(int row=0; row<grid.length; row++){
|
||||
total[row] = 0;
|
||||
for(int col=0; col<grid[row].length; col++){
|
||||
if(grid[row][col] == true){
|
||||
total[row]++;
|
||||
}
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration getRowCounts");
|
||||
}
|
||||
return total;
|
||||
}//end of getRowCounts
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ArrayTest.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
//////////////////
|
||||
//test Phase III//
|
||||
//////////////////
|
||||
ArrayTest arraytest = new ArrayTest();
|
||||
|
||||
//test sum()
|
||||
int[] array = new int[10];
|
||||
for(int i = 0; i<array.length;i++){
|
||||
array[i] = i+1;
|
||||
}
|
||||
|
||||
System.out.println(arraytest.sum(array));
|
||||
|
||||
//test buildArray()
|
||||
double[] array1;
|
||||
array1 = arraytest.buildArray(8);
|
||||
for(int i=0;i<array1.length;i++){
|
||||
System.out.println(array1[i]);
|
||||
}
|
||||
|
||||
//test bounds
|
||||
int[] array2;
|
||||
array2 = arraytest.bounds(array);
|
||||
for(int i = 0; i<array2.length;i++){
|
||||
System.out.println(array2[i]);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
//from Phase IV//
|
||||
//////////////////
|
||||
|
||||
System.out.println("\nPhase IV begins here\n");
|
||||
|
||||
/////////////////
|
||||
//test Phase IV//
|
||||
/////////////////
|
||||
int[][] array2D = new int[5][4];
|
||||
|
||||
//test sum(twoDArray())
|
||||
for(int row=0; row<array2D.length; row++){
|
||||
for(int col=0; col<array2D[row].length; col++){
|
||||
array2D[row][col] = row+col;
|
||||
}
|
||||
}
|
||||
System.out.println(arraytest.sum(array2D) + "\n");
|
||||
|
||||
//test getRowCounts()
|
||||
boolean[][] array2D_1 = new boolean[4][5];
|
||||
for(int row=0; row<array2D_1.length; row++){
|
||||
for(int col=0; col<array2D_1[row].length; col++){
|
||||
if(col%2 != 0){
|
||||
array2D_1[row][col] = false;
|
||||
}
|
||||
else { //col%2 == 0
|
||||
array2D_1[row][col] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
int[] truthtellers = arraytest.getRowCounts(array2D_1);
|
||||
for(int i=0; i<truthtellers.length; i++){
|
||||
System.out.println(truthtellers[i]);
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class ArrayTest
|
||||
Reference in New Issue
Block a user