REF 6I Comprehensive review of Arrays by Dane Henry
/**
* Write a description of class MyArray here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyArray
{
// instance variables - replace the example below with your own
public MyArray()
{
}
// Method to reverse element in an array
public int[] reverseArray(int[] numbers)
{
int[] reverse = new int[numbers.length];
int count = numbers.length;
for (int a = 0; a < numbers.length; a++)
{
reverse[a] = numbers[count - 1];
count--;
}
return reverse;
}
// Combining two arrays into one
public int[] mergeTwoArrays(int[] arrayOne, int[] arrayTwo)
{
int oneCount = 0;
int twoCount = 0;
int total = arrayOne.length + arrayTwo.length;
int[] mergeArray = new int[total];
for (int a = 0; a < arrayOne.length; a++)
{
mergeArray[a] = arrayOne[a];
oneCount++;
}
for (int b = oneCount; b < total; b++)
{
mergeArray[b] = arrayTwo[twoCount];
twoCount++;
}
return mergeArray;
}
// Splitting one array into two
public void splitArray(int[] arrayOne, int[] arrayTwo, int[] arrayThree)
{
int counter = 0;
int countTwo = 0;
int countThree = 0;
countTwo = arrayOne.length / 2;
for (int a = 0; a < countTwo; a++)
{
arrayTwo[a] = arrayOne[a];
}
for (int b = countTwo; b < arrayOne.length; b++)
{
arrayThree[counter] = arrayOne[b];
counter++;
}
}
// Adding all the elements in array and dividing by the # of elements to get the average
public double findAvg(int[] scores)
{
double average = 0.0;
double tempTotal = 0.0;
int count = 0;
for (int a = 0; a < scores.length; a++)
{
tempTotal += scores[a];
count++;
}
average = (double)(tempTotal/count);
return average;
}
// Comparing two arrays to see if they are identical
// both in size and by each element
public boolean isIdentical(int[] arrayOne, int[] arrayTwo)
{
boolean b = false;
if (arrayOne.length == arrayTwo.length)
{
for (int a = 0; a < arrayOne.length; a++)
{
if (arrayOne[a] == arrayTwo[a])
{
b = true;
}
else
{
b = false;
}
}
}
return b;
}
// Test to see whether 2 2-dimesional arrays can be multiplied
public boolean isMultiplyCompatible(int[][] arrayOne, int[][] arrayTwo)
{
int a = 0;
if(arrayOne[0].length == arrayTwo.length)
{
return true;
}
else
{
return false;
}
}
// Multipling two arrays together
public int[][] matrixMultiply(int[][] arrayOne, int[][] arrayTwo)
{
int a = arrayOne.length;
int b = arrayTwo[0].length;
int[][] arrayThree = new int[a][b];
int tempTotal;
for (int c = 0; c < arrayOne.length; c++)
{
for (int d = 0; d < arrayTwo[c].length; d++)
{
tempTotal = 0;
for (int e = 0; e < arrayOne[c].length; e++)
{
if((c == 0) && (d == 0))
{
tempTotal += arrayOne[c][e] * arrayTwo[e][c];
}
else
{
tempTotal += arrayOne[c][e] * arrayTwo[e][d];
}
}
arrayThree[c][d] = tempTotal;
}
}
return arrayThree;
}
}
*****************************************************************************************
import java.util.*;
import java.io.*;
import java.lang.*;
/**
* Write a description of class MyArrayTest here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyArrayTest extends Thread
{
public MyArrayTest()
{
}
public static void main(String[] args)
{
MyArray myArray = new MyArray();
// Declarations of variables
// Giving values to variables
Thread thread = new Thread();
// For reverseArray Method (Step 1)
int[] one = new int[1];
int[] two = new int[2];
int[] three = new int[3];
int[] hundred = new int[100];
for (int a = 0; a < one.length; a++)
{
one[a] = (int)(Math.random()*200);
}
for (int b = 0; b < two.length; b++)
{
two[b] = (int)(Math.random()*200);
}
for (int c = 0; c < three.length; c++)
{
three[c] = (int)(Math.random()*200);
}
for (int d = 0; d < hundred.length; d++)
{
hundred[d] = (int)(Math.random()*200);
}
// For mergeTwoArrays method (Step 2)
int[] mergeOne = new int[1];
int[] mergeTwo = new int[1];
int[] mergeThree = new int[20];
for (int o = 0; o < mergeOne.length; o++)
{
mergeOne[o] = (int)(Math.random()*200);
mergeTwo[o] = (int)(Math.random()*500);
}
for (int p = 0; p < mergeThree.length; p++)
{
mergeThree[p] = (int)(Math.random()*200);
}
// For splitArray method (Step 3)
int[] arraySplit = new int[50];
int[] arraySplitTwo = new int[99];
int[] arraySplitThree = new int[1];
int[] arraySplitA = new int[arraySplit.length / 2];
int[] arraySplitB = new int[arraySplit.length / 2];
int[] arraySplitTwoA = new int[arraySplitTwo.length / 2];
int[] arraySplitTwoB = new int[(arraySplitTwo.length / 2) + 1];
int[] arraySplitThreeA = new int[1];
int[] arraySplitThreeB = new int[1];
for (int t = 0; t < arraySplit.length; t++)
{
arraySplit[t] = (int)(Math.random()*2000);
}
for (int u = 0; u < arraySplitTwo.length; u++)
{
arraySplitTwo[u] = (int)(Math.random()*2000);
}
for (int v = 0; v < arraySplitThree.length; v++)
{
arraySplitThree[v] = (int)(Math.random()*2000);
}
// For findAvg method (Step 4)
double avg = 0.0;
double avgTwo = 0.0;
double avgThree = 0.0;
int[] averageOne = new int[1];
int[] averageTwo = new int[50];
int[] averageThree = new int[100];
for (int g = 0; g < averageOne.length; g++)
{
averageOne[g] = (int)(Math.random()*2000);
}
for (int h = 0; h < averageTwo.length; h++)
{
averageTwo[h] = (int)(Math.random()*2000);
}
for (int i = 0; i < averageThree.length; i++)
{
averageThree[i] = (int)(Math.random()*2000);
}
// For isIdentical method (Step 5)
boolean oneIdentity;
boolean twoIdentity;
int[] identityOne = new int[20];
int[] identityTwo = new int[20];
int[] identityThree = new int[30];
for (int n = 0; n < identityOne.length; n++)
{
identityOne[n] = (int)(Math.random()*200);
identityTwo[n] = identityOne[n];
}
for (int o = 0; o < identityThree.length; o++)
{
identityThree[o] = (int)(Math.random()*200);
}
// For isMultiplyCompatible (Step 6)
int[][] multiOne = new int[2][3];
int[][] multiTwo = new int[3][2];
int[][] multiThree = new int[2][5];
boolean isMultiOne;
boolean isMultiTwo;
for (int a = 0; a < multiOne.length; a++)
{
for (int b = 0; b < multiOne[a].length; b++)
{
multiOne[a][b] = (int)(Math.random()*200);
}
}
for (int a = 0; a < multiTwo.length; a++)
{
for (int b = 0; b < multiTwo[a].length; b++)
{
multiTwo[a][b] = (int)(Math.random()*200);
}
}
for (int a = 0; a < multiThree.length; a++)
{
for (int b = 0; b < multiThree[a].length; b++)
{
multiThree[a][b] = (int)(Math.random()*200);
}
}
// For matrixMultiply method (Step 7)
int[][] matrixOne = new int[2][3];
int[][] matrixTwo = new int[3][2];
int[][] matrixThree = new int[3][2];
int[][] matrixAnswerOne = new int[matrixOne.length][matrixTwo[0].length];
int[][] matrixAnswerTwo = new int[matrixOne.length][matrixThree[0].length];
for (int a = 0; a < matrixOne.length; a++)
{
for (int b = 0; b < matrixOne[a].length; b++)
{
matrixOne[a][b] = (int)(Math.random()*300);
}
}
for (int a = 0; a < matrixTwo.length; a++)
{
for (int b = 0; b < matrixTwo[a].length; b++)
{
matrixTwo[a][b] = (int)(Math.random()*300);
}
}
for (int a = 0; a < matrixThree.length; a++)
{
for (int b = 0; b < matrixThree[a].length; b++)
{
matrixThree[a][b] = (int)(Math.random()*300);
}
}
// Begin Output
// For reverseArray Method (Step 1)
System.out.println("To show output of reverseArray Method:");
System.out.println("Output of array with 1 element:");
for (int e = 0; e < one.length; e++)
{
System.out.print(one[e]);
}
System.out.println("\n" + "Output of array with 1 element reversed:");
int[] oneReverse = new int[1];
oneReverse = myArray.reverseArray(one);
for (int i = 0; i < oneReverse.length; i++)
{
System.out.print(oneReverse[i]);
}
System.out.println("\n");
System.out.println("Output of array with 2 elements: ");
for (int f = 0; f < two.length; f++)
{
System.out.print(two[f] + " ");
}
System.out.println("\n" + "Output of array with 2 element reversed:");
int[] twoReverse = new int[2];
twoReverse = myArray.reverseArray(two);
for (int j = 0; j < twoReverse.length; j++)
{
System.out.print(twoReverse[j] + " ");
}
System.out.println("\n");
System.out.println("Output of array with 3 elements: ");
for (int k = 0; k < three.length; k++)
{
System.out.print(three[k] + " ");
}
System.out.println("\n" + "Output of array with 3 element reversed:");
int[] threeReverse = new int[3];
threeReverse = myArray.reverseArray(three);
for (int l = 0; l < threeReverse.length; l++)
{
System.out.print(threeReverse[l] + " ");
}
System.out.println("\n");
System.out.println("Output of array with 100 elements: ");
for (int m = 0; m < hundred.length; m++)
{
System.out.print(hundred[m] + " ");
}
System.out.println("\n" + "Output of array with 100 element reversed:");
int[] hundredReverse = new int[100];
hundredReverse = myArray.reverseArray(hundred);
for (int n = 0; n < hundredReverse.length; n++)
{
System.out.print(hundredReverse[n] + " ");
}
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For mergeTwoArrays method (Step 2)
int[] mergedArrayOne = new int[mergeOne.length + mergeTwo.length];
mergedArrayOne = myArray.mergeTwoArrays(mergeOne, mergeTwo);
System.out.println("To show output of mergeTwoArrays method: ");
System.out.println("mergeOne: " + mergeOne[0] + "\nmergeTwo: " + mergeTwo[0]);
System.out.print("mergedArrayOne: ");
for (int q = 0; q < mergedArrayOne.length; q++)
{
System.out.print(mergedArrayOne[q] + " ");
}
System.out.println("\n");
int[] mergedArrayTwo = new int[mergeOne.length + mergeThree.length];
mergedArrayTwo = myArray.mergeTwoArrays(mergeOne, mergeThree);
System.out.println("mergeOne: " + mergeOne[0]);
System.out.print("mergeThree: ");
for (int r = 0; r < mergeThree.length; r++)
{
System.out.print(mergeThree[r] + " ");
}
System.out.print("\nmergedArrayTwo: ");
for (int s = 0; s < mergedArrayTwo.length; s++)
{
System.out.print(mergedArrayTwo[s] + " ");
}
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For splitArray method (Step 3)
myArray.splitArray(arraySplit, arraySplitA, arraySplitB);
myArray.splitArray(arraySplitTwo, arraySplitTwoA, arraySplitTwoB);
myArray.splitArray(arraySplitThree, arraySplitThreeA, arraySplitThreeB);
System.out.println("To show output the splitArray methods:");
System.out.print("arraySplit: ");
for (int w = 0; w < arraySplit.length; w++)
{
System.out.print(arraySplit[w] + " ");
}
System.out.print("\narraySplitA: ");
for (int x = 0; x < arraySplitA.length; x++)
{
System.out.print(arraySplitA[x] + " ");
}
System.out.print("\narraySplitB: ");
for (int y = 0; y < arraySplitB.length; y++)
{
System.out.print(arraySplitB[y] + " ");
}
System.out.println("\n");
System.out.print("arraySplitTwo: ");
for (int a = 0; a < arraySplitTwo.length; a++)
{
System.out.print(arraySplitTwo[a] + " ");
}
System.out.print("\narraySplitTwoA: ");
for (int b = 0; b < arraySplitTwoA.length; b++)
{
System.out.print(arraySplitTwoA[b] + " ");
}
System.out.print("\narraySplitTwoB: ");
for (int c = 0; c < arraySplitTwoB.length; c++)
{
System.out.print(arraySplitTwoB[c] + " ");
}
System.out.println("\n");
System.out.print("arraySplitThree: ");
for (int d = 0; d < arraySplitThree.length; d++)
{
System.out.print(arraySplitThree[d] + " ");
}
System.out.print("\narraySplitThreeA: ");
for (int e = 0; e < arraySplitThreeA.length; e++)
{
System.out.print(arraySplitThreeA[e] + " ");
}
System.out.print("\narraySplitThreeB: ");
for (int f = 0; f < arraySplitThreeB.length; f++)
{
System.out.print(arraySplitThreeB[f] + " ");
}
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For findAvg method (Step 4)
avg = myArray.findAvg(averageOne);
avgTwo = myArray.findAvg(averageTwo);
avgThree = myArray.findAvg(averageThree);
System.out.println("To show the output of the findAvg method: ");
System.out.print("averageOne: ");
for (int j = 0; j < averageOne.length; j++)
{
System.out.print(averageOne[j] + " ");
}
System.out.println("\nAverage of averageOne: " + avg + "\n");
System.out.print("averageTwo: ");
for (int k = 0; k < averageTwo.length; k++)
{
System.out.print(averageTwo[k] + " ");
}
System.out.println("\nAverage of averageTwo: " + avgTwo + "\n");
System.out.print("averageThree: ");
for (int l = 0; l < averageThree.length; l++)
{
System.out.print(averageThree[l] + " ");
}
System.out.println("\nAverage of averageThree: " + avgThree + "\n");
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For isIdentical method (Step 5)
oneIdentity = myArray.isIdentical(identityOne, identityTwo);
twoIdentity = myArray.isIdentical(identityOne, identityThree);
System.out.println("To show output from the isIndetical method: ");
System.out.print("identityOne: ");
for (int a = 0; a < identityOne.length; a++)
{
System.out.print(identityOne[a] + " ");
}
System.out.print("\nidentityTwo: ");
for (int a = 0; a < identityTwo.length; a++)
{
System.out.print(identityTwo[a] + " ");
}
System.out.println("\nAre identityOne and identityTwo the same: " + oneIdentity + "\n");
System.out.print("identityOne: ");
for (int a = 0; a < identityOne.length; a++)
{
System.out.print(identityOne[a] + " ");
}
System.out.print("\nidentityThree: ");
for (int a = 0; a < identityThree.length; a++)
{
System.out.print(identityThree[a] + " ");
}
System.out.println("\nAre identityOne and identityThree the same: " + twoIdentity + "\n");
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For isMultiplyCompatible (Step 6)
isMultiOne = myArray.isMultiplyCompatible(multiOne, multiTwo);
isMultiTwo = myArray.isMultiplyCompatible(multiOne, multiThree);
System.out.println("To show output from the isMultiplyCompatible method: ");
System.out.print("multiOne: \n");
for (int a = 0; a < multiOne.length; a++)
{
for (int b = 0; b < multiOne[a].length; b++)
{
System.out.print(multiOne[a][b] + " ");
}
System.out.print("\n");
}
System.out.print("\nmultiTwo: \n");
for (int a = 0; a < multiTwo.length; a++)
{
for (int b = 0; b < multiTwo[a].length; b++)
{
System.out.print(multiTwo[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nAre multiOne and multiTwo multipliable: " + isMultiOne + "\n");
System.out.print("multiOne: \n");
for (int a = 0; a < multiOne.length; a++)
{
for (int b = 0; b < multiOne[a].length; b++)
{
System.out.print(multiOne[a][b] + " ");
}
System.out.print("\n");
}
System.out.print("\nmultiThree: \n");
for (int a = 0; a < multiThree.length; a++)
{
for (int b = 0; b < multiThree[a].length; b++)
{
System.out.print(multiThree[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nAre multiOne and multiThree multipliable: " + isMultiTwo + "\n");
System.out.println("\nProgram waiting 15 seconds for you to read output...\n\n\n\n\n\n\n\n\n");
try
{
thread.sleep(15000);
}
catch (InterruptedException e)
{
}
// For matrixMultiply method (Step 7)
boolean isMatrixOne = myArray.isMultiplyCompatible(matrixOne, matrixTwo);
boolean isMatrixTwo = myArray.isMultiplyCompatible(matrixOne, matrixThree);
boolean isMatrixThree = myArray.isMultiplyCompatible(matrixTwo, matrixThree);
System.out.println("To show the output for the matrixMultiply method: ");
if (isMatrixOne == true)
{
matrixAnswerOne = myArray.matrixMultiply(matrixOne, matrixTwo);
System.out.println("matrixOne: ");
for (int a = 0; a < matrixOne.length; a++)
{
for (int b = 0; b < matrixOne[a].length; b++)
{
System.out.print(matrixOne[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nmatrixTwo: ");
for (int a = 0; a < matrixTwo.length; a++)
{
for (int b = 0; b < matrixTwo[a].length; b++)
{
System.out.print(matrixTwo[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nmatrixAnswerOne: ");
for (int a = 0; a < matrixAnswerOne.length; a++)
{
for (int b = 0; b < matrixAnswerOne[a].length; b++)
{
System.out.print(matrixAnswerOne[a][b] + " ");
}
System.out.print("\n");
}
}
if (isMatrixTwo == true)
{
matrixAnswerTwo = myArray.matrixMultiply(matrixOne, matrixThree);
System.out.println("\nmatrixOne: ");
for (int a = 0; a < matrixOne.length; a++)
{
for (int b = 0; b < matrixOne[a].length; b++)
{
System.out.print(matrixOne[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nmatrixThree: ");
for (int a = 0; a < matrixThree.length; a++)
{
for (int b = 0; b < matrixThree[a].length; b++)
{
System.out.print(matrixThree[a][b] + " ");
}
System.out.print("\n");
}
System.out.println("\nmatrixAnswerTwo: ");
for (int a = 0; a < matrixAnswerTwo.length; a++)
{
for (int b = 0; b < matrixAnswerTwo[a].length; b++)
{
System.out.print(matrixAnswerTwo[a][b] + " ");
}
System.out.print("\n");
}
}
if (isMatrixThree == true)
{
System.out.println("\nThis multiplication can occur.");
}
else
{
System.out.println("\nThe multiplication of matrixTwo and matrixThree cannot occur becuase of a mis-matched size.");
}
}
}