Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new project called simplelist. The following partial code shows a simpl

ID: 3638384 • Letter: C

Question

Create a new project called simplelist.
The following partial code shows a simple Java implementation of a List in List.java, ArrayList in ArrayList.java , LinkedList in LinkedList.java, Node in Node.java, and ListTester in ListTester.java classes (parts of the code is missing, you are to complete it). ===========

Notice that the LinkedList class makes use of a Node class which is exactly the same as the one used for the dynamic queue.


List.java

interface List{

public void add(int index,Object data);

public void remove(Object data);

public Object get(int index);

public int size();

}

ArrayList.java

public class ArrayList implements List {

private int length = 0; //current mumber of items
private int size = 10; //capacity
private Object[] alist = new Object[10];

public void ArrayList () {

}

public void ArrayList (int size) {
this.size = size;
this.alist = new Object[size];
}

//list = new Object[10];
public void add(int index, Object data) {
======
======
======
======
======
======
======
======
======
======
======
======
}

public void remove(Object data) {
int j = -1; // index of the found data
int i;
for ( i = 0; i < length; i++){
if (alist[i] == data){
j=i;
break;
}
}

if (j != -1){
for (i = j+1; i < length; i++){
alist[j] = alist[i];
j++;
}

}

}

public Object get(int index) {
return alist[index];
}

public int size() {
return length;
}
}

LinkedList.java

public class LinkedList implements List{

private Node head = null;
private int length = 0; // the number of data items in the list

public void LinkedList()
{

}

public void add(int index,Object data)
{
======
======
======
======
======
======
======
======
======
}

public void remove(Object data){
Node nodeRef = head;
Node nodePrev = null;
while (nodeRef != null){
if (nodeRef.dataItem.equals(data)){
if (nodePrev == null)
head = nodeRef.nextNode;
else
nodePrev.nextNode = nodeRef.nextNode;
length--;
}
nodePrev = nodeRef;
nodeRef = nodeRef.nextNode;
}
}

public Object get(int index){
Node nodeRef = head;
int i = 0;
while ((nodeRef != null)& (i <= index)){
if (i == index){
return (nodeRef.dataItem);
}
else{
i++;
nodeRef = nodeRef.nextNode;
}

}
return (head.dataItem);

}

public int size() {
return length;
}
}


Node.java

/**
* class Node
*
*/
public class Node
{
Object dataItem;
Node nextNode;
}


ANSWER:







Using an ArrayList

Controller Class: Note that the constructor parameter type is List, so an instance of either subclass can be tested.
The following class ListTester.java shows how to use a List to hold String objects. Calls to List operations are shown in bold.


ListTester.java

public class ListTester {

private List list;

public ListTester(List list)
{
this.list = list;
}

/**
* add item string to list
*/
public void addString(String str)
{
list.add(0, str);
System.out.println("Added new string");
}

/**
* remove item string from list
*/
public void removeString(String str)
{
list.remove(str);
System.out.println("Removed String :" + str);

}

/**
* return item at index
*/
public String returnStringAtIndex(int index)
{
String item = "";
Object str = list.get(index);
System.out.println("Returned: " + str);
return (item);
}

/**
* list the strings in list
*/
public void listStringsInlist(List mylist)
{
System.out.println("Strings in list are: ");
System.out.println();

int length = mylist.size();

for (int i = 0; i < length; i++)
{
String item = (String) mylist.get(i);
System.out.println(item);
}
System.out.println();
}

Create a Driver.java class (it contains main).
1. Create a new instance of ArrayList called alist with capacity 10.

2. Create a new instance of Controller Class called ListTester called atest and select your ArrayList instance alist in the object bench as the parameter in the constructor. This means that you will be testing the ArrayList you created in the previous step. Note that the constructor parameter type is List, so an instance of either subclass can be tested.

3. Call the addString method of ListTester repeatedly to add the following strings (they will be added at the index 0):
“apples”, “hazelnuts”, “bananas”, “raisins”, “coconuts”, “sultanas”

4. Inspect the ArrayList instance.
a. What attribute contains the data?
5. Inspect the data in ArrayList:

6. Call the removeString method of your ListTester to remove “apples” and inspect the data again.
i. Describe the result of this operation.



Using a LinkedList

7. Repeat the same exercise as above, this time using a LinkedList instance called llist instead of an ArrayList.
Create a new instance of LinkedList called llist.

8. Create a new instance of Controller Class called ListTester called ltest and select your LinkedList instance llist in the object bench as the parameter in the constructor. This means that you will be testing the LinkedList you created in the previous step. Note that the constructor parameter type is List, so an instance of either subclass can be tested.

9. Compare the results of calling the ListTester methods with the results you
got using the ArrayList.

Compare the way the data items are stored in the LinkedList and ArrayList.

Explanation / Answer

import java.net.*; import java.io.*; public class MyLoader { public static void main (String argv[]) throws Exception { URLClassLoader loader = new URLClassLoader(new URL[] { new URL("http://www.javacourses.com/classes/") }); // Load class from class loader. argv[0] is the name of the class to be loaded Class c = loader.loadClass (argv[0]); // Create an instance of the class just loaded Object o = c.newInstance(); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote