Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the java class ArrayList to implement your List class. I.e., your only instance data is private ArrayList mList; List class methods will delegate their

Use the java class ArrayList to implement your List class. I.e., your only instance data is

private ArrayList mList;

List class methods will delegate their functionality to ArrayList. Need help with the class List.

//main

public class AssignmentFive { public static void main(String[] args) { List myList = new List(); List emptyList = new List(myList); // Cause List Empty Message myList.removeFront(); myList.removeRear(); myList.removeItem("a"); // Cause Not found message myList.addToFront("x"); myList.removeItem("y"); myList.removeItem("x"); myList.addAfterItem("x", "z"); myList.addBeforeItem("x", "z"); // Normal behavior myList.addToFront("not."); myList.addToFront("or"); myList.addToRear("is"); myList.addToRear("try."); myList.addAfterItem("is", "no"); myList.addBeforeItem("is", "There"); myList.addToFront("Do"); myList.addAfterItem("or", "do"); myList.print("Original list"); myList.printSorted("Sorted Original List"); emptyList.print("Empty List"); List copyOfList = new List(myList); sop(" Front is " + myList.getFront()); sop("Rear is " + myList.getRear()); sop("Count is " + myList.askCount()); sop("Is There present? " + myList.isPresent("There")); sop("Is Dog present? " + myList.isPresent("Dog")); myList.addToFront("junk"); myList.addToRear("morejunk"); myList.addAfterItem("or", "moremorejunk"); myList.print("List with junk"); sop("Count is " + myList.askCount()); copyOfList.print("Untouched copy of the list"); myList.removeFront(); myList.removeRear(); myList.removeItem("moremorejunk"); myList.print("List with junk removed"); sop("Count is " + myList.askCount()); sop(""); copyOfList.print("Untouched copy of the list"); while(myList.askCount() > 0) myList.removeFront(); myList.print("List after removing all items"); copyOfList.print("Copy of List after removing all items"); } private static void sop(String s) { System.out.println(s); } }

//Class List:

public class List { private Stirng[] mList; private int mCount; /** * Create a List with the indicated size * @param size the maximum number of items in the List */ public List(int size) { mCount = 0; mList = new String[size]; } /** * If the List is not full, item becomes the new front element * If the List is full, prints "List Full" * @param item the item to add */ public void addToFront(String item) { if(mCount >= mList.length) { sop("List Full"); } else if(mCount == 0) { mList[0] = item ; mCount++; } else { shiftRight(mCount,0); mList[0] = item; ++mCount; } } /** * If the List is not full, item becomes the new rear element * If the List is full, prints "List Full" * @param item the item to add */ public void addToRear(String item) { if(mCount == mList.length) { sop("List Full"); } else { mList[mCount] = item; mCount++; } } /** * If the List is not full and beforeItem is in the List item becomes the element before beforeItem * If the List is full, prints "List Full" * If List is not full but beforeItem is not in List, prints "Item Not Found" * @param beforeItem the item in the list to add item before * @param item the item to add */ public void addBeforeItem(String beforeItem, String item) { if(mCount == mList.length) { sop("List Full"); } else if(isPResent(beforeItem) && mCount < mList.Length)) { if(find(beforeItem) == 0) { } else { sop("Item not found"); } } /** * If the List is not full and afterItem is in the List item becomes the element after afterItem * If the List is full, prints "List Full" * If List is not full but afterItem is not in List, prints "Item Not Found" * @param afterItem the item in the list to add item before * @param item the item to add */ public void addAfterItem(String afterItem, String item) { if(mCount >= mList.length) { sop("List Full"); return; } int ii = find(afterItem); if(ii != -1) { ++ii; shiftRight(ii); mList[ii] = item; ++mCount; } else { sop("Item not found"); } } /** * Returns the item at the front of the List (List is not altered) * @return the item at the front of the List */ public String getFront() { if(mCount == 0) { sop("List Empty"); return ""; } return(mList[0]); } /** * Returns the item at the rear of the List (List is not altered) * @return the item at the rear of the List */ public String getRear() { if(mCount == 0) { sop("List Empty"); return ""; } return(mList[mCount - 1]); } /** * Return true if item is in List, false otherwise * @param item to check presence in List * @return true if item is in List, false otherwise */ public boolean isPresent(String item) { if(find(item) != -1) return true; else return false; } /** * Returns the number of items in the List * @return the number of items in the List */ public int askCount() { return(mCount); } /** * If the List is empty, prints "List Empty" * If the List is not empty, removes the item at the front of the List */ public void removeFront() { if(mCount == 0) { sop("List Empty"); return; } shiftLeft(0); --mCount; } /** * If the List is empty, prints "List Empty" * If the List is not empty, removes the item at the rear of the List */ public void removeRear() { if(mCount == 0) { sop("List Empty"); return; } --mCount; } /** * If the List is empty, prints "List Empty" * If item is not present in List, prints "Item not found" * Otherwise, item is removed from the List * @param item the item to remove */ public void removeItem(String item) { if(mCount == 0) { sop("List Empty"); return; } int ii = find(item); if(ii != -1) { shiftLeft(ii); --mCount; } else { sop("Item not found"); } } /** * Print title on a line by itself * Prints the List from front to rear with 1 space between each item * @param title the description of the List */ public void print(String title) { System.out.println(" " + title); for(int ii = 0; ii < mCount; ++ii) { System.out.print(mList[ii] + " "); } System.out.println(""); } /** * Print title on a line by itself * Prints the Sorted List with 1 space between each item * Does not alter the List * @param title the description of the List */ public void printSorted(String title) { ArrayList tempList = new ArrayList(); for(int ii = 0; ii < mCount; ++ii) { tempList.add(mList[ii]); } Collections.sort(tempList); System.out.println(" " + title); for(String s : tempList) { System.out.print(s + " "); } System.out.println(""); } }

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

Microsoft Visual Basic 2017 For Windows Web And Database Applications

Authors: Corinne Hoisington

1st Edition

1337102113, 978-1337102117

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago