Page 1 of 1

DO NOT COPY AND PASTE QUESTION AS ANSWER OTHERWISE Will be reported. THANK-YOU JAVA CODE help. TESTS are also provided.

Posted: Thu Jun 02, 2022 8:40 am
by answerhappygod
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 other
* @return the id of the first common block
between the two Chains. If there is
* no common block, return null. If
the ranks differ, return the one
* with the higher rank.
*/
public Block firstCommonBlock(Chain other)
{
return null;
}
/**
*
* @param id - ID to be removed
* @return the removed Block. If there doesn't
exist a Block with the given ID,
* return null. If a Block is
removed, the rank of all the Blocks after
* it needs to be updated to ensure
there isn't a gap in ranking.
*/
public Block removeId(int id)
{
return null;
}
/**
*
* @return a Chain where all the Blocks in the
current chain is sorted in
* ascending order. Do not modify the
calling object.
*/
public Chain Sort() {
return null;
}
/**
*
* @param min
* @param max
* @return the seed (id) in the interval [min,
max] that gives the longest
* chain. Only positive seed should
be considered. If the interval is
* invalid, return null.
*/
public Chain longestChain(int
min, int max) {
return null;
}
}
@Test
@Order(9)
@Graded(description = "Chain - testSort", marks = 5)
public void testSort() {
testCreateChain();
currentMethodName = null;
Chain c = new Chain();
Chain ans = c.Sort();
assertNotNull(ans);
assertNull(ans.head);
String copy = ten.toString();
ans = ten.Sort();
assertNotNull(ans);
assertEquals("(1, 7) -> (2, 6) -> (4, 5) ->
(5, 2) -> (8, 4) -> (10, 1) -> (16, 3)",
ans.toString());
assertEquals(copy, ten.toString());
copy = six.toString();
ans = six.Sort();
assertNotNull(ans);
assertEquals("(1, 9) -> (2, 8) -> (3, 2) ->
(4, 7) -> (5, 4) -> (6, 1) -> (8, 6) -> (10, 3) ->
(16, 5)",
ans.toString());
assertEquals(copy, six.toString());
copy = seven.toString();
ans = seven.Sort();
assertNotNull(ans);
assertEquals(
"(1, 17) -> (2, 16) -> (4, 15) -> (5, 12) -> (7, 1)
-> (8, 14) -> (10, 11) -> (11, 3) -> (13, 8) -> (16,
13) -> (17, 5) -> (20, 10) -> (22, 2) -> (26, 7) ->
(34, 4) -> (40, 9) -> (52, 6)",
ans.toString());
assertEquals(copy, seven.toString());
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(10)
@Graded(description = "Chain - testRemoveId", marks = 5)
public void testRemoveId()
{
testCreateChain();
currentMethodName = null;
assertNull(six.removeId(7));
// (6, 1) -> (3, 2) -> (10, 3) -> (5, 4) -> (16, 5)
-> (8, 6) -> (4, 7) -> (2,
// 8) -> (1, 9)
assertNotNull(six.head);
Block toBeRemoved = six.head.next.next.next;
Block removed = six.removeId(5);
assertNotNull(removed);
assertEquals(5, removed.id);
assertEquals(4, removed.rank);
assertNull(removed.next);
assertTrue(toBeRemoved == removed, "They are
refferencing the same Block");
assertEquals(8, six.size());
assertEquals("(6, 1) -> (3, 2) -> (10, 3) ->
(16, 4) -> (8, 5) -> (4, 6) -> (2, 7) -> (1, 8)",
six.toString());
toBeRemoved = six.head.next;
removed = six.removeId(3);
assertNotNull(removed);
assertEquals(3, removed.id);
assertEquals(2, removed.rank);
assertNull(removed.next);
assertTrue(toBeRemoved == removed);
assertEquals(7, six.size());
assertEquals("(6, 1) -> (10, 2) -> (16, 3) ->
(8, 4) -> (4, 5) -> (2, 6) -> (1, 7)",
six.toString());
toBeRemoved = six.head;
removed = six.removeId(6);
assertNotNull(removed);
assertEquals(6, removed.id);
assertEquals(1, removed.rank);
assertNull(removed.next);
assertTrue(toBeRemoved == removed);
assertEquals(10, six.head.id);
assertEquals(6, six.size());
assertEquals("(10, 1) -> (16, 2) -> (8, 3) ->
(4, 4) -> (2, 5) -> (1, 6)", six.toString());
toBeRemoved = six.head.next.next.next.next.next;
removed = six.removeId(1);
assertNotNull(removed);
assertEquals(1, removed.id);
assertEquals(6, removed.rank);
assertNull(removed.next);
assertTrue(toBeRemoved == removed);
assertEquals(5, six.size());
assertEquals("(10, 1) -> (16, 2) -> (8, 3) ->
(4, 4) -> (2, 5)", six.toString());
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(11)
@Graded(description = "Chain - testFirstCommonBlock", marks =
5)
public void
testFirstCommonBlock() {
testCreateChain();
currentMethodName = null;
assertNull(ten.firstCommonBlock(null));
assertNotNull(seven.head);
assertEquals("(10, 11) -> (5, 12) -> (16, 13)
-> (8, 14) -> (4, 15) -> (2, 16) -> (1, 17)",
seven.firstCommonBlock(ten).toString());
assertEquals("(10, 11) -> (5, 12) -> (16, 13)
-> (8, 14) -> (4, 15) -> (2, 16) -> (1, 17)",
ten.firstCommonBlock(seven).toString());
assertEquals("(10, 106) -> (5, 107) -> (16, 108)
-> (8, 109) -> (4, 110) -> (2, 111) -> (1, 112)",
six.firstCommonBlock(twentySeven).toString());
assertEquals("(10, 106) -> (5, 107) -> (16, 108)
-> (8, 109) -> (4, 110) -> (2, 111) -> (1, 112)",
twentySeven.firstCommonBlock(six).toString());
Chain a = new Chain();
Chain b = new Chain();
a.createChain(26);
b.createChain(15);
assertEquals(
"(40, 10) -> (20, 11) -> (10, 12) -> (5, 13) -> (16,
14) -> (8, 15) -> (4, 16) -> (2, 17) -> (1, 18)",
a.firstCommonBlock(b).toString());
assertEquals(
"(40, 10) -> (20, 11) -> (10, 12) -> (5, 13) -> (16,
14) -> (8, 15) -> (4, 16) -> (2, 17) -> (1, 18)",
b.firstCommonBlock(a).toString());
a.createChain(341);
b.createChain(80);
assertEquals("(16, 8) -> (8, 9) -> (4, 10) ->
(2, 11) -> (1, 12)", a.firstCommonBlock(b).toString());
assertEquals("(16, 8) -> (8, 9) -> (4, 10) ->
(2, 11) -> (1, 12)", b.firstCommonBlock(a).toString());
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}
@Test
@Order(12)
@Graded(description = "Chain - testLongestChain", marks = 5)
public void testLongestChain()
{
testCreateChain();
currentMethodName = null;
Chain result = new Chain();
assertNull(ten.longestChain(-5, -2));
assertNull(ten.longestChain(-10, 0));
assertNull(ten.longestChain(10, 3));
Chain ans = ten.longestChain(1, 3);
assertNotNull(ans);
assertEquals(result.createChain(3).toString(),
ans.toString());
assertEquals(result.createChain(10).toString(),
ten.toString());
ans = ten.longestChain(3, 3);
assertNotNull(ans);
assertEquals(result.createChain(3).toString(),
ans.toString());
assertEquals(result.createChain(10).toString(),
ten.toString());
ans = ten.longestChain(2, 10);
assertNotNull(ans);
assertEquals(20, ans.size());
assertEquals(result.createChain(9).toString(),
ans.toString());
ans = ten.longestChain(-3, 10);
assertNotNull(ans, "Since there are a few valid chains
in this range, it should not be null");
assertEquals(20, ans.size());
assertEquals(result.createChain(9).toString(),
ans.toString());
ans = ten.longestChain(5, 30);
assertNotNull(ans);
assertEquals(112, ans.size());
assertEquals(result.createChain(27).toString(),
ans.toString());
ans = seven.longestChain(27, 40);
assertNotNull(ans);
assertEquals(112, ans.size());
assertEquals(result.createChain(27).toString(),
ans.toString());
ans = seven.longestChain(100, 105);
assertNotNull(ans);
assertEquals(88, ans.size());
assertEquals(103, ans.head.id);
ans = seven.longestChain(100, 6900);
assertNotNull(ans);
assertEquals(262, ans.size());
assertEquals(6171, ans.head.id);
currentMethodName = new
Throwable().getStackTrace()[0].getMethodName();
}