Question: Implemement the getMode method in Java. The instructions, UML diagram and skeleton program are given below. Please include a sample run of your working code.
Implemement the getMode method in Java. The instructions, UML diagram and skeleton program are given below. Please include a sample run of your working code.


----
SortedArrayListWithMode.java
package Lab10; import java.util.Arrays; /** * A class that implements the ADT sorted list by using a resizable array. * Duplicate entries are allowed. * * @author YOUR NAME * @version 10/30/2018 */ public class SortedArrayListWithMode> implements SortedListInterface { private T[] list; // Array of list entries; ignore list[0] private int numberOfEntries; private boolean initialized = false; private static final int DEFAULT_CAPACITY = 25; private static final int MAX_CAPACITY = 10000; public SortedArrayListWithMode() { this(DEFAULT_CAPACITY); } // end default constructor public SortedArrayListWithMode(int initialCapacity) { // Is initialCapacity too small? if (initialCapacity DEFAULT_CAPACITY) initialCapacity = DEFAULT_CAPACITY; else // Is initialCapacity too big? checkCapacity(initialCapacity); T[] tempList = (T[])new Comparable[initialCapacity + 1]; this.list = tempList; this.numberOfEntries = 0; this.initialized = true; } // end default constructor // Part a: manipulating directly the instance variables of SortedArrayListWithMode, // find the mode. The mode is the most frequent value. public T getMode() { // TODO Project 2a T mode = null; int modeCount = 0; System.out.println("IMPLEMENT Part a: manipulating directly the instance variables of SortedArrayListWithMode," + "find the mode. The mode is the most frequent value."); System.out.println("NOTE - the list is 1 based!"); System.out.println("---> mode is " + mode + "; mode count is " + modeCount ); return mode; } // end getMode public void add(T newEntry) { checkInitialization(); int newEntryPosition = Math.abs(getPosition(newEntry)); int currentIndex = this.numberOfEntries; while (currentIndex >= newEntryPosition) { this.list[currentIndex + 1] = this.list[currentIndex]; currentIndex--; } // end while this.list[currentIndex + 1] = newEntry; this.numberOfEntries++; ensureCapacity(); // Ensure enough room for next add } // end add public boolean remove(T anEntry) { checkInitialization(); boolean found = false; if (!isEmpty()) { int position = getPosition(anEntry); assert position != 0; if (position > 0) { removeGap(position); this.numberOfEntries--; found = true; } // end if } // end if return found; } // end remove public int getPosition(T anEntry) { assert this.numberOfEntries >= 0; int position = 1; while ( (position 0) ) { position++; } // end while if ( (position > this.numberOfEntries) || !anEntry.equals(getEntry(position))) { position = -position; } // end if return position; } // end getPosition public T getEntry(int givenPosition) { checkInitialization(); if ((givenPosition >= 1) && (givenPosition 0; } // end contains public T remove(int givenPosition) { checkInitialization(); if ((givenPosition >= 1) && (givenPosition = capacity) { int newCapacity = 2 * capacity; checkCapacity(newCapacity); this.list = Arrays.copyOf(this.list, newCapacity + 1); } // end if } // end ensureCapacity private void removeGap(int givenPosition) { assert (givenPosition >= 1) && (givenPosition MAX_CAPACITY) throw new IllegalStateException("Attempt to create a sorted list " + "whose capacity is larger than " + MAX_CAPACITY); } // end checkCapacity public void display() { System.out.print(" The data has " + this.numberOfEntries + " element(s): "); for (int i = 1; i out.print(this.list[i] + " "); } System.out.println(); } public static void main(String args[]) { SortedArrayListWithMode data = new SortedArrayListWithMode(); System.out.println("The mode of the empty list should be null, got: " + data.getMode()); data.add(9); data.display(); System.out.println("The mode should be 9, got: " + data.getMode()); data.add(13); data.display(); System.out.println("The mode should be 9, got: " + data.getMode()); data.add(13); data.display(); System.out.println("The mode should be 13, got: " + data.getMode()); data = new SortedArrayListWithMode(); data.add(9); data.add(9); data.add(13); data.display(); System.out.println("The mode should be 9, got: " + data.getMode()); data = new SortedArrayListWithMode(); for (int i = 0; i out.println("The mode should be 0, got: " + data.getMode()); for (int i = 0; i out.println("The mode should be 9, got: " + data.getMode()); for (int i = 0; i out.println("The mode should be 20, got: " + data.getMode()); for (int i = 0; i out.println("The mode should be 6, got: " + data.getMode()); System.out.println(" *** Done ***"); } // end main } // end ArraySortedList
-----
SortedLinkedList.java
package Lab10; public class SortedLinkedList> implements SortedListInterface { private Node firstNode; // Reference to first node of chain private int numberOfEntries; public SortedLinkedList() { this.firstNode = null; this.numberOfEntries = 0; } // end default constructor public void add(T newEntry) { Node newNode = new Node(newEntry); Node nodeBefore = getNodeBefore(newEntry); if (isEmpty() || (nodeBefore == null)) { // Add at beginning newNode.next = this.firstNode; this.firstNode = newNode; } else { // Add after nodeBefore Node nodeAfter = nodeBefore.next; newNode.next = nodeAfter; nodeBefore.next = newNode; } // end if this.numberOfEntries++; } // end add public boolean remove(T anEntry) { boolean found = false; if (this.numberOfEntries > 0) { Node nodeToRemove; Node nodeBefore = getNodeBefore(anEntry); if (nodeBefore == null) { nodeToRemove = this.firstNode; } else { nodeToRemove = nodeBefore.next;} if ((nodeToRemove != null) && anEntry.equals(nodeToRemove.data) ) { found = true; if (nodeBefore == null) { this.firstNode = nodeToRemove.next; } else { Node nodeAfter = nodeToRemove.next; nodeBefore.next = nodeAfter; } // end if this.numberOfEntries--; } // end if } // end if return found; } // end remove public int getPosition(T anEntry) { int position = 1; Node currentNode = this.firstNode; while ( (currentNode != null) && ( anEntry.compareTo(currentNode.data) > 0) ) { currentNode = currentNode.next; position++; } // end while if ( (currentNode == null) || anEntry.compareTo(currentNode.data) != 0) position = -position; return position; } // end getPosition public T getEntry(int givenPosition) { T result = null; // Result to return if ((givenPosition >= 1) && (givenPosition = 1) && (givenPosition 1 Node nodeBefore = getNodeAt(givenPosition - 1); Node nodeToRemove = nodeBefore.next; Node nodeAfter = nodeToRemove.next; nodeBefore.next = nodeAfter; // Disconnect the node to be removed result = nodeToRemove.data; // Save entry to be removed } // end if this.numberOfEntries--; } // end if return result; // Return removed entry, or // null if operation fails } // end remove public final void clear() { this.firstNode = null; this.numberOfEntries = 0; } // end clear public boolean contains(T anEntry){ return getPosition(anEntry) > 0; } // end contains public int getLength() { return this.numberOfEntries; } // end getLength public boolean isEmpty() { boolean result; if (this.numberOfEntries == 0){ // Or getLength() == 0 assert this.firstNode == null; result = true; } else { assert this.firstNode != null; result = false; } // end if return result; } // end isEmpty public T[] toArray() { // The cast is safe because the new array contains null entries @SuppressWarnings("unchecked") T[] result = (T[])new Comparable[this.numberOfEntries]; // Warning: unchecked cast int index = 0; Node currentNode = this.firstNode; while ((index getNodeBefore(T anEntry) { Node currentNode = this.firstNode; Node nodeBefore = null; while ( (currentNode != null) && (anEntry.compareTo(currentNode.data) > 0) ) { nodeBefore = currentNode; currentNode = currentNode.next; } // end while return nodeBefore; } // end getNodeBefore private Node getNodeAt(int givenPosition) { assert !isEmpty() && (1 currentNode = this.firstNode; for (int counter = 1; counter { private S data; // Entry in list private Node next; // Link to next node private Node(S dataPortion) { this.data = dataPortion; this.next = null; } // end constructor private Node(S dataPortion, Node nextNode) { this.data = dataPortion; this.next = nextNode; } // end constructor } // end Node } // end LinkedSortedList
II. The mode of a list of values is the value having the greatest frequency. Findthemode of the sorted list. Your taskis to implement this method in this way: a. Assuming an array-based implementation manipulate the array directly implement method getMode in SortedArrayListwithMode.java. Please note that the list is 1 based
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
