Question: Code: package homework; import algs13.Queue; import stdlib.StdOut; /** * Complete the 5 methods marked ToDo * You must not change the declaration of any method.

Code:

package homework;

import algs13.Queue; import stdlib.StdOut;

/** * Complete the 5 methods marked ToDo * You must not change the declaration of any method. */

/** * The LinkedListST class represents an (unordered) symbol table of * generic key-value pairs. It supports put, get, and delete methods (already implemented) */ public class LinkedListST, Value extends Comparable> { private Node first; // the linked list of key-value pairs

// a helper linked list data type private class Node { private Key key; private Value val; private Node next;

public Node(Key key, Value val, Node next) { this.key = key; this.val = val; this.next = next; } }

/** * Initializes an empty symbol table. */ public LinkedListST() { first = null; }

/** * Returns the value associated with the given key in this symbol table. */ public Value get(Key key) { if (key == null) throw new NullPointerException("argument to get() is null"); for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) return x.val; } return null; }

/** * Inserts the specified key-value pair into the symbol table, overwriting the old * value with the new value if the symbol table already contains the specified key. * Deletes the specified key (and its associated value) from this symbol table * if the specified value is null. */ public void put(Key key, Value val) { if (key == null) throw new NullPointerException("first argument to put() is null"); if (val == null) { delete(key); return; }

for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); }

/** * Removes the specified key and its associated value from this symbol table * (if the key is in this symbol table). */ public void delete(Key key) { if (key == null) throw new NullPointerException("argument to delete() is null"); first = delete(first, key); }

// delete key in linked list beginning at Node x // warning: function call stack too large if table is large private Node delete(Node x, Key key) { if (x == null) return null; if (key.equals(x.key)) { return x.next; } x.next = delete(x.next, key); return x; }

/** * size returns the number of key-value pairs in the symbol table. * it returns 0 if the symbol table is empty. */ public int size () { int N = 0; for(Node temp = first; temp != null; temp = temp.next) { N++; } return N; } /** * secondMaxKey returns the second maximum key in the symbol table. * it returns null if the symbol table is empty or if it has only one key. * See if you can write it with only one loop */ public Key secondMaxKey () { if(size() == 0 || size() == 1) return null; Key firstMax = first.key; Key secondMax = first.next.key; for (Node temp = first; temp != null; temp = temp.next) { if (firstMax.compareTo(temp.key) < 0) { secondMax = firstMax; firstMax = temp.key; } }

if (secondMax != null) { return secondMax; } return null; }

Tests:

package homework;

import stdlib.StdIn; import stdlib.StdOut;

public class hw1Driver {

public static void main(String[] args) {

// the simple testing code from the textbook pg 370 // you may delete/comment this out if you wish LinkedListST st = new LinkedListST<>(); StdIn.fromFile("data/tinyST.txt"); for (int i = 0; !StdIn.isEmpty(); i++) { String key = StdIn.readString(); st.put(key, i); } for (String s : st.keys()) StdOut.println(s + " " + st.get(s)); allSecondMaxKeyTests(); } public static void allSecondMaxKeyTests() { //secondMaxKeyTest("", null); // trouble getting a true value comparing null to null //secondMaxKeyTest("a", null); // same trouble secondMaxKeyTest("abcde", "d"); // test secondMaxKey on a non-empty ST secondMaxKeyTest("abcdefghij", "i"); // test secondMaxKey on a non-empty ST } public static void allRankTests() { rankTest("abcdef", "a", 0); // test rank on ST given first (highest rank) key rankTest("abcdef", "f", 5); // test rank on ST given last (lowest rank) rankTest("abcdef", "c", 2); // test rank on ST given a key in the middle of string } // sample testing function. // param vals: all substrings of length 1 are added to the ST // param answer: the correct value of the ST for the input:vals public static void sizeTest( String vals, int answer ) { // create and populate the table from the input string vals LinkedListST aList = new LinkedListST(); for (int i=0; i < vals.length(); i++) { aList.put(vals.substring(i, i+1),i); } // call the size function int result = aList.size(); //report result if ( result == answer) // test passes StdOut.format("sizeTest: Correct String %s Answer: %d ", vals,result); else StdOut.format("sizeTest: *Error* String %s Answer: %d ", vals,result); } //Todo: add your testing modules and functions here //See note about testing inverse function public static void secondMaxKeyTest(String vals, String answer) { // create and populate the table from the input string vals LinkedListST aList = new LinkedListST(); for (int i=0; i < vals.length(); i++) { aList.put(vals.substring(i, i+1),i); } // call the secondMax function String result = aList.secondMaxKey(); if(result.equals(answer)) { // test passes StdOut.println("secondMaxKeyTest: Correct " + "String: " + vals + " Answer: " + answer); } else { StdOut.println("secondMaxKeyTest: *Error* " + "String: " + vals + " Answer: " + answer); } } public static void rankTest(String vals, String key, int answer) { // create and populate the table from the input string vals LinkedListST aList = new LinkedListST(); for (int i=0; i < vals.length(); i++) { aList.put(vals.substring(i, i+1),i); } // call the rank function int result = aList.rank(key); //report result if(result == answer) { // test passes StdOut.println("secondMaxKeyTest: Correct " + "String: " + vals + " Key: " + key + " Answer: " + answer); } else { StdOut.println("secondMaxKeyTest: *Error* " + "String: " + vals + " Key: " + key + " Answer: " + answer); } } }

When the test function receives two null values, it should return true because null == null. Can anyone help me figure out why I'm getting NullPointerExceptions?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!