Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
Maths Sem1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jack.templeman
Maths Sem1
Commits
a063e2e9
Commit
a063e2e9
authored
Nov 30, 2018
by
jackt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finito
parent
4176483b
Pipeline
#360
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
861 additions
and
266 deletions
+861
-266
Program.java
Maths assessment/src/Program.java
+675
-101
ProgramTest.java
Maths assessment/src/ProgramTest.java
+186
-165
No files found.
Maths assessment/src/Program.java
View file @
a063e2e9
...
...
@@ -2,6 +2,7 @@ import java.awt.List;
import
java.util.Arrays
;
import
java.util.ArrayList
;
import
java.util.Random
;
import
java.lang.Math
;
public
class
Program
{
...
...
@@ -23,109 +24,197 @@ public class Program {
public
static
int
[]
ConvertBinaryToInt
(
String
[]
binaryArray
)
{
int
[]
resultsArray
=
new
int
[
binaryArray
.
length
];
{
int
iA
=
0
;
int
iB
=
0
;
int
iD
=
binaryArray
.
length
-
1
;
int
iE
=
0
;
Double
dA
=
0.0
;
for
(
iA
=
0
;
iA
<=
iD
;
iA
++)
{
String
sA
=
binaryArray
[
iA
];
iE
=
binaryArray
[
iA
].
length
()
-
1
;
int
total
=
0
;
int
iC
=
100
;
for
(
iB
=
0
;
iB
<=
iE
;
iB
++)
{
if
(
iC
==
100
)
{
iC
=
iE
;
}
char
cA
=
sA
.
charAt
(
iB
);
String
sB
=
Character
.
toString
(
cA
);
for
(
int
l
=
0
;
l
<
resultsArray
.
length
;
l
++
)
{
switch
(
sB
)
{
String
binary
=
binaryArray
[
l
];
String
character
=
""
;
int
total
=
0
;
int
b
=
binary
.
length
()
-
1
;
int
c
=
l
;
int
d
=
resultsArray
[
0
]
;
int
e
=
resultsArray
[
1
];
int
f
=
resultsArray
[
2
];
int
g
=
resultsArray
[
3
]
;
for
(
int
a
=
0
;
a
<=
b
;
a
++)
{
if
(
a
==
0
)
{
total
=
0
;
case
"0"
:
{
dA
=
Math
.
pow
(
2
,
iC
)
*
0
;
total
=
total
+
dA
.
intValue
()
;
resultsArray
[
iA
]
=
total
;
iC
--
;
continue
;
}
case
"1"
:
{
dA
=
Math
.
pow
(
2
,
iC
)
*
1
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
}
}
char
theChar
=
binary
.
charAt
(
a
)
;
character
=
Character
.
toString
(
theChar
);
}
return
resultsArray
;
}
if
(
a
==
0
&&
character
.
equals
(
"1"
))
{
total
=
total
++;
}
if
(
character
.
equals
(
"0"
)
&&
a
==
0
)
{
continue
;
}
}
public
static
int
[]
ConvertHexToInt
(
String
[]
hexArray
)
{
int
[]
resultsArray
=
new
int
[
hexArray
.
length
];
{
int
iA
=
0
;
int
iB
=
0
;
int
iD
=
hexArray
.
length
-
1
;
int
iE
=
0
;
Double
dA
=
0.0
;
for
(
iA
=
0
;
iA
<=
iD
;
iA
++)
{
String
sA
=
hexArray
[
iA
];
iE
=
hexArray
[
iA
].
length
()
-
1
;
int
total
=
0
;
int
iC
=
100
;
for
(
iB
=
0
;
iB
<=
iE
;
iB
++)
{
if
(
iC
==
100
)
{
iC
=
iE
;
}
char
cA
=
sA
.
charAt
(
iB
);
String
sB
=
Character
.
toString
(
cA
);
switch
(
a
)
{
case
1
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
2
;
resultsArray
[
l
]
=
total
;
switch
(
sB
)
{
case
"0"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
0
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
2
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
4
;
resultsArray
[
l
]
=
total
;
case
"1"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
1
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
}
case
"2"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
2
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
case
3
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
8
;
resultsArray
[
l
]
=
total
;
}
case
"3"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
3
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
}
case
"4"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
4
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
case
4
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
16
;
resultsArray
[
l
]
=
total
;
}
case
"5"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
5
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
}
case
"6"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
6
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
case
5
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
32
;
resultsArray
[
l
]
=
total
;
}
case
"7"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
7
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
}
case
"8"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
8
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
case
6
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
64
;
resultsArray
[
l
]
=
total
;
}
case
"9"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
9
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
}
case
"A"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
10
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
case
7
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
128
;
resultsArray
[
l
]
=
total
;
}
case
"B"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
11
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
else
break
;
case
8
:
if
(
character
.
equals
(
"1"
))
{
total
=
total
+
256
;
resultsArray
[
l
]
=
total
;
}
case
"C"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
12
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--
;
continue
;
}
case
"D"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
13
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"E"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
14
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"F"
:
{
dA
=
Math
.
pow
(
16
,
iC
)
*
15
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
}
}
resultsArray
[
l
-
1
]
=
total
;
}
return
resultsArray
;
}
return
resultsArray
;
}
public
static
int
[]
ConvertHexToInt
(
String
[]
hexArray
)
{
int
[]
resultsArray
=
new
int
[
hexArray
.
length
];
return
resultsArray
;
}
public
static
int
[]
Union
(
int
[]
intArrayA
,
int
[]
intArrayB
)
{
...
...
@@ -133,12 +222,55 @@ public class Program {
ArrayList
<
Integer
>
unionList
=
new
ArrayList
<
Integer
>();
int
[]
resultsArray
=
new
int
[
unionList
.
size
()];
for
(
int
i
=
0
;
i
<
unionList
.
size
();
i
++)
{
resultsArray
[
i
]
=
unionList
.
get
(
i
);
int
currentCount
=
0
;
int
iVal
=
0
;
int
jVal
=
0
;
int
z
=
0
;
int
y
=
0
;
int
a
=
intArrayB
.
length
;
for
(
int
i
=
0
;
i
<
intArrayA
.
length
;
i
++)
{
z
=
intArrayA
[
i
];
iVal
=
0
;
if
(
i
==
0
&&
unionList
.
size
()
==
0
)
{
unionList
.
add
(
intArrayA
[
i
]);
continue
;
}
if
(
i
!=
0
)
{
for
(
int
k
=
0
;
k
<
unionList
.
size
();
k
++)
{
if
(
intArrayA
[
i
]
==
unionList
.
get
(
k
))
{
iVal
++;
}
}
if
(
iVal
==
0
)
{
unionList
.
add
(
intArrayA
[
i
]);
}
}
}
for
(
int
i
=
0
;
i
<
intArrayB
.
length
;
i
++)
{
;
iVal
=
0
;
for
(
int
k
=
0
;
k
<
unionList
.
size
();
k
++)
{
int
proof
=
unionList
.
get
(
k
);
int
compare
=
intArrayB
[
i
];
if
(
intArrayB
[
i
]
==
unionList
.
get
(
k
))
{
iVal
++;
}
}
if
(
iVal
==
0
)
{
unionList
.
add
(
intArrayB
[
i
]);
}
}
resultsArray
=
unionList
.
stream
().
mapToInt
(
g
->
g
).
toArray
();
Arrays
.
sort
(
resultsArray
);
return
resultsArray
;
}
public
static
int
[]
Intersection
(
int
[]
intArrayA
,
int
[]
intArrayB
)
{
...
...
@@ -146,23 +278,69 @@ public class Program {
ArrayList
<
Integer
>
intersectList
=
new
ArrayList
<
Integer
>();
int
[]
resultsArray
=
new
int
[
intersectList
.
size
()];
for
(
int
i
=
0
;
i
<
intersectList
.
size
();
i
++)
{
resultsArray
[
i
]
=
intersectList
.
get
(
i
);
int
currentCount
=
0
;
int
iVal
=
0
;
int
jVal
=
0
;
int
z
=
0
;
int
y
=
0
;
int
a
=
intArrayB
.
length
;
for
(
int
i
=
0
;
i
<
intArrayA
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
intArrayB
.
length
;
j
++)
{
z
=
intArrayA
[
i
];
y
=
intArrayB
[
j
];
iVal
=
0
;
if
(
intArrayA
[
i
]
==
intArrayB
[
j
]
&&
i
==
0
)
{
intersectList
.
add
(
intArrayA
[
i
]);
}
if
(
intArrayA
[
i
]
==
intArrayB
[
j
]
&&
i
!=
0
)
{
for
(
int
k
=
0
;
k
<
intersectList
.
size
();
k
++)
{
if
(
intArrayA
[
i
]
==
intersectList
.
get
(
k
))
{
iVal
++;
}
}
if
(
iVal
==
0
)
{
intersectList
.
add
(
intArrayA
[
i
]);
}
}
}
}
resultsArray
=
intersectList
.
stream
().
mapToInt
(
i
->
i
).
toArray
();
return
resultsArray
;
}
public
static
int
[]
Difference
(
int
[]
intArrayA
,
int
[]
intArrayB
)
{
ArrayList
<
Integer
>
differenceList
=
new
ArrayList
<
Integer
>();
int
totalA
=
0
;
int
totalB
=
0
;
int
[]
resultsArray
=
new
int
[
differenceList
.
size
()];
for
(
int
i
=
0
;
i
<
differenceList
.
size
();
i
++)
{
resultsArray
[
i
]
=
differenceList
.
get
(
i
);
int
currentCount
=
0
;
int
iVal
=
0
;
int
jVal
=
0
;
for
(
int
i
=
0
;
i
<
intArrayA
.
length
;
i
++)
{
currentCount
=
0
;
for
(
int
j
=
0
;
j
<
intArrayB
.
length
;
j
++)
{
iVal
=
intArrayA
[
i
];
jVal
=
intArrayB
[
j
];
if
(
iVal
==
jVal
)
{
currentCount
++;
continue
;
}
}
if
(
currentCount
==
0
)
{
differenceList
.
add
(
iVal
);
}
}
resultsArray
=
differenceList
.
stream
().
mapToInt
(
i
->
i
).
toArray
();
Arrays
.
sort
(
resultsArray
);
return
resultsArray
;
}
...
...
@@ -170,6 +348,12 @@ public class Program {
public
static
int
Sum
(
int
[]
input
)
{
int
result
=
0
;
int
length
=
0
;
length
=
input
.
length
;
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
result
+=
input
[
i
];
}
return
result
;
...
...
@@ -178,7 +362,13 @@ public class Program {
public
static
double
Mean
(
int
[]
input
)
{
double
output
=
0
;
int
length
=
input
.
length
;
double
total
=
0
;
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
total
+=
input
[
i
];
}
output
=
total
/
length
;
return
output
;
}
...
...
@@ -186,13 +376,184 @@ public class Program {
int
[]
resultsArray
=
new
int
[
b20array
.
length
];
int
iA
=
0
;
int
iB
=
0
;
int
iD
=
b20array
.
length
-
1
;
int
iE
=
0
;
Double
dA
=
0.0
;
for
(
iA
=
0
;
iA
<=
iD
;
iA
++)
{
String
sA
=
b20array
[
iA
];
iE
=
b20array
[
iA
].
length
()
-
1
;
int
total
=
0
;
int
iC
=
100
;
for
(
iB
=
0
;
iB
<=
iE
;
iB
++)
{
if
(
iC
==
100
)
{
iC
=
iE
;
}
char
cA
=
sA
.
charAt
(
iB
);
String
sB
=
Character
.
toString
(
cA
);
switch
(
sB
)
{
case
"0"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
0
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"1"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
1
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"2"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
2
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"3"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
3
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"4"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
4
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"5"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
5
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"6"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
6
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"7"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
7
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"8"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
8
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"9"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
9
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"A"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
10
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"B"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
11
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"C"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
12
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"D"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
13
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"E"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
14
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"F"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
15
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"G"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
16
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"H"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
17
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"I"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
18
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"J"
:
{
dA
=
Math
.
pow
(
20
,
iC
)
*
19
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
}
}
}
return
resultsArray
;
}
public
static
int
Range
(
int
[]
input
)
{
int
output
=
0
;
int
length
=
input
.
length
-
1
;
int
output
;
Arrays
.
sort
(
input
);
output
=
input
[
length
]
-
input
[
0
];
return
output
;
}
...
...
@@ -201,6 +562,52 @@ public class Program {
int
[]
resultsArray
=
new
int
[
b3array
.
length
];
int
iA
=
0
;
int
iB
=
0
;
int
iD
=
b3array
.
length
-
1
;
int
iE
=
0
;
Double
dA
=
0.0
;
for
(
iA
=
0
;
iA
<=
iD
;
iA
++)
{
String
sA
=
b3array
[
iA
];
iE
=
b3array
[
iA
].
length
()
-
1
;
int
total
=
0
;
int
iC
=
100
;
for
(
iB
=
0
;
iB
<=
iE
;
iB
++)
{
if
(
iC
==
100
)
{
iC
=
iE
;
}
char
cA
=
sA
.
charAt
(
iB
);
String
sB
=
Character
.
toString
(
cA
);
switch
(
sB
)
{
case
"0"
:
{
dA
=
Math
.
pow
(
3
,
iC
)
*
0
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"1"
:
{
dA
=
Math
.
pow
(
3
,
iC
)
*
1
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
case
"2"
:
{
dA
=
Math
.
pow
(
3
,
iC
)
*
2
;
total
=
total
+
dA
.
intValue
();
resultsArray
[
iA
]
=
total
;
iC
--;
continue
;
}
}
}
}
return
resultsArray
;
}
...
...
@@ -208,13 +615,54 @@ public class Program {
public
static
int
Mode
(
int
[]
input
)
{
int
output
=
0
;
int
highestCount
=
0
;
int
currentCount
=
0
;
int
highestVal
=
0
;
int
iVal
=
0
;
int
jVal
=
0
;
for
(
int
i
=
0
;
i
<
input
.
length
;
i
++)
{
currentCount
=
0
;
for
(
int
j
=
0
;
j
<
input
.
length
;
j
++)
{
iVal
=
input
[
i
];
jVal
=
input
[
j
];
if
(
i
!=
j
&&
iVal
==
jVal
)
{
currentCount
++;
}
if
(
currentCount
>
highestCount
)
{
highestCount
=
currentCount
;
output
=
input
[
i
];
}
}
}
return
output
;
}
public
static
double
Median
(
int
[]
input
)
{
int
output
=
0
;
Arrays
.
sort
(
input
);
;
int
length
;
double
output
=
0
;
double
modeA
;
double
modeB
;
length
=
input
.
length
;
Arrays
.
sort
(
input
);
if
(
input
.
length
%
2
==
0
)
{
modeA
=
length
/
2
;
modeB
=
modeA
+
1
;
double
divisible
=
input
[(
int
)
modeB
-
1
]
-
input
[(
int
)
modeA
-
1
];
divisible
/=
2
;
output
=
input
[(
int
)
modeA
-
1
]
+
divisible
;
}
if
(
input
.
length
%
2
!=
0
)
{
modeA
=
input
.
length
/
2
;
output
=
input
[(
int
)
modeA
];
}
return
output
;
}
...
...
@@ -222,20 +670,63 @@ public class Program {
public
static
double
StandardDeviation
(
int
[]
input
)
{
double
output
=
0
;
double
xBar
=
0
;
double
total
=
0
;
double
i
=
0
;
int
a
=
0
;
double
top
=
0
;
double
divisor
=
0
;
double
out
=
0
;
for
(
i
=
0
;
i
<
input
.
length
;
i
++)
{
a
=
(
int
)
i
;
total
+=
input
[
a
];
}
xBar
=
total
/
input
.
length
;
for
(
int
j
=
0
;
j
<
input
.
length
;
j
++)
{
a
=
(
int
)
j
;
top
=
input
[
a
]
-
xBar
;
divisor
=
input
.
length
-
1
;
out
=
Math
.
pow
(
top
,
2
);
output
+=
out
;
}
output
/=
divisor
;
output
=
Math
.
sqrt
(
output
);
return
output
;
}
public
static
double
HarmonicMean
(
int
[]
input
)
{
double
output
=
0
;
double
c
=
0
;
double
a
=
0
;
for
(
int
i
=
0
;
i
<
input
.
length
;
i
++)
{
a
=
input
[
i
];
c
=
c
+
(
1
/
a
);
output
=
(
input
.
length
)
/
c
;
}
return
output
;
}
public
static
double
ComplexSumFunctionA
(
int
[]
input
)
{
double
output
=
0
;
double
a
=
0
;
double
b
=
0
;
double
c
=
0
;
double
d
=
0
;
for
(
int
i
=
0
;
i
<
input
.
length
;
i
++)
{
a
=
input
[
i
]
/
2
;
b
=
input
.
length
+
3
;
d
=
i
;
c
=
d
/
2
;
output
=
output
+
(
Math
.
pow
(
a
,
c
))
/
b
;
;
}
return
output
;
}
...
...
@@ -244,26 +735,72 @@ public class Program {
int
[][]
resultMatrix
=
new
int
[
matrix
.
length
][
matrix
[
0
].
length
];
int
mat
=
0
;
int
matTwo
=
0
;
for
(
int
i
=
0
;
i
<
matrix
[
0
].
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
matrix
[
1
].
length
;
j
++)
{
resultMatrix
[
i
][
j
]
=
matrix
[
i
][
j
]
*
11
;
}
}
return
resultMatrix
;
}
public
static
int
[][]
ComplexMatrixOperation
(
int
[][]
matrix
,
int
scalar
)
{
public
static
int
[][]
ComplexMatrixOperation
(
int
[][]
matrixA
,
int
[][]
matrixB
,
int
scalar
)
{
int
[][]
resultMatrix
=
new
int
[
matrix
.
length
][
matrix
[
0
].
length
];
int
[][]
resultMatrix
=
new
int
[
matrixA
[
0
].
length
][
matrixA
[
0
].
length
];
int
mat
=
0
;
int
matTwo
=
0
;
for
(
int
i
=
0
;
i
<
matrixA
[
0
].
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
matrixA
[
1
].
length
;
j
++)
{
resultMatrix
[
i
][
j
]
=
matrixA
[
i
][
j
]
+
matrixB
[
i
][
j
];
}
}
for
(
int
i
=
0
;
i
<
resultMatrix
[
0
].
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
resultMatrix
[
1
].
length
;
j
++)
{
resultMatrix
[
i
][
j
]
=
resultMatrix
[
i
][
j
]
*
11
;
}
}
return
resultMatrix
;
}
public
static
double
[]
GenerateRandomDistributionForSpecificDie
(
int
iterations
)
{
double
[]
resultsArray
=
new
double
[
5121
];
int
n
=
0
;
int
z
=
0
;
Random
rng
=
new
Random
(
1234
);
for
(
int
i
=
0
;
i
<
iterations
;
i
++)
{
int
total
=
0
;
for
(
int
j
=
0
;
j
<
20
;
j
++)
{
n
=
rng
.
nextInt
(
256
)
+
1
;
total
+=
n
;
}
resultsArray
[
total
]++;
}
for
(
int
i
=
0
;
i
<
5121
;
i
++)
{
if
(
resultsArray
[
i
]
==
0
)
{
continue
;
}
resultsArray
[
i
]
=
resultsArray
[
i
]/
iterations
;
double
a
=
(
resultsArray
[
i
]/
iterations
);
}
return
resultsArray
;
}
...
...
@@ -271,12 +808,34 @@ public class Program {
double
[]
resultsArray
=
new
double
[(
dieSides
*
rolls
)
+
1
];
int
n
=
0
;
int
z
=
0
;
Random
rng
=
new
Random
(
1234
);
for
(
int
i
=
0
;
i
<
iterations
;
i
++)
{
int
total
=
0
;
for
(
int
j
=
0
;
j
<
rolls
;
j
++)
{
n
=
rng
.
nextInt
(
dieSides
)
+
1
;
total
+=
n
;
}
resultsArray
[
total
]++;
}
for
(
int
i
=
0
;
i
<
dieSides
*
rolls
;
i
++)
{
if
(
resultsArray
[
i
]
==
0
)
{
continue
;
}
resultsArray
[
i
]
=
resultsArray
[
i
]/
iterations
;
double
a
=
(
resultsArray
[
i
]/
iterations
);
}
return
resultsArray
;
}
...
...
@@ -284,8 +843,23 @@ public class Program {
double
[]
distribution
=
GenerateRandomDistribution
(
dieSides
,
rolls
,
100000
);
double
[]
resultsArray
=
new
double
[(
dieSides
*
rolls
)
+
1
];
double
iterations
=
100000
;
double
n
=
0
;
double
y
=
0
;
System
.
out
.
println
(
Arrays
.
toString
(
distribution
));
for
(
y
=
sum
;
y
<
dieSides
*
rolls
;
y
++)
{
double
e
=
distribution
[(
int
)
y
];
n
+=
distribution
[(
int
)
y
];
}
double
result
=
0
;
result
=
n
;
return
result
;
}
...
...
Maths assessment/src/ProgramTest.java
View file @
a063e2e9
...
...
@@ -7,275 +7,296 @@ class ProgramTest {
@Test
void
testConvertBinaryToInt
()
{
String
[]
input
=
{
"010"
,
"110"
,
"111"
,
"1010101"
};
String
[]
input
=
{
"010"
,
"110"
,
"111"
,
"1010101"
};
int
[]
result
=
Program
.
ConvertBinaryToInt
(
input
);
assertEquals
(
result
[
0
],
2
);
assertEquals
(
result
[
1
],
6
);
assertEquals
(
result
[
2
],
7
);
assertEquals
(
result
[
3
],
85
);
assertEquals
(
result
[
0
],
2
);
assertEquals
(
result
[
1
],
6
);
assertEquals
(
result
[
2
],
7
);
assertEquals
(
result
[
3
],
85
);
}
@Test
void
testConvertBase3ToInt
()
{
String
[]
input
=
{
"2"
,
"12"
,
"10"
,
"111"
};
String
[]
input
=
{
"2"
,
"12"
,
"10"
,
"111"
};
int
[]
result
=
Program
.
ConvertBase3ToInt
(
input
);
assertEquals
(
result
[
0
],
2
);
assertEquals
(
result
[
1
],
5
);
assertEquals
(
result
[
2
],
3
);
assertEquals
(
result
[
3
],
13
);
assertEquals
(
result
[
0
],
2
);
assertEquals
(
result
[
1
],
5
);
assertEquals
(
result
[
2
],
3
);
assertEquals
(
result
[
3
],
13
);
}
@Test
void
testConvertHexToInt
()
{
String
[]
input
=
{
"50"
,
"110"
,
"FA"
,
"B0"
,
"F"
};
String
[]
input
=
{
"50"
,
"110"
,
"FA"
,
"B0"
,
"F"
};
int
[]
result
=
Program
.
ConvertHexToInt
(
input
);
assertEquals
(
result
[
0
],
80
);
assertEquals
(
result
[
1
],
272
);
assertEquals
(
result
[
2
],
250
);
assertEquals
(
result
[
3
],
176
);
assertEquals
(
result
[
4
],
15
);
assertEquals
(
result
[
0
],
80
);
assertEquals
(
result
[
1
],
272
);
assertEquals
(
result
[
2
],
250
);
assertEquals
(
result
[
3
],
176
);
assertEquals
(
result
[
4
],
15
);
}
@Test
void
testConvertBase20ToInt
()
{
// 1s, 20s, 400s
//1s, 20s, 400s
String
[]
input
=
{
"50"
,
"110"
,
"FA"
,
"GH"
};
int
[]
result
=
Program
.
ConvertBase20ToInt
(
input
);
String
[]
input
=
{
"50"
,
"110"
,
"FA"
,
"GH"
};
assertEquals
(
result
[
0
],
100
);
assertEquals
(
result
[
1
],
420
);
assertEquals
(
result
[
2
],
310
);
assertEquals
(
result
[
3
],(
16
*
20
)+
17
);
int
[]
result
=
Program
.
ConvertBase20ToInt
(
input
);
assertEquals
(
result
[
0
],
100
);
assertEquals
(
result
[
1
],
420
);
assertEquals
(
result
[
2
],
310
);
assertEquals
(
result
[
3
],
(
16
*
20
)
+
17
);
}
@Test
void
testUnion
()
{
int
[]
setA
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
15
};
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
);
int
[]
setA
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
15
};
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
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
3
,
6
,
8
};
int
[]
interSet
=
Program
.
Intersection
(
setA
,
setB
);
assertEquals
(
setC
.
length
,
interSet
.
length
);
for
(
int
i
:
setC
)
{
Boolean
flag
=
false
;
for
(
int
j
:
interSet
)
{
if
(
i
==
j
)
{
flag
=
true
;
}
}
assertTrue
(
flag
);
int
[]
setA
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
3
,
6
,
8
};
int
[]
interSet
=
Program
.
Intersection
(
setA
,
setB
);
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
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
4
,
2
,
1
};
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
);
int
[]
setA
=
{
3
,
6
,
8
,
4
,
2
,
1
};
int
[]
setB
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
setC
=
{
4
,
2
,
1
};
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
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Sum
(
set
),
3
+
6
+
7
+
9
+
8
+
7
+
6
+
5
+
15
);
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Sum
(
set
),
3
+
6
+
7
+
9
+
8
+
7
+
6
+
5
+
15
);
}
@Test
void
testMean
()
{
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Mean
(
set
),((
3
d
+
6
d
+
7
d
+
9
d
+
8
d
+
7
d
+
6
d
+
5
d
+
15
d
)/
9
d
),
0.01
);
assertEquals
(
Program
.
Mean
(
set
),
((
3
d
+
6
d
+
7
d
+
9
d
+
8
d
+
7
d
+
6
d
+
5
d
+
15
d
)
/
9
d
),
0.01
);
}
@Test
void
testMode
()
{
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
7
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Mode
(
set
),
7
);
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
7
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Mode
(
set
),
7
);
}
@Test
void
testMedian
()
{
int
[]
set
=
{
3
,
6
,
7
,
9
,
8
,
6
,
7
,
6
,
5
,
15
};
assertEquals
(
Program
.
Median
(
set
),
6.5d
,
0.001
);
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
);
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
=
{
15
,
3
,
7
,
9
,
8
,
7
,
7
,
6
,
5
,
2
};
assertEquals
(
Program
.
Range
(
set
),
13
);
int
[]
set
=
{
15
,
3
,
7
,
9
,
8
,
7
,
7
,
6
,
5
,
2
};
assertEquals
(
Program
.
Range
(
set
),
13
);
}
@Test
void
testHarmonicMean
()
{
int
[]
set
=
{
3
,
6
,
77
};
assertEquals
(
Program
.
HarmonicMean
(
set
),
(
3
d
/
((
1
d
/
3
d
)
+
(
1
d
/
6
d
)
+
(
1
d
/
77
d
))),
0.1
);
int
[]
set
=
{
3
,
6
,
77
};
assertEquals
(
Program
.
HarmonicMean
(
set
),(
3
d
/((
1
d
/
3
d
)+(
1
d
/
6
d
)+(
1
d
/
77
d
))),
0.1
);
}
@Test
void
testComplexSumFunctionA
()
{
int
[]
set
=
{
3
,
6
,
77
};
int
[]
set
=
{
3
,
6
,
77
};
assertEquals
(
Program
.
ComplexSumFunctionA
(
set
),
(
1
d
+
Math
.
pow
(
6
d
/
2
d
,
0.5d
)+
(
77
d
/
2
d
))
/
6
,
0.1
);
}
@Test
void
testMatrixScalar
()
{
int
[][]
matrix
=
{{
1
,
2
},
{
3
,
4
}};
(
1
d
+
Math
.
pow
(
6
d
/
2
d
,
0.5d
)
+
(
77
d
/
2
d
))
/
6
,
0.1
);
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
testMatrixScalar
()
{
int
[][]
matrix
=
{
{
1
,
2
},
{
3
,
4
}
};
void
testComplexMatrixOperation
()
{
int
[][]
mScaled
=
Program
.
MatrixScalarMultiplication
(
matrix
,
11
);
int
[][]
matrix
=
{{
1
,
2
},
{
3
,
4
}};
assertEquals
(
mScaled
[
0
][
0
],
11
);
assertEquals
(
mScaled
[
0
][
1
],
22
);
assertEquals
(
mScaled
[
1
][
0
],
33
);
assertEquals
(
mScaled
[
1
][
1
],
44
);
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 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
);
}
assertEquals
(
d
[
2479
],
0.0012
,
0.0001
);
}
@Test
void
testGenerateRandomDistribution
()
{
assertEquals
(
Program
.
GenerateRandomDistribution
(
50
,
5
,
100000
)[
89
],
0.0062
,
0.0001
);
assertEquals
(
Program
.
GenerateRandomDistribution
(
50
,
5
,
100000
)[
89
],
0.0062
,
0.0001
);
}
@Test
void
testGetProbabilityOfResultOrHigher
()
{
double
d
=
Program
.
GetProbabilityOfResultOrHigher
(
50
,
10
,
300
);
double
d
=
Program
.
GetProbabilityOfResultOrHigher
(
50
,
10
,
300
);
assertEquals
(
d
,
0.16947000000000007
,
0.0001
);
assertEquals
(
d
,
0.16947000000000007
,
0.0001
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment