QUESTION

Text
Image

Need help with this assignment in Java language. The program is already almost completed, all I have to do is to modify the "TODO" methods so that the output will be exactly the same as it is shown in the document below. Providing the starter code below and I work in Eclipse editor. Thanks in advance


Assignment-01 $\triangle$ SinglyLinkedLists in Java - Implementing the IList Interface Linked-Lists are the most primitive data structures. In this assignment you will implement a Singly-linked-List for a Collection Data Interface IList. The starter code is attached herewith. Please note you need to write codes only in the "SinglyLinkedList.java" file. Please do not modify any other files. Following are the Interface functions you must implement inside your "SinglyLinkedList.java" file. public interface luists void add(int item); //adds an item at the end of the list void add(int index, int item); //adds an item at a position specified by the index, //if index goes outside list size, the program adds the item at the end of the list void clear(); //clears the entire list and list size becomes 0 int indexOf(int item); //returns an items position (index value) from the list int get(int index); //gets an item from the list specified by the index value, //return - 1 if item is not found boolean remove(int index); //removes an item specified by the index value //and returns a boglean (successful or failed when value not exists) boolean removeAll(int item); // removes all occurrences of item from the list and returns a //bqolean (successful when at-least one item removed or failed when value not exists) int size(); //returns the size of the list void reverse(); //reverses the entire list void sort(); //sorts the contents of the list in ascending order String toString(); //displays the entire list \} After successful implementations of the functions, your main method will produce the following exact output: List items: $7 \rightarrow 98 \rightarrow 6 \rightarrow 77 \rightarrow 22 \rightarrow 42 \rightarrow 78 \rightarrow 777$. List Size: 8 List Empty! List Size: 0 List items: $61 \rightarrow 34 \rightarrow 31 \rightarrow 46 \rightarrow 51$. List Size: 5 Index of 31 is 2 . Index of 1000 is -1 . Value at index $-3,4$ and 100 are: $-1,51$, and -1 List items: $34 \rightarrow 31 \rightarrow 51$. List Size: 3 List items: $34 \rightarrow 31 \rightarrow 51 \rightarrow 31 \rightarrow 32 \rightarrow 33 \rightarrow 31$. Successfully removed items from the list. After removal operation list items are: List items: $34 \rightarrow 51 \rightarrow 32 \rightarrow 33$. The updated reversed list is: List items: $33 \rightarrow 32 \rightarrow 51 \rightarrow 34$. The sorted list is: List items: $32 \rightarrow 33 \rightarrow 34 \rightarrow 51$.

STARTER CODE:

IList.java - class code

/*please do not modify a single character from this java file */
public interface IList{
void add(int item); //adds an item at the end of the list
void add(int index, int item); //adds an item at a position specified by the index, if index goes outside list size, the program adds the item at the end of the list
void clear(); //clears the entire list and list size becomes 0
int indexOf(int item); //returns an items position (index value) from the list
int get(int index); //gets an item from the list specified by the index value, return -1/null if item is not found
boolean remove(int index); //removes an item specified by the index value and returns a boolean (successful or failed when value not exists)
boolean removeAll(int item); // removes all occurrences of item from the list and returns a boolean (successful when at-least one item removed or failed when value not exists)
int size(); //returns the size of the list
void reverse(); //reverses the entire list
void sort(); //sorts the contents of the list in ascending order
String toString(); //displays the entire list
}

Node.java - class code

/*please do not modify a single character from this java class*/
public class Node {
public int value;
public Node next=null;
public String toString()
{
return String.valueOf(value);
}
}

SinglyLinkedList.java - class code - implement the changes only here!

public class SinglyLinkedList implements IList{

Node head;

@Override
public void add(int item) {
// TODO Auto-generated method stub

}

@Override
public void add(int index, int item) {
// TODO Auto-generated method stub

}

@Override
public void clear() {
// TODO Auto-generated method stub

}

@Override
public int indexOf(int item) {
// TODO Auto-generated method stub
return 0;
}

@Override
public int get(int index) {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean remove(int index) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean removeAll(int item) {
// TODO Auto-generated method stub
return false;
}

@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}

@Override
public void reverse() {
// TODO Auto-generated method stub

}

@Override
public void sort() {
// TODO Auto-generated method stub

}

public String toString()
{
Node start=head;
String str="";
if(start==null) return "List Empty!";
while(start.next!=null)
{
str+=start.value+" -> ";
start=start.next;
}
str+=start.value+".";
return "List items: "+str;
}
}

SinglyLinkedListTest.java - code

import java.util.Random;
/*please do not modify a single character from this java class*/

public class SinglyLinkedListTest {

public static void main(String[] args) {

IList myList=new SinglyLinkedList();
Random r=new Random(1); //do not change the seed parameter
for(int i=0; i<5; i++)
myList.add(r.nextInt(101)+1);
myList.add(0, 7);
myList.add(3, 77);
myList.add(1000, 777);
System.out.println(myList);
System.out.println("List Size: "+myList.size());
myList.clear();
System.out.println(myList);
System.out.println("List Size: "+myList.size());

for(int i=0; i<5; i++)
myList.add(r.nextInt(101)+1);
System.out.println(myList);
System.out.println("List Size: "+myList.size());
int sVal=31;
System.out.format("Index of %d is %d.\n",sVal, myList.indexOf(sVal));
sVal=1000;
System.out.format("Index of %d is %d.\n",sVal, myList.indexOf(sVal));
int index=4;
System.out.println("Value at index -3, "+index+" and 100 are: "+myList.get(-3)+", "+myList.get(index)+", and "+myList.get(100));
myList.remove(0);
myList.remove(-10);
myList.remove(100);
myList.remove(2);
System.out.println(myList);
System.out.println("List Size: "+myList.size());
myList.add(31);
myList.add(32);
myList.add(33);
myList.add(31);
System.out.println(myList);
if(myList.removeAll(31)==true)
System.out.println("Successfully removed items from the list. \n" +"After removal operation list items are: "+myList);
myList.reverse();
System.out.println("The updated reversed list is: "+myList);
myList.sort();
System.out.println("The sorted list is: "+myList);

}
}

Public Answer

PPMTDZ The First Answerer