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

[JAVA] fix errors import java.util.*; import java.util.ArrayList; public class L

ID: 3706430 • Letter: #

Question

[JAVA]

fix errors

import java.util.*;
import java.util.ArrayList;

public class List
{
public static void main(String[] args)
{
new List();
}
public List()
{
String[] name = {"A", "R", "V", "B", "U", "M"};
MyList<String> list = new MyLinkedList<String>(name);
System.out.println(list.contains("R"));
System.out.println(list.get(3));
System.out.println(list.indexOf("R"));
System.out.println(list.lastIndexOf("George"));
list.set(4, "Barnie");
System.out.println(list);
}
public interface MyList<E> extends java.lang.Iterable {
public void add(E e);
public void add(int index, E e);
public void clear();
public boolean contains(E e);
public E get(int index);
public int indexOf(E e);
public boolean isEmpty();
public int lastIndexOf(E e);
public boolean remove(E e);
public E remove(int index);
public object set(int index, E e);
public int size();
}
public abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0;
protected MyAbstractList(){}
protected MyAbstractList(E[] objects)
   {
   for (int i = 0; i < objects.length; i++)
   add(objects[i]);
   }
public void add(E e) {
   add(size, e);
}
public boolean isEmpty() {
   return size == 0;
}
public int size() {
   return size;
}
public boolean remove(E e) {
   if (indexOf(e) >= 0){
    remove(indexOf(e));
    return true;
   }
   else
    return false;
   }
}
public class MyLinkedList<E> extends MyAbstractList<E> {
   private Node<E> head,tail;
   public MyLinkedList()
   {
   }
   public MylinkedList(E[] objects) {
    super(objects);
      }

   public E getFirst(){
    if (size == 0)
        {
     return null;
        }
    else {
     return head.element;
      }
        }


   public E getLast() {
    if (size == 0 ) {
     return null;
    }
    else {
     return tail.element;
    }
   }
   public void addFirst (E e) {
    Node<E> newNode = new Node<E>(e);
    newNodeNext = head;
    head = newNode;
    size++;
    if (tail == null)
    tail = head;
   }
   public void addLast(E e) {
    Node<E> newNode = new Node<E>(e);
    if (tail == null) {
     head = tail = newNode;
    }
    else {
     tail.next = newNode;
     tail = tail.next;
    }
    size++;
   }
   public void add(int index, E e){
    if (index == 0) {
     addFirst(e);
    }
    else if (index >= size) {
     addLast(e);
    }
    else {
     Node<E> current = head;
     for (int i = 1; i < index; i++)
     {
      current = current.next;
     }
     Node<E> temp = current.next;
     current.next = new Node<E>(e);
     (current.next).next = temp;
     size++;
    }
   }
   public E removeFirst() {
    if (size == 0) {
     return null;
    }
    else {
     Node<E> temp = head;
     head = head.next;
     size--;
     if (head == null)
     {
      tail = null;
    }
    return temp.element;
   }
}
public E removelast() {
   if (sieze == 0){
    return null;
   }
else if (size == 1) {
Node<E> temp = head;
head = tail = null;
size = 0;
return temp.element;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++){ //FOR
current = current.next;
}
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) return null; // Out of range
else if (index == 0) return removeFirst(); // Remove first
else if (index == size - 1) return removeLast(); // Remove last
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", ");
}
else {
result.append("]");
}
}
return result.toString();
}
@Override
public void clear() {
size = 0;
head = tail = null;
}
@Override
public boolean contains(Object o) {
Node<E> current = head;
for(int i = 0; i < size; i++) {
if (current.element.equals(o))
return true;
current = current.next;
}
return false;
}
return false;
}

public E get(int index) {
if (index < 0 || index > size - 1) return null;

Node<E> current = head.next;
for (int i = 1; i < index; i++)
current = current.next;
return current.element;
}
public int indexOf(Object o) {
Node<E> current = head;
for( int i = 0; i < size; i++)
{
if (current.element.equals(o)) return i;
current = current.ext;
}
return -1;
}

public int lastIndexOf(Object o) {
int lastIndex = -1;
Node<E> current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(o))
lastIndex = i;
current = current.next;
}
return lastIndex;
}
public E set(int index, E e) {
if (index < 0 || index > size - 1) return null;
else {
Node<E> current = head;
for (int i = 0; i < index; i++)
current = current.next;
E temp = current.element;
current.element = e;
return temp;
}

public java.util.Iterator<E> iterator() {
return new LinkedListIterator();
}
private class LinkedListIterator
implements java.util.Iterator<E> {
private Node<E> current = head; // Current index

public boolean hasNext() {
return (current != null);
}
@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}

public void remove() {
}
}
private class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}
}

Explanation / Answer

I have done changes into the above program to compllng. The program has compiled and executed successfully.

To complie the program successfully i have correted all errors. Added undefined class E,Node <E> code and removed improper braces and corrected the method names and constructor names.

I have added my comments what are all i fixed and added code for fixing the compilation errors.

Program

import java.util.*;
import java.util.ArrayList;


// I have declared E class
class E {}

// I have declared Node <E>class
class Node < E > {
public Node current;
public Node next;
E element;
  
}

// E and Node <E> classes i have declared, because you haven't declared that due to
// it is giving erros if you want define own E and Node <E> classes
public class List {
public static void main(String[] args) {
new List();
}

public List() {
String[] name = {
"A",
"R",
"V",
"B",
"U",
"M"
};
MyList < String > list = new MyLinkedList < String > (name);
System.out.println(list.contains("R"));
System.out.println(list.get(3));
System.out.println(list.indexOf("R"));
System.out.println(list.lastIndexOf("George"));
list.set(4, "Barnie");
System.out.println(list);
}

public interface MyList < E > extends java.lang.Iterable {
public void add(E e);
public void add(int index, E e);
public void clear();
public boolean contains(E e);
public E get(int index);
public int indexOf(E e);
public boolean isEmpty();
public int lastIndexOf(E e);
public boolean remove(E e);
public E remove(int index);
public E set(int index, E e); // Object type is wrong and return type is E not Object
public int size();
}

public abstract class MyAbstractList < E > implements MyList < E > {
protected int size = 0;
protected MyAbstractList() {}
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++)
add(objects[i]);
}

public void add(E e) {
add(size, e);
}

public boolean isEmpty() {
return size == 0;
}

public int size() {
return size;
}

public boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else
return false;
}
}

public class MyLinkedList < E > extends MyAbstractList < E > {
private Node < E > head,
tail;
public MyLinkedList() {}

public MyLinkedList(E[] objects) { //Improper constructor name. MyLinkedList() not MylinkedList()
super(objects);
}

public E getFirst() {
if (size == 0) {
return null;
} else {
return head.element;
}
}


public E getLast() {
if (size == 0) {
return null;
} else {
return tail.element;
}
}
public void addFirst(E e) {
Node < E > newNode = new Node < E > (e);
//newNodeNext = head; // newNodeNext has not declared and used. Unnessarly used this
head = newNode;
size++;
if (tail == null)
tail = head;
}
public void addLast(E e) {
Node < E > newNode = new Node < E > (e);
if (tail == null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail = tail.next;
}
size++;
}
public void add(int index, E e) {
if (index == 0) {
addFirst(e);
} else if (index >= size) {
addLast(e);
} else {
Node < E > current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node < E > temp = current.next;
current.next = new Node < E > (e);
(current.next).next = temp;
size++;
}
}
public E removeFirst() {
if (size == 0) {
return null;
} else {
Node < E > temp = head;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return temp.element;
}
}
public E removeLast() { // removeLast() is correct but not removelast()
if (size == 0) { //it is size not seize
return null;
} else if (size == 1) {
Node < E > temp = head;
head = tail = null;
size = 0;
return temp.element;
} else {
Node < E > current = head;
for (int i = 0; i < size - 2; i++) { //FOR
current = current.next;
}
Node < E > temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
//verride
public E remove(int index) {
if (index < 0 || index >= size) return null; // Out of range
else if (index == 0) return removeFirst(); // Remove first
else if (index == size - 1) return removeLast(); // Remove last
else {
Node < E > previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node < E > current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
//verride
public String toString() {
StringBuilder result = new StringBuilder("[");
Node < E > current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", ");
} else {
result.append("]");
}
}
return result.toString();
}
//@Override
public void clear() {
size = 0;
head = tail = null;
}
//@Override
public boolean contains(E o){// We should use E object ,but not Object o

Node < E > current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(o))
return true;
current = current.next;
}
return false;
}
//return false;
//} // removed the brases due to Incorrectly closed the class

public E get(int index) {
if (index < 0 || index > size - 1) return null;

Node < E > current = head.next;
for (int i = 1; i < index; i++)
current = current.next;
return current.element;
}
public int indexOf(E o ) {// We should use E object ,but not Object o
Node < E > current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(o)) return i;
current = current.next; // next spelling mistake as ext
}
return -1;
}

public int lastIndexOf(E o){ // We should use E object ,but not Object o
int lastIndex = -1;
Node < E > current = head;
for (int i = 0; i < size; i++) {
if (current.element.equals(o))
lastIndex = i;
current = current.next;
}
return lastIndex;
}
public E set(int index, E e) {
if (index < 0 || index > size - 1) return null;
else {
Node < E > current = head;
for (int i = 0; i < index; i++)
current = current.next;
E temp = current.element;
current.element = e;
return temp;
}

} // added brases in proper place  
public java.util.Iterator < E > iterator() {
return new LinkedListIterator();
}
  
  
class LinkedListIterator implements java.util.Iterator < E >
{
private Node < E > current = head; // Current index

public boolean hasNext() {
return (current != null);
}
//@Override
public E next() {
E e = current.element;
current = current.next;
return e;
}

public void remove() {}
}
  
  
class Node < E > {
E element;
Node < E > next;
public Node(E element) {
this.element = element;
}
}
  
}
}

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