Question

In: Computer Science

Submit your complete implementation of our abstract list type using an array, ArrayList.java. public class ArrayList...

Submit your complete implementation of our abstract list type using an array, ArrayList.java.

public class ArrayList {
public static void main(String[] args) {
ArrayList list = new ArrayList();

}
private String[]a = new String[1000];
private int end = -1;

 // Throw an IndexOutOfBoundsException if the index is invalid
  public String get(int index); 

  // Return the first index in the list with the given value, or -1 if it's not found
  public int find(String val); 

  // Return true if the element is successfully added, false if the list is full
  public boolean add(String val); 

  // Throw an IndexOutOfBoundsException if the index is invalid
  public void add(int index, String val); 

  // This method should return true only if the value is in the list and removed, 
  // false if the value isn't found in the list 
  public boolean remove(String val); 

  // return the value that's removed; throw an IndexOutOfBoundsException if the index is invalid
  public String remove(int index); 

  // LENGTH() → int
  public int size(); 

  // Return true if the list is empty, false if it's not
  public boolean isEmpty(); 

  // Return true if the value is in the list, false if it's not
  public boolean contains(String value); 

  // DELETE_ALL()
  public void clear(); 

  // Convert the contents into a single string with values separated by commas. 
  // It's OK to have a comma at the end of the String.
  public String toString(); 

Solutions

Expert Solution

Program:

//ArrayList class
class ArrayList
{
   //main method
   public static void main(String[] args)
   {
       ArrayList list = new ArrayList();
      
       list.add("C");
       list.add("C++");
       list.add("C#");
       list.add("Java");
       list.add("Python");
      
       System.out.println(list);
      
       System.out.println("Size = " + list.size());
      
       System.out.println("Is empty = " + list.isEmpty());
      
       System.out.println(list.get(2));
      
       System.out.println(list.contains("C#"));
      
       list.remove(2);
       list.remove("Python");
      
       System.out.println(list.contains("C#"));
      
       System.out.println(list);
      
       list.clear();
      
       System.out.println(list);
      
       System.out.println("Is empty = " + list.isEmpty());

   }
   //private members
   private String[]a = new String[1000];
   private int end = -1;

    // Throw an IndexOutOfBoundsException if the index is invalid
   public String get(int index)
   {
       if(index<0 || index >=a.length)
           throw new IndexOutOfBoundsException();
      
       return a[index];
   }

   // Return the first index in the list with the given value, or -1 if it's not found
   public int find(String val)
   {  
       for(int i=0; i<=end; i++)
       {
           if(a[i].equals(val))
               return i;
       }
      
       return -1;
   }

   // Return true if the element is successfully added, false if the list is full
   public boolean add(String val)
   {
       if(end+1 >= a.length) return false;
       end++;
       //add the value
       a[end] = val;
       return true;
   }

   // Throw an IndexOutOfBoundsException if the index is invalid
   public void add(int index, String val)
   {
       if(index<0 || index >=a.length)
           throw new IndexOutOfBoundsException();
       a[index] = val;
   }

   // This method should return true only if the value is in the list and removed,
   // false if the value isn't found in the list
   public boolean remove(String val)
   {
       int f = find(val);
       if(f==-1)
           return false;
       //remove the value
       for(int i=f; i<end; i++)
           a[i] = a[i+1];
       end--;
       return true;
   }

// return the value that's removed; throw an IndexOutOfBoundsException if the index is invalid
public String remove(int index)
{
       if(index<0 || index > end)
           throw new IndexOutOfBoundsException();
          
       String s = a[index];
       //remove the value
       for(int i=index; i<end; i++)
           a[i] = a[i+1];
       end--;
       return s;
}

// LENGTH() → int
public int size()
{
   return (end + 1);
}

// Return true if the list is empty, false if it's not
public boolean isEmpty()
{
   if(end==-1) return true;
  
   return false;
}

// Return true if the value is in the list, false if it's not
public boolean contains(String value)
{
   int i = find(value);
   if(i==-1) return false;
   return true;
}

// DELETE_ALL()
public void clear()
{
   end = -1;
}

// Convert the contents into a single string with values separated by commas.
// It's OK to have a comma at the end of the String.
public String toString()
{
   String str = "[]";
   if(end==-1) return str;
  
   str = "[";
   for(int i=0; i<end; i++)
   {
       str = str + a[i] + ", ";
   }
   str = str + a[end] + "]";
  
   return str;
}
}

Output:

[C, C++, C#, Java, Python]
Size = 5
Is empty = false
C#
true
false
[C, C++, Java]
[]
Is empty = true

N.B: Whether you face any problem then share with me in the comment section, I'll happy to help you. I expect positive feedback from your end.


Related Solutions

1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template...
1) Submit your completed dynamic array with template/generic data type (DArray.java) implementation. 2) Complete the template DArray (if you have not done so) and this attached Iterator class. Make sure to document and test your code thoroughly. import java.util.Arrays; public class DArray { private final double GROW_FACTOR = 0.5;// array size growing rate //attributes private int size; private int buffer[]; //the actual array //constructors public DArray() { this.size=10; } public DArray(int size) throws Exception { if(size < 0) { throw...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public...
Define the classes to complete dynamic array hierarchy with a concrete, abstract and interface class. public class DArray { private int array[]; public DArray() { } private void expandArray() { } private void shrinkArray() { } } --------------------------------------------------------------- public abstract class ArrayBP { protected int numElements; protected int numAllocations; public abstract void storeAt(int item, int index) { } public abstract getFrom(int index) { } public abstract int len() { } public abstract void remove();{ } public abstract void removeAt(int index)...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
Complete the implementation of the Die class. Note that the Die class uses an array to...
Complete the implementation of the Die class. Note that the Die class uses an array to represent the faces of a die. You need to complete the no-argument constructor and the two methods compareTo and equals. Code: import java.util.Arrays; import java.util.Objects; import java.util.Random; /* * NOTE TO STUDENTS: * The constructor that you need to complete can be found on line 47. * * The two methods you need to complete can be found at the end of this file....
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What...
Give a pseudo-code description for an array-based implementation of the deque ADT (Abstract Data Type). What is the running time for each operation?
Using Java Languse, Complete ArraySet.java down below by using (Array collection) : package Homework3; public class...
Using Java Languse, Complete ArraySet.java down below by using (Array collection) : package Homework3; public class ArraySet extends ArrayCollection { public ArraySet() { } public ArraySet(int size) { super(size); } public boolean add(T element) { // Complete your code here return true; } } ArrayCollection.java: package Homework3; public class ArrayCollection { protected static final int DEFAULT_CAPACITY = 100; protected T[] elements; protected int numberOfElements; public ArrayCollection() { this(DEFAULT_CAPACITY); } public ArrayCollection(int size) { elements = (T[]) new Object[size]; numberOfElements =...
Using the implementation of the array based list given, write a CLIENT method (that is NOT...
Using the implementation of the array based list given, write a CLIENT method (that is NOT part of the class) called replace, that given a value and a position replaces the value of the element at that position. REMEMBER error checking public static void replace( List aList, int newValue, int position) public class ArraybasedList implements MyListInterface{ private static final int DEFAULTSIZE = 25; // Data members: private int currentSize; private int maxSize; private S[] elements; //default constructor has NO parameters...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT