Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Java Linked Lists) create method getGroupedString in class ListUtilities. refer to test cases and Node class for more info. pay attenion to noAliasing method in

(Java Linked Lists) create method getGroupedString in class ListUtilities. refer to test cases and Node class for more info. pay attenion to noAliasing method in test cases:

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; } }

@Test public void test_getGroupedStrings_01() { ListUtilities util = new ListUtilities(); Node input = new Node<>("vwxyzj", new Node<>("xy", new Node<>("ghic", new Node<>("pqrstu", new Node<>("def", new Node<>("bc", new Node<>("a", new Node<>("", null)))))))); /* * Calling getGroupedStrings(input, m, n) returns a chain of nodes * which groups all elements from the input chain as follows, from left to right: * Group 1: strings whose lengths are less than m * Group 2: strings whose lengths are greater than or equal to m and less than n * Group 3: strings whose lengths are greater than or equal to n * * Requirements: * - The input and output chains are equally long. * - Each group in the output chain preserves the order in which its elements appear in the input chain. * * Assumptions: * - Assume that m <= n. * - When m = n, it means that group2 is empty. * (say m = n = 3: there is no string whose length is >= 3 and < 3). */ Node output = util.getGroupedStrings(input, 2, 4); /* * Group 1: strings from the input chain whose lengths are * less than 2 (i.e., 0, 1) */ assertEquals("a" , output.getElement()); assertEquals("" , output.getNext().getElement()); /* * Group 2: strings from the input chain whose lengths are * greater than or equal to 2 and less than 4 (i.e., 2, 3) */ assertEquals("xy" , output.getNext().getNext().getElement()); assertEquals("def" , output.getNext().getNext().getNext().getElement()); assertEquals("bc" , output.getNext().getNext().getNext().getNext().getElement()); /* * Group 3: strings from the input chain whose lengths are * greater than or equal to 4 (i.e., 4, 5, ...) */ assertEquals("vwxyzj" , output.getNext().getNext().getNext().getNext().getNext().getElement()); assertEquals("ghic" , output.getNext().getNext().getNext().getNext().getNext().getNext().getElement()); assertEquals("pqrstu" , output.getNext().getNext().getNext().getNext().getNext().getNext().getNext().getElement()); assertNull(output.getNext().getNext().getNext().getNext().getNext().getNext().getNext().getNext()); /* * The input and output chains do not share any node references in common. * i.e., there is no reference aliasing. */ assertTrue(noAliasing(input, output)); }

private boolean noAliasing(Node n1, Node n2) { Node current1 = n1; Node current2 = n2; boolean found = false; while(current1 != null && !found) { while(current2 != null && !found) { found = current1 == current2; current2 = current2.getNext(); } current1 = current1.getNext(); current2 = n2; } return !found; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

How does the concept of hegemony relate to culture?

Answered: 1 week ago

Question

Understand the roles of signs, symbols, and artifacts.

Answered: 1 week ago

Question

Know the three main dimensions of the service environment.

Answered: 1 week ago