DO NOT COPY AND PASTE QUESTION AS ANSWER OTHERWISE Will be
reported. THANK-YOU
JAVA CODE help. TESTS are also provided.
DO NOT USE: Arrays or loops please.
public class Chain {
public Block head;
// Attributes to be added
/**
* Default constructor
*
* You may modify this constructor if you need to (e.g. if you
want to
* initialise extra attributes in the class)
*/
public Chain() {
head = null;
// Attributes to be added
}
// DO NOT MODIFY
public String toString() {
if (head == null) {
return null;
}
return head.toString();
}
/**
*
* @param id - The first number in the chain
* @return a valid Collatz chain starting at id
and ending at 1. The chain
* shouldn't contain any loops. If
the given id is non-positive, return
* null. There are 2 tests for this
method, one to check your logic (P)
* and another to check the
efficiency of the code (HD).
*/
public Block createChain(int
id) {
return null;
}
/**
*
* @return the number of Blocks in the chain.
There are 2 tests for this method,
* one to check your logic (P) and
another to check the efficiency of
* the code (HD).
*/
public int size() {
return 0;
}
/**
*
* @return the Block that contains the highest
id in the Chain. If there aren't
* any Blocks in the chain, return
null.
*/
public Block maxValue() {
return null;
}
/**
*
* @param id - The first number in the chain
* @return a faster Collatz chain which skips
the number following an even number.
* It should follow the 3n+1/2 rule
directly, as 3n+1 is always even.
* The chain shouldn't contain any
loops. If the given id is
* non-positive, return null. Do not
modify the original Chain.
*/
public Chain
createFasterChain(int id) {
return null;
}
@Test
@Order(5)
@Graded(description = "Chain - testCreateChain", marks = 11)
public void testCreateChain()
{
Chain c = new Chain();
assertNull(c.createChain(0));
assertNull(c.createChain(-5));
assertNotNull(ten.head);
assertEquals(10, ten.head.id);
assertEquals("(10, 1) -> (5, 2) -> (16, 3) ->
(8, 4) -> (4, 5) -> (2, 6) -> (1, 7)",
ten.toString());
assertEquals(10, ten.head.id);
assertEquals("(10, 1) -> (5, 2) -> (16, 3) ->
(8, 4) -> (4, 5) -> (2, 6) -> (1, 7)",
ten.toString());
assertEquals("(6, 1) -> (3, 2) -> (10, 3) ->
(5, 4) -> (16, 5) -> (8, 6) -> (4, 7) -> (2, 8) ->
(1, 9)",
six.toString());
assertEquals(
"(7, 1) -> (22, 2) -> (11, 3) -> (34, 4) -> (17, 5)
-> (52, 6) -> (26, 7) -> (13, 8) -> (40, 9) -> (20,
10) -> (10, 11) -> (5, 12) -> (16, 13) -> (8, 14) ->
(4, 15) -> (2, 16) -> (1, 17)",
seven.toString());
c = new Chain();
assertEquals(
"(12, 1) -> (6, 2) -> (3, 3) -> (10, 4) -> (5, 5)
-> (16, 6) -> (8, 7) -> (4, 8) -> (2, 9) -> (1,
10)",
c.createChain(12).toString());
assertEquals(
"(12, 1) -> (6, 2) -> (3, 3) -> (10, 4) -> (5, 5)
-> (16, 6) -> (8, 7) -> (4, 8) -> (2, 9) -> (1,
10)",
c.toString());
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(6)
@Graded(description = "Chain - testSize", marks = 11)
public void testSize() {
testCreateChain();
currentMethodName = null;
Chain c = new Chain();
for (int i = 1; i < 1000;
i++) {
c.createChain(i);
assertNotNull(c.head);
assertEquals(i, c.head.id);
assertEquals(1, c.head.rank);
assertEquals(sizes, c.size());
}
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(7)
@Graded(description = "Chain - testMaxValue", marks = 11)
public void testMaxValue()
{
testCreateChain();
currentMethodName = null;
assertNotNull(ten.maxValue());
assertEquals(16, ten.maxValue().id);
assertEquals(3, ten.maxValue().rank);
assertEquals("(16, 3) -> (8, 4) -> (4, 5) ->
(2, 6) -> (1, 7)", ten.maxValue().toString());
assertEquals(16, six.maxValue().id);
assertEquals(5, six.maxValue().rank);
assertEquals("(16, 5) -> (8, 6) -> (4, 7) ->
(2, 8) -> (1, 9)", six.maxValue().toString());
Chain c = new Chain();
assertNull(c.maxValue());
c.createChain(9);
assertEquals(52, c.maxValue().id);
assertEquals(9, c.maxValue().rank);
assertEquals(9232, twentySeven.maxValue().id);
assertEquals(78, twentySeven.maxValue().rank);
c.createChain(9232);
assertEquals(9232, c.maxValue().id);
assertEquals(1, c.maxValue().rank);
c.createChain(1);
assertEquals(1, c.maxValue().id);
assertEquals(1, c.maxValue().rank);
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(8)
@Graded(description = "Chain - testCreateFasterChain", marks =
5)
public void
testCreateFasterChain() {
testSize();
currentMethodName = null;
Chain c = new Chain();
Chain ans = c.createFasterChain(1);
assertNotNull(ans);
assertEquals("(1, 1)", ans.toString());
ans = c.createFasterChain(-1);
assertNull(ans);
ans = c.createFasterChain(-7);
assertNull(ans);
ans = c.createFasterChain(0);
assertNull(ans);
String copy = ten.toString();
ans = ten.createFasterChain(10);
assertNotNull(ans);
assertEquals(6, ans.size());
assertEquals("(10, 1) -> (5, 2) -> (8, 3) ->
(4, 4) -> (2, 5) -> (1, 6)", ans.toString());
assertEquals(copy, ten.toString());
ans = ten.createFasterChain(11);
assertNotNull(ans);
assertEquals(11, ans.size());
assertEquals(
"(11, 1) -> (17, 2) -> (26, 3) -> (13, 4) -> (20, 5)
-> (10, 6) -> (5, 7) -> (8, 8) -> (4, 9) -> (2, 10)
-> (1, 11)",
ans.toString());
assertEquals(copy, ten.toString());
ans = ten.createFasterChain(6);
assertNotNull(ans);
assertEquals(7, ans.size());
assertEquals("(6, 1) -> (3, 2) -> (5, 3) ->
(8, 4) -> (4, 5) -> (2, 6) -> (1, 7)",
ans.toString());
assertEquals(copy, ten.toString());
ans = ten.createFasterChain(512);
assertNotNull(ans);
assertEquals(10, ans.size());
assertEquals(
"(512, 1) -> (256, 2) -> (128, 3) -> (64, 4) -> (32,
5) -> (16, 6) -> (8, 7) -> (4, 8) -> (2, 9) -> (1,
10)",
ans.toString());
assertEquals(copy, ten.toString());
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
DO NOT COPY AND PASTE QUESTION AS ANSWER OTHERWISE Will be reported. THANK-YOU JAVA CODE help. TESTS are also provided.
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am