Page 1 of 1

(Java) Complete the ListUtilities class so that it passes all the tests for GETALLPREFIXES. Need to complete the GETALLP

Posted: Sat Feb 19, 2022 3:22 pm
by answerhappygod
(Java) Complete the ListUtilities class so that it passes all
the tests for GETALLPREFIXES. Need to complete the GETALLPREFIXES
method in listUtilities class**.
**DO NOT PROVIDE ANYTHING ELSE OTHER THAN THE
GETALLPREFIXES method in listUtilities class**
listUtilities class:
package model;
import tests.Node;
public class ListUtilities {
public Node getAllPrefixes(Node input, int r, int
k) {
/*** implement your answer here ****/
}
}
Tester Class:
package tests;
import model.ListUtilities;
import static org.junit.Assert.*;
import org.junit.Test;
/*
* Study carefully the test methods below. They suggest:
* - the required class(es) and method(s) to be
implement in the `model` package
* - how the required class(es) and method(s) should be
implemented
* Requirements:
* + Do not create any new class that is not required
by the starter tests.
* + You do not need to declare attributes.
* But if you really want to, all
attributes you declare in the model classes must be private.
* + If necessary, you may define private helper
methods.
*/
public class StarterTests {
/*
* - This assignment focuses on the
manipulation of singly-linked nodes.
* Therefore, you are
forbidden to use primitive arrays (e.g., Integer[], int[],
String[])
* for declaring
attributes or local variables.
* - In addition, any use of a Java
library class or method is also forbidden
* (that is, use
selections and loops to build your solution from scratch
instead):
* - Here are some examples of forbidden
classes/methods:
* - Arrays class
(e.g., Arrays.copyOf)
* - System class
(e.g., System.arrayCopy)
* - ArrayList
class
* - String class
(e.g., substring).
* - The use of some library classes does
not require an import statement,
* but these classes
are also forbidden to be used.
* - Here are the exceptions (library
methods which you are allowed to use if needed):
* - String class
(equals, format, length)
* Tests included in this class serve as documentation
on how instances of List Utilities operate.
*/

@Test
public void test_getAllPrefixes_01() {
ListUtilities util = new
ListUtilities();
Node input =
new
Node<>(23,
new
Node<>(46,
new
Node<>(19,
new
Node<>(-9, null))));

/*
* Given an input chain of integers,
return a chain of strings.
*
* Each string in the output chain
is a prefix (i.e., first few numbers) of the input chain,
* whose length is
between the specified lower and upper bounds (inclusive at both
ends).
*
* Each prefix should be formatted
as a comma-separated list of integers, surrounded by a pair of
square brackets.
*
* Hints:
* + The length of the
output chain is equal to the length of the input chain.
* + Elements in the
output chain are ``sorted'' by the lengths of the prefixes
*
(e.g,. the first element is the prefix of length 1, the second
element is the prefix of length 2).
*
* Assumptions:
* + For each call of
getAllPrefixes(input, m, n):
* *
The input chain is not null and contains at least one node.
* *
m <= number of nodes in the input chain
* *
n <= number of nodes in the input chain
* *
m <= n
*/

/*
* getAllPrefixes(23 -> 46 ->
19 -> -9, 1, 4) returns a chain of all prefixes whose lengths
are between 1 and 4.
*/
Node output =
util.getAllPrefixes(input, 1, 4);

assertEquals("[23]",
output.getElement());
assertEquals("[23, 46]",
output.getNext().getElement());
assertEquals("[23, 46, 19]",
output.getNext().getNext().getElement());
assertEquals("[23, 46, 19, -9]",
output.getNext().getNext().getNext().getElement());

assertNull(output.getNext().getNext().getNext().getNext());
}

@Test
public void test_getAllPrefixes_02() {
ListUtilities util = new
ListUtilities();
Node input =
new
Node<>(23,
new
Node<>(46,
new
Node<>(19,
new
Node<>(-9, null))));

/*
* getAllPrefixes(23 -> 46 ->
19 -> -9, 2, 3) returns a chain of all prefixes whose lengths
are between 2 and 3.
*/
Node output =
util.getAllPrefixes(input, 2, 3);

assertEquals("[23, 46]",
output.getElement());
assertEquals("[23, 46, 19]",
output.getNext().getElement());

assertNull(output.getNext().getNext());
}

@Test
public void test_getAllPrefixes_03() {
ListUtilities util = new
ListUtilities();
Node input =
new
Node<>(23,
new
Node<>(46,
new
Node<>(19,
new
Node<>(-9, null))));

/*
* getAllPrefixes(23 -> 46 ->
19 -> -9, 3, 3) returns a chain of all prefixes whose lengths
are between 3 and 3.
*/
Node output =
util.getAllPrefixes(input, 3, 3);

assertEquals("[23, 46, 19]",
output.getElement());
assertNull(output.getNext());
}
Node Class:
package tests;
/*
* This Node class is provided to you,
* and you must use it to implement the required
class(es) and method(s) in the model package.
* The StarterTests class in the `tests` package suggests what you
need to create in the `model` package.
* Where the Node class is needed, you must:
* + Only use the public methods given here.
* + Not add any additional attributes or methods in this Node
class.
*/
public class Node {

private E element;
private Node next;

/*
* Constructor
*/
public Node(E e, Node n) {
element = e;
next = n;
}

/*
* Accessors
*/
public E getElement() {
return element;
}

public Node getNext() {
return next;
}

/*
* Mutators
*/
public void setElement(E e) {
element = e;
}

public void setNext(Node n) {
next = n;
}
}