Commit 7642908a authored by iieliteiisquady's avatar iieliteiisquady

First Commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MathsAssesment</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Program {
public static String[] binaryInput =
{
"0010010",
"0100100",
"0010010",
"0100010",
"0001001",
"0100000",
"0010001",
"1000010",
"1000100",
"0010100"
};
public static String[] hexInput =
{
"87",
"32",
"11",
"43",
"F1",
"97",
"42",
"56",
"FF",
"6B",
};
public static String[] base20Input =
{
"87",
"32",
"11",
"43",
"F1",
"97",
"42",
"56",
"FF",
"6B",
};
public static String[] base3input =
{
"0020010",
"0100200",
"0010010",
"0220010",
"0002201",
"0200020",
"0210021",
"1200010",
"1000100",
"0010100"
};
public static void main(String[] args) {
}
public static int[] ConvertBinaryToInt(String[] binaryArray) {
int[] resultsArray = new int[binaryArray.length];
for(int i=0; i<binaryArray.length; i++) {
String current = binaryArray[i];
int column = 1;
int tot = 0;
for(int x=current.length()-1; x>=0; x--) {
char c = current.charAt(x);
tot = tot + (Character.getNumericValue(c)*column);
column = column * 2;
}
resultsArray[i] = tot;
}
return resultsArray;
}
public static int[] ConvertHexToInt(String[] hexArray) {
int[] resultsArray = new int[hexArray.length];
for (int i=0; i<hexArray.length; i++) {
int column = 1;
int tot = 0;
String current = hexArray[i];
for(int x=current.length()-1; x>=0; x--) {
char c = current.charAt(x);
if (c == 'A') {
tot = tot + (10*column);
}
else if (c == 'B') {
tot = tot + (11*column);
}
else if (c == 'C') {
tot = tot + (12*column);
}
else if (c == 'D') {
tot = tot + (13*column);
}
else if (c == 'E') {
tot = tot + (14*column);
}
else if (c == 'F') {
tot = tot + (15*column);
}
else {
tot = tot + (Character.getNumericValue(c)*column);
}
column = column * 16;
}
resultsArray[i] = tot;
}
return resultsArray;
}
public static int[] Union(int[] intArrayA, int[] intArrayB) {
ArrayList<Integer> unionList = new ArrayList<Integer>();
for(int i=0;i<intArrayA.length;i++) {
unionList.add(intArrayA[i]);
}
for(int x=0;x<intArrayB.length;x++) {
boolean foundA = false;
for(int i=0;i<unionList.size();i++) {
if (unionList.get(i) == intArrayB[x]) {
foundA = true;
}
}
if (foundA == false) {
unionList.add(intArrayB[x]);
}
}
int[] resultsArray = new int[unionList.size()];
for(int i = 0; i<unionList.size(); i++) {
resultsArray[i] = unionList.get(i);
}
return resultsArray;
}
public static int[] Intersection(int[] intArrayA, int[] intArrayB) {
ArrayList<Integer> intersectList = new ArrayList<Integer>();
for(int x=0;x<intArrayA.length;x++) {
boolean foundA = false;
for(int i=0;i<intersectList.size();i++) {
if (intersectList.get(i) == intArrayA[x]) {
foundA = true;
}
}
boolean foundB = false;
for(int f=0;f<intArrayB.length;f++) {
if (intArrayB[f] == intArrayA[x]) {
foundB = true;
}
}
if (foundA == false && foundB == true) {
intersectList.add(intArrayA[x]);
}
}
int[] resultsArray = new int[intersectList.size()];
for(int i = 0; i<intersectList.size(); i++) {
resultsArray[i] = intersectList.get(i);
}
return resultsArray;
}
public static int[] Difference(int[] intArrayA, int[] intArrayB) {
ArrayList<Integer> differenceList = new ArrayList<Integer>();
for(int i=0;i<intArrayA.length;i++) {
boolean found = false;
for(int x=0;x<intArrayB.length;x++) {
if(intArrayA[i] == intArrayB[x]) {
found = true;
continue;
}
}
if(found == false) {
differenceList.add(intArrayA[i]);
}
}
int[] resultsArray = new int[differenceList.size()];
for(int i = 0; i<differenceList.size(); i++) {
resultsArray[i] = differenceList.get(i);
}
return resultsArray;
}
public static int Sum (int[] input) {
int result = 0;
for(int i=0;i<input.length;i++) {
result = result + input[i];
}
return result;
}
public static double Mean (int[] input) {
double output = 0;
for(int i=0;i<input.length;i++) {
output = output + input[i];
}
output=output/input.length;
return output;
}
public static int Range (int[] input) {
int output = 0;
int large = input[0];
int small = input[0];
for(int i=0;i<input.length;i++) {
if(input[i] > large) {
large=input[i];
}
if(input[i]<small) {
small = input[i];
}
}
output = large-small;
return output;
}
public static int[] ConvertBase20ToInt(String[] b20array) {
int[] resultsArray = new int[b20array.length];
for (int i=0; i<b20array.length; i++) {
int column = 1;
int tot = 0;
String current = b20array[i];
for(int x=current.length()-1; x>=0; x--) {
char c = current.charAt(x);
if (c == 'A') {
tot = tot + (10*column);
}
else if (c == 'B') {
tot = tot + (11*column);
}
else if (c == 'C') {
tot = tot + (12*column);
}
else if (c == 'D') {
tot = tot + (13*column);
}
else if (c == 'E') {
tot = tot + (14*column);
}
else if (c == 'F') {
tot = tot + (15*column);
}
else if (c == 'G') {
tot = tot + (16*column);
}
else if (c == 'H') {
tot = tot + (17*column);
}
else if (c == 'I') {
tot = tot + (18*column);
}
else if (c == 'J') {
tot = tot + (19*column);
}
else {
tot = tot + (Character.getNumericValue(c)*column);
}
column = column * 20;
}
resultsArray[i] = tot;
}
return resultsArray;
}
public static int[] ConvertBase3ToInt(String[] b3array) {
int[] resultsArray = new int[b3array.length];
for(int i=0; i<b3array.length; i++) {
String current = b3array[i];
int column = 1;
int tot = 0;
for(int x=current.length()-1; x>=0; x--) {
char c = current.charAt(x);
tot = tot + (Character.getNumericValue(c)*column);
column = column * 3;
}
resultsArray[i] = tot;
}
return resultsArray;
}
public static int Mode (int[] input) {
int output = 0;
int max = 0;
Arrays.sort(input);
for(int i=0;i<input.length;i++) {
int count = 0;
for(int x=i+1;x<input.length;x++) {
if (input[x] == input[i]) {
count++;
}
}
if(count > max) {
max = count;
output = input[i];
}
}
return output;
}
public static double Median (int[] input) {
double output = 0;
Arrays.sort(input);
if(input.length%2==0) {
int n = input.length/2;
output = (double) input[n];
output = output - 0.5;
}
else {
int n = (input.length+1)/2;
output = input[n];
}
return output;
}
public static double StandardDeviation (int[] input) {
double output = 0;
for (int i=0; i<input.length; i++) {
output = output + input[i];
}
double mean = output/input.length;
output = 0;
for(int i=0;i<input.length;i++) {
output = output + ((input[i]-mean)*(input[i]-mean));
}
output = Math.sqrt((output/(input.length-1)));
return output;
}
public static double HarmonicMean (int[] input) {
double output = 0;
double[] arr = new double[input.length];
for(int z=0;z<input.length;z++) {
arr[z] = input[z];
}
for(int i=0;i<input.length;i++) {
output = output + (1/arr[i]);
}
output = input.length/output;
return output;
}
public static double ComplexSumFunctionA (int[] input) {
double output = 0;
double[] arr = new double[input.length];
for(int z=0;z<input.length;z++) {
arr[z] = (double) input[z];
}
for(int i=0;i<input.length;i++) {
double totalSquare = 1;
for (int x=0; x<i;x++) {
totalSquare = totalSquare*(input[i]/2);
}
output = output + Math.sqrt(totalSquare);
}
output = output/(input.length+3);
return output;
}
public static int[][] MatrixScalarMultiplication (int[][] matrix, int scalar){
int[][] resultMatrix = new int[matrix.length][matrix[0].length];
for(int i=0;i<matrix.length;i++) {
for(int x=0;x<matrix[0].length;x++) {
resultMatrix[i][x] = matrix[i][x]*scalar;
}
}
return resultMatrix;
}
public static int[][] ComplexMatrixOperation (int[][] matrixA, int[][] matrixB, int scalar){
int[][] resultMatrix = new int[matrixA.length][matrixA[0].length];
for(int i=0;i<matrixA.length;i++) {
for(int x=0;x<matrixA[0].length;x++) {
resultMatrix[i][x] = (matrixA[i][x]+matrixB[i][x])*scalar;
}
}
return resultMatrix;
}
public static double[] GenerateRandomDistributionForSpecificDie(int iterations) {
double[] resultsArray = new double[5121];
Random rng = new Random(1234);
for(int i = 0; i<iterations; i++) {
int sum = 0;
for (int x = 0; x<20;x++) {
int rand = rng.nextInt(256) + 1;
sum = sum + rand;
}
resultsArray[sum] = resultsArray[sum]+1;
}
for(int i = 0; i<resultsArray.length; i++) {
resultsArray[i] = resultsArray[i]/iterations;
}
return resultsArray;
}
public static double[] GenerateRandomDistribution(int dieSides, int rolls, int iterations) {
double[] resultsArray = new double[(dieSides*rolls)+1];
Random rng = new Random(1234);
for(int i = 0; i<iterations; i++) {
int sum = 0;
for (int x = 0; x<rolls;x++) {
int rand = rng.nextInt(dieSides)+1;
sum = sum + rand;
}
resultsArray[sum] = resultsArray[sum]+1;
}
for(int i = 0; i<resultsArray.length; i++) {
resultsArray[i] = resultsArray[i]/iterations;
}
return resultsArray;
}
public static double GetProbabilityOfResultOrHigher(int dieSides, int rolls, int sum) {
double[] distribution = GenerateRandomDistribution(dieSides,rolls,100000);
double result = 0;
int start = sum;
int end = distribution.length-1;
for(int i=start;i<end;i++) {
result = result + distribution[i];
}
return result;
}
}
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class ProgramTestFixed {
@Test
void testConvertBinaryToInt() {
String[] input = {"1010","110","111","11010101"};
int[] result = Program.ConvertBinaryToInt(input);
assertEquals(result[0],8+2);
assertEquals(result[1],6);
assertEquals(result[2],7);
assertEquals(result[3],1+4+16+64+128);
}
@Test
void testConvertBase3ToInt() {
String[] input = {"2","22","10","112"};
int[] result = Program.ConvertBase3ToInt(input);
assertEquals(result[0],2);
assertEquals(result[1],6+2);
assertEquals(result[2],3);
assertEquals(result[3],14);
}
@Test
void testConvertHexToInt() {
String[] input = {"50","110","FA","B1","F"};
int[] result = Program.ConvertHexToInt(input);
assertEquals(result[0],80);
assertEquals(result[1],272);
assertEquals(result[2],250);
assertEquals(result[3],177);
assertEquals(result[4],15);
}
@Test
void testConvertBase20ToInt() {
//1s, 20s, 400s
String[] input = {"50","110","0","GH"};
int[] result = Program.ConvertBase20ToInt(input);
assertEquals(result[0],100);
assertEquals(result[1],420);
assertEquals(result[2],0);
assertEquals(result[3],(16*20)+17);
}
@Test
void testUnion() {
int[] setA = {1,2,3};
int[] setB = {2,3,4,5};
int[] setC = {1,2,3,4,5};
int[] unionSet = Program.Union(setA, setB);
assertEquals(setC.length,unionSet.length);
for(int i: setC) {
Boolean flag = false;
for(int j : unionSet) {
if(i==j) {flag = true;}
}
assertTrue(flag);
}
}
@Test
void testIntersection() {
int[] setA = {2,4,6,8,10,12,14,16,18,20};
int[] setB = {1,2,4,8,16,32,64};
int[] setC = {2,4,8,16};
int[] interSet = Program.Intersection(setA, setB);
System.out.println(Arrays.toString(interSet));
assertEquals(setC.length,interSet.length);
for(int i: setC) {
Boolean flag = false;
for(int j : interSet) {
if(i==j) {flag = true;}
}
assertTrue(flag);
}
}
@Test
void testDifference() {
int[] setA = {0,1,2,3,4,5,6,7,8,9,10};
int[] setB = {2,4,6,8,10};
int[] setC = {0,1,3,5,7,9};
int[] diffSet = Program.Difference(setA, setB);
assertEquals(setC.length,diffSet.length);
for(int i: setC) {
Boolean flag = false;
for(int j : diffSet) {
if(i==j) {flag = true;}
}
assertTrue(flag);
}
}
@Test
void testSum() {
int[] set = {5,10,15,20,25,30,35,40};
assertEquals(Program.Sum(set),5+10+15+20+25+30+35+40);
}
@Test
void testMean() {
int[] set = {1,2,3,4,5,6,666};
assertEquals(Program.Mean(set),((1d+2d+3d+4d+5d+6d+666d)/7d),0.01);
}
@Test
void testMode() {
int[] set = {1,1,1,1,2,3,4,5,6,7,8,9,10,10,10,2};
assertEquals(Program.Mode(set),1);
}
@Test
void testMedian() {
int[] set = {3,6,7,9,8,6,7,6,5,15};
assertEquals(Program.Median(set),6.5d,0.001);
}
@Test
void testStandardDeviation() {
int[] set = {3,6,7,9,8,6,7,6,5,15};
assertEquals(Program.StandardDeviation(set),3.19d,0.1);
}
@Test
void testRange() {
int[] set = {150,3,7,9,8,7,7,6,5,0};
assertEquals(Program.Range(set),150);
}
@Test
void testHarmonicMean() {
int[] set = {3,6,4};
assertEquals(Program.HarmonicMean(set),(3d/((1d/3d)+(1d/6d)+(1d/4d))),0.1);
}
@Test
void testComplexSumFunctionA() {
int[] set = {3,6,77};
assertEquals(Program.ComplexSumFunctionA(set),
(1d+
Math.pow(6d/2d, 0.5d)+
(77d/2d))
/6
,0.1);
}
@Test
void testMatrixScalar() {
int[][] matrix = {{1,2},
{3,4}};
int[][] mScaled = Program.MatrixScalarMultiplication(matrix,11);
assertEquals(mScaled[0][0],11);
assertEquals(mScaled[0][1],22);
assertEquals(mScaled[1][0],33);
assertEquals(mScaled[1][1],44);
}
@Test
void testComplexMatrixOperation() {
int[][] matrix = {{1,2},
{3,4}};
int[][] matrix2 = Program.ComplexMatrixOperation(matrix,matrix,11);
assertEquals(matrix2[0][0],22);
assertEquals(matrix2[0][1],44);
assertEquals(matrix2[1][0],66);
assertEquals(matrix2[1][1],88);
}
@Test
void testGenerateDistributionForSpecificDie() {
double[] d = Program.GenerateRandomDistributionForSpecificDie(100000);
assertEquals(d[2479],0.0012,0.0001);
}
@Test
void testGenerateRandomDistribution() {
assertEquals(Program.GenerateRandomDistribution(50,5,100000)[89],0.0062,0.0001);
}
@Test
void testGetProbabilityOfResultOrHigher() {
double d = Program.GetProbabilityOfResultOrHigher(50,10,300);
assertEquals(d,0.16947000000000007,0.0001);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment