Question

In: Computer Science

I have the following code: //Set.java import java.util.ArrayList; public class Set<T> { //data fields private ArrayList<T>...

I have the following code:

//Set.java

import java.util.ArrayList;

public class Set<T> {
    //data fields
    private ArrayList<T> myList;
    // constructors
    Set(){
        myList = new ArrayList<T>();
    }
    // other methods
    public void add(T item){
        if(!membership(item))
            myList.add(item);
    }

    public void remove(T item){
        if(membership(item))
            myList.remove(item);
    }

    public Boolean membership(T item){
        for (T t : myList)
            if (t.equals(item))
                return true;
        return false;
    }

    public String toString(){
        StringBuilder str = new StringBuilder("[ ");

        for(int i = 0; i < myList.size() - 1; i++)
            str.append(myList.get(i).toString()).append(", ");

        if(myList.size() > 0)
            str.append(myList.get(myList.size() - 1).toString());

        str.append(" ]");

        return str.toString();
    }

}

(20 points) Fill the following table with the big-O running times of each of the methods implemented, as well as the running times for the same methods if they were implemented with LinkedList instead. (No need to explain here, only the big-O expression.)

Array List Linked List
add
remove
membership
toString

(20 points) Explain each of the eight running times in the table applying the rules of counting seen in class, and for the method calls, if 4 we have covered them already, directly refer to the running times of those methods. (Partial credit if you do not explain all eight. No points for ambiguous explanations. For instance, if you implement add in the class Set using addFirst in the class ArrayList, then you should say something like the following. “The add method of Set is implemented with just one method call to addFirst of ArrayList. addFirst of ArrayList is in O(n) in the worst case. Hence, the same bound on the running time applies to add”. )

Solutions

Expert Solution

Array List Linked List
add O(n) O(n)
remove O(n2)   O(n)
membership O(n) O(n)
toString   O(n) O(n)

Explanation:

1) add :
For adding an element we need to check if it is present in the ArrayList/Linked List of not, This will take O(n) time and then we can just add that element . Hence for ArrayList/Linked List, this will take O(n) time


2) remove : For removing an element we need to check if it is present in the ArrayList/Linked List of not, This will take O(n) time and then we can just remove that element from arrayList . Hence for ArrayList/Linked List, this will take O(n2) time.
Membership will take n and remove takes n. Hence its n*n = n2

In Linked list we just need to manipulate references, So find the element and then manipulate the references as we dont have to shift all the elements unlike arrayList.

3) Membership : if it is present in the ArrayList/Linked List of not, This will take O(n) time as we need to iterate till the end.
Hence this will be same for arrayList or Linked list


4) toString(): We iterate the list or Linkedlist till the size. Hence for ArrayList/Linked List, this will take O(n) time.

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the...
blueJ code given: import java.until.ArrayList; public class TestArrayList; { private ArrayList<TestArrays> testArrayList; /** * Complete the constructor for the class. Instantiate the testArrayList *and call the fillArrayList method to add objects to the testArrayList. * @param numElements The number of elements in the ArrayList */ public TestArrayLists(int numElements) { } /** * Complete the fillArrayList method. It should fill the testArrayList with * TestArrays objects consisting of 10 element int Array of random numbers. * @param numElements The number of...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT