Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: NEED HELP FIXING CODE (Static method compress list returns wrong numbers) ERROR: Test testCompress() FAILED org.opentest4j.AssertionFailedError: expected: but was: at app//LinkedListTest.testCompress(LinkedListTest.java:298) TESTED CODE: public

JAVA: NEED HELP FIXING CODE (Static method compress list returns wrong numbers) ERROR: Test testCompress() FAILED org.opentest4j.AssertionFailedError: expected:  but was:  at app//LinkedListTest.testCompress(LinkedListTest.java:298) TESTED CODE: public class LinkedList { public class Node{ String key; int value; Node next; public Node(String key, int value) { this.key = key; this.value = value; } } private Node head; private Node tail; public LinkedList() { head = null; tail = null; } public void addHead(String key, int val) { Node n = new Node(key, val); if(head == null) { head = n; tail = n; } else { n.next = head; head = n; } } public void addTail(String key, int val) { Node n = new Node(key, val); if(tail == null) { head = n; tail = n; } else { tail.next = n; tail = n; } } public String toString() { String ret = ""; Node curr = head; while(curr != null) { if(curr.next != null) ret += curr.key + ":" + curr.value + ", "; else ret += curr.key + ":" + curr.value; curr = curr.next; } return ret; } private Node find(String key) { Node curr = head; while(curr != null) { if(curr.key.equals(key)) return curr; curr = curr.next; } return null; } public static double average(LinkedList list) { Node curr = list.head; double sum = 0; int count = 0; while (curr != null) { sum += curr.value; count++; curr = curr.next; } return sum / count; } public static boolean ordered(LinkedList list) { Node curr = list.head; while (curr != null && curr.next != null) { if (curr.key.compareTo(curr.next.key) > 0) { return false; } curr = curr.next; } return true; } public static LinkedList reversed(LinkedList list) { Node curr = list.head; LinkedList reversedList = new LinkedList(); while (curr != null) { reversedList.addHead(curr.key, curr.value); curr = curr.next; } return reversedList; } public static LinkedList compressList(LinkedList list) { Node curr = list.head; LinkedList compressedList = new LinkedList(); while (curr != null) { if (compressedList.find(curr.key) == null) { compressedList.addTail(curr.key, curr.value); } curr = curr.next; } return compressedList; } }

Instructions

Create the following inside LinkedList.java:

A static method called average (returns a double) that takes a LinkedList as a parameter. This method computes the average (mean) of the value field of all entries in the list. You should traverse the list beginning at the head, to sum up the total value of the list's nodes. You should use iteration (a loop) in this method. Do not change the list or any values in it.

Example (result in red)

// assume list contains these keys and values:

// ie:6, bi:48, bi:73, bi:63

LinkedList.average(list);

47.5

A static method called ordered (returns a boolean) that takes a LinkedList as a parameter. This method traverses the list (starting at the head) and checks whether the list is in order according to the key fields of each node. This means that for each node, the node following it should have a key which is greater than or equal. You will need to use the compareTo method from the String class.

Example (result in red)

// assume list contains these keys and values:

// ie:6, bi:48, bi:73, bi:63

LinkedList.ordered(list);

false

Example (result in red)

// assume list contains these keys and values:

// ao:101, bi:29, bi:14, fk:6

LinkedList.ordered(list);

true

A static method called reversed that takes a LinkedList as a parameter and returns a new LinkedList that is reversed. f; traverse through the input list (starting at the head). In order to reverse the ordering, you need to add the current node's key and value onto the head of the new list (addHead method). Do not change the input list or any values in it.

Example (result in red)

// assume list contains these keys and values:

// ie:6, bi:48, bi:73, bi:63

LinkedList.reversed(list);

bi:63, bi:73, bi:48, ie:6

A static method called compressList that takes a LInkedList as a parameter. This method should:

Create a new list from the old one such that all entries in the new list have unique keys. The nodes of the new list should be in the same order as the first occurrence of the ids in the original list.

set the value field of each entry in the new list to the sum of all the value fields that share that key in the original list.

NOTE: The existing find method will be helpful both in checking whether a key is already in the list you're creating and for modifying the value associated with that key if it's already in the new list you will be returning.

Example (result in red)

// assume list contains these keys and values:

// ie:6, bi:48, bi:73, bi:63

LinkedList.compressList(list);

ie:6, bi:184,

CODE PROVIDED:

import java.util.Random; public class ListApp { static LinkedList generateData(int numKeys, int numItems, int seed) { LinkedList l = new LinkedList(); Random rng; if (seed < 0) { rng = new Random(); } else { rng = new Random(seed); } String letters = "abcdefghijkloaeipr"; String[] keys = new String[ numKeys ]; String uniqueKeys = ""; for (int i = 0; i < numKeys; i++) { int letter1 = rng.nextInt(letters.length()); int letter2 = rng.nextInt(letters.length()); keys[ i ] = letters.substring(letter1, letter1 + 1) + letters.substring(letter2, letter2 + 1); uniqueKeys += keys[ i ] + " "; } System.err.println(keys.length + " unique keys: " + uniqueKeys); for (int i = 0; i < numItems; i++) { String key = keys[ rng.nextInt(keys.length)]; int val = rng.nextInt(100); l.addHead(key, val); } return l; } public static void main(String[] args) { LinkedList list; list = ListApp.generateData( 5, 9, 1 ); System.out.println("List: " + list); } }

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_2

Step: 3

blur-text-image_3

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions