Use the java class ArrayList to implement your List class. I.e., your only insta
ID: 3861962 • Letter: U
Question
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("");
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
//main
public class AssignmentFive
{
public static void main(String[] args)
{
List myList = new List(100);
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 String[] 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];
}
/**
* Creates an instance with the given list
*/
public List(List inputList) {
mList = inputList.mList;
mCount = inputList.mCount;
}
/**
* 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 will also increment mCount
if (!shiftRight(0)) {
sop("List Full");
return;
}
mList[0] = item;
}
}
/**
* 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 method will also increment mCount
if (!shiftRight(ii)) {
sop("List Full");
return;
}
mList[ii] = item;
}
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;
}
// Shift left will decrement mCount
shiftLeft(0);
}
/**
* 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)
{
// Shift left will decrement mCount
shiftLeft(ii);
}
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<String> tempList = new ArrayList<String>();
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("");
}
/**
* Returns the index of the item in the list
* returns -1 if not found
*
* @param item to be searched
*/
public int find(String item) {
for (int i=0; i<mCount; i++) {
if (mList[i].equals(item)) {
return i;
}
}
return -1;
}
/**
* Shifts the entire list one step to the left
* at the given index.
*
* @param k
*/
public void shiftLeft(int k) {
for (int i=k; i<mCount-2; i++) {
mList[i] = mList[i+1];
}
mCount = mCount -1;
}
/**
* Shifts the entire list one step to the right
* at the given index.
*
* @param k
*/
public boolean shiftRight(int k) {
if (mCount+1 > mList.length) {
return false;
}
for (int i=k; i<mCount; i++) {
mList[i+1] = mList[i];
}
mCount = mCount+1;
return true;
}
private static void sop(String s)
{
System.out.println(s);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.