Hello, I am completely lost on how to implement the ( private static ArrayList d
ID: 3860547 • Letter: H
Question
Hello,
I am completely lost on how to implement the ( private static ArrayList data; // a reference to an ArrayList of String objects ) into the methods. I've tried taking the data and turning it into an array but it becomes a mess... The driver and other codes are pertaining to the sortedstringlist are already done so I can only work with what is designated in the code... Can someone help explain how to use the ' private static ArrayList < String> data; ' ?
public class SortedStringList implements SortedStringListInterface{
private static ArrayList data; // a reference to an ArrayList of String objects
public SortedStringList()
// creates an empty list
{
} // end no-args constructor
public boolean isEmpty()
// Determines whether a list is empty
{
return true;
} // end isEmpty
public int size()
// Returns the number of items that are in a list
{
return 0;
} // end size
public void removeAll()
// Removes all the items in the list
{
} // end removeAll
public void add(String item) throws ListException
// Inserts item into its proper position in a sorted list
// Throws an exception if the item connot be placed on the list
{
try
{
}
catch(Exception e)
{
throw new ListException("Add to List failed: " + e.toString());
}
}
public String get(int index) throws ListIndexOutOfBoundsException
// Retrieves the item at position index of a sorted list, if 1 <= index <= size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
{
if (index > size())
{
throw new ListIndexOutOfBoundsException(index + " is an invalid index");
}
return null;
}
public void remove(String item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
try
{
}
catch(Exception e)
{
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndex(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
return 0;
}
} // end SortedStringList
******************* sorted string interface ***********************************
// Interface SortedListInterface for the ADT SortedList.
// *********************************************************
public interface SortedStringListInterface
{
public boolean isEmpty();
// Determines whether a sorted list is empty
public int size();
// Returns the number of items that are in a sorted list
public void add(String item) throws ListException;
// Inserts item into it's proper position in a sorted list
// Throws an exception if the item cannot be placed on the list
public String get(int index) throws ListIndexOutOfBoundsException;
// Retrieves the item at position index of a sorted list, if 1 <= index <= size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
public void remove(String item) throws ListException;
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
public int locateIndex(String item);
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
public void removeAll();
// Removes all the items in the list
} // end ListInterface
Explanation / Answer
Hi Below is your code. I have implemented a driver class and main function to test it .. Please integrate the code with your test class and let me know in comments if you face any issue: -
SortedStringList.java
import java.util.ArrayList;
public class SortedStringList implements SortedStringListInterface {
private static ArrayList<String> data; // a reference to an ArrayList of
// String
// objects
public SortedStringList()
// creates an empty list
{
data = new ArrayList<>();
} // end no-args constructor
public boolean isEmpty()
// Determines whether a list is empty
{
return data.isEmpty();
} // end isEmpty
public int size()
// Returns the number of items that are in a list
{
return data.size();
} // end size
public void removeAll()
// Removes all the items in the list
{
data.clear();
} // end removeAll
public void add(String item) throws ListException
// Inserts item into its proper position in a sorted list
// Throws an exception if the item cannot be placed on the list
{
try {
if (data.isEmpty()) {
data.add(item);
} else {
int i;
for (i = 0; i < data.size(); i++) {
if (data.get(i).compareTo(item) < 0) {
continue;
} else if (data.get(i).compareTo(item) >= 0) {
data.add(i, item);
break;
}
}
if(data.size() == i) {
data.add(item);
}
}
} catch (Exception e) {
throw new ListException("Add to List failed: " + e.toString());
}
}
public String get(int index) throws ListIndexOutOfBoundsException
// Retrieves the item at position index of a sorted list, if 1 <= index <=
// size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
{
if (index >= size() || index < 0) {
throw new ListIndexOutOfBoundsException(index + " is an invalid index");
}
return data.get(index);
}
public void remove(String item) throws ListException
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
{
try {
boolean isDel = data.remove(item);
if (!isDel) {
throw new Exception();
}
} catch (Exception e) {
throw new ListException("Remove " + item.toString() + " from List failed: " + e.toString());
}
}
public int locateIndex(String item)
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
{
return data.indexOf(item);
}
} // end SortedStringList
SortedStringListInterface.java
public interface SortedStringListInterface {
public boolean isEmpty();
// Determines whether a sorted list is empty
public int size();
// Returns the number of items that are in a sorted list
public void add(String item) throws ListException;
// Inserts item into it's proper position in a sorted list
// Throws an exception if the item cannot be placed on the list
public String get(int index) throws ListIndexOutOfBoundsException;
// Retrieves the item at position index of a sorted list, if 1 <= index <=
// size().
// The list is left unchanged by this operation.
// Throws an exception when index is out of range.
public void remove(String item) throws ListException;
// Removes the item from a sorted list.
// Throws an exception if the item is not found.
public int locateIndex(String item);
// Returns the position where the item belongs or exists in a sorted list;
// item and the list are unchanged.
public void removeAll();
// Removes all the items in the list
} // end ListInterface
ListException.java
public class ListException extends Exception {
public ListException(String string) {
super(string);
}
}
ListIndexOutOfBoundsException.java
public class ListIndexOutOfBoundsException extends Exception {
public ListIndexOutOfBoundsException(String string) {
super(string);
}
}
SortedListDriver.java
public class SortedListDriver {
public static void main(String[] args) throws ListException, ListIndexOutOfBoundsException {
String[] strings = {"Bob","Akshay","Call","Friend","Love","Health"};
SortedStringList strList = new SortedStringList();
for (int i = 0; i < strings.length; i++) {
strList.add(strings[i]);
}
System.out.print("ArrayList Contains "+strList.size()+" elements.They are : ");
for (int i = 0; i < strList.size(); i++) {
System.out.print(strList.get(i)+" ");
}
System.out.println();
System.out.println("Adding Death : ");
strList.add("Death");
System.out.print("ArrayList Contains "+strList.size()+" elements.They are : ");
for (int i = 0; i < strList.size(); i++) {
System.out.print(strList.get(i)+" ");
}
System.out.println();
System.out.println("Removing Call");
strList.remove("Call");
System.out.print("ArrayList Contains "+strList.size()+" elements.They are : ");
for (int i = 0; i < strList.size(); i++) {
System.out.print(strList.get(i)+" ");
}
System.out.println();
System.out.println("Index of Friend(starting from 0) is "+strList.locateIndex("Friend"));
}
}
Sample Output: -
ArrayList Contains 6 elements.They are : Akshay Bob Call Friend Health Love
Adding Death :
ArrayList Contains 7 elements.They are : Akshay Bob Call Death Friend Health Love
Removing Call
ArrayList Contains 6 elements.They are : Akshay Bob Death Friend Health Love
Index of Friend(starting from 0) is 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.