Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class LinkedListTester { public static void mai

ID: 3563479 • Letter: I

Question

import java.util.Scanner;

public class LinkedListTester
{
   public static void main(String[] args)
   {
Scanner input = new Scanner(System.in);
LinkedListArray myList = null;

Memory env = new Memory();
// env.toggleDebug();

System.err.printf("Enter number of tests: ");
int numTests = input.nextInt();

System.err.printf("Begin entering %d tests ", numTests);
for(int i=0;i<numTests;i++)
{
   String theTest = input.next().toLowerCase();
   System.err.printf("Received %d: <%s> ", i, theTest);
   if(theTest.equals("initialize"))
   {
myList = new LinkedListArray(env);
myList.toggleDebug();
   }
   else if(theTest.equals("initializesize"))
   {
myList = new LinkedListArray(input.nextInt(), env);
   }
   else if(theTest.equals("copy"))
   {
LinkedListArray copyList = new LinkedListArray(myList);
for(int j=0;j<myList.getSize();j++)
{
   if(myList.dataAt(j).equals(copyList.dataAt(j)) == false)
   {
System.out.printf("Mismatch in copied list ");
   }
}
System.out.printf("%s ",copyList.rawOutput());
copyList = null;
   }
   else if(theTest.equals("insert"))
   {
if(myList.insert(input.next()) == false)
{
   System.out.printf("No room! ");
}
   }
   else if(theTest.equals("insertfront"))
   {
if(myList.insertFront(input.next()) == false)
{
   System.out.printf("No room ");
}
   }
   else if(theTest.equals("remove"))
   {
if(myList.remove(input.next()) == false)
{
   System.out.printf("Not found ");
}
   }
   else if(theTest.equals("find"))
   {
String toFind = input.next();
int offset = input.nextInt();
if(myList.find(toFind, offset) == -1)
{
   System.out.printf("Not found ");
}
   }
   else if(theTest.equals("output"))
   {
System.out.printf("%s ", myList);
   }
   else if(theTest.equals("size"))
   {
System.out.printf("Size: %d ", myList.getSize());
   }
   else if(theTest.equals("capacity"))
   {
System.out.printf("Capacity: %d ", myList.getCapacity());
   }
   else if(theTest.equals("dataat"))
   {
int index = input.nextInt();
System.out.printf("At %d: %s ", index, myList.dataAt(index));
   }
   else if(theTest.equals("rawoutput"))
   {
System.out.printf("%s ", myList.rawOutput());
   }
   else
   {
System.out.printf("Invalid test scenario. ");
   }
   System.err.printf("Finished test %d. ", i);
}

   }
}

Explanation / Answer

// Package visible for use in nested classes. 136: Entry getEntry(int n) 137: { 138: Entry e; 139: if (n 0) 144: e = e.next; 145: } 146: else 147: { 148: e = last; 149: // n greater than size/2, iterate from end 150: while (++n size) 198: throw new IndexOutOfBoundsException("Index: " + index + ", Size:" 199: + size); 200: } 201: 202: /** 203: * Checks that the index is in the range of existing elements (exclusive). 204: * 205: * @param index the index to check 206: * @throws IndexOutOfBoundsException if index < 0 || index >= size 207: */ 208: private void checkBoundsExclusive(int index) 209: { 210: if (index < 0 || index >= size) 211: throw new IndexOutOfBoundsException("Index: " + index + ", Size:" 212: + size); 213: } 214: 215: /** 216: * Create an empty linked list. 217: */ 218: public LinkedList() 219: { 220: } 221: 222: /** 223: * Create a linked list containing the elements, in order, of a given 224: * collection. 225: * 226: * @param c the collection to populate this list from 227: * @throws NullPointerException if c is null 228: */ 229: public LinkedList(Collection