Question

In: Computer Science

Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3....

Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been provided to allow you to insure that your LinkedList implementation is correct. b. Check your progress by noting the number of passing and failing tests. As you implement more of the class, more tests will pass, until all tests are passing. c. The provided tests are sufficient to show insure the LinkedList class is implemented correctly. d. You are free to add more tests as you feel are necessary; however, none of the existing tests should be modified or removed. here is the provided code class

class LinkedList<E> {

    private Node<E> head;

    // the constructor.


    public LinkedList(Node<E> head) {
        this.head = head;
    }

    /**
     * TODO Implement this method.
     * Add the given element, value, to the end of the list.
     * @param value The value to be added.
     */
    public void add(E value) {

    }

    /**
     * TODO Implement this method.
     * Add the given element, value, to the list at the given index.
     * After this operation is complete, get(index) will return value.
     * After this operation, all elements that had an index
     * greater than index (as determined by get()) will have their index increased by one.
     * This operation is only valid for 0 <= index <= size().
     * @param index The index at which to add value.
     * @param value The value to be added.
     * @throws IndexOutOfBoundsException when index is less than zero or greater than size.
     */
    public void add(int index, E value) {

    }

    /**
     * TODO Implement this method.
     * Determine if the LinkedList is empty or not.
     * @return true if this LinkedList has no items. (This is the same as the size equal to zero.)
     * false if the size is greater than zero.
     */
    public boolean isEmpty() {
        return false;
    }

    /**
     * TODO Implement this method.
     * Return the size (number of items) in this LinkedList.
     * @return the number of items in the list.
     */
    public int size() {
        return -1;
    }

    /**
     * TODO Implement this method.
     * Return the element of the list at the given index.
     * This operation is only valid for 0 <= index < size().
     * This operation does not modify the list.
     * @param index The index at which to retrieve an element.
     * @return The element of the list at the given index.
     * @throws IndexOutOfBoundsException when index is less than zero or greater than or equal to size.
     */
    public E get(int index) {
        return null;
    }

    /**
     * TODO Implement this method.
     * Remove and return the first element (element number zero) from the list.
     * This operation is only valid for non-empty (size() > 0) lists.
     * @return The removed element.
     * @throws IndexOutOfBoundsException When the list is empty.
     */
    public E remove() {
        return null;
    }

    /**
     * TODO Implement this method.
     * Remove and return the element with the given index from the list.
     * This operation is only valid for 0 <= index < size().
     * After this operation, all elements that had an index
     * greater than index (as determined by get()) will have their index reduced by one.
     * @param index The index to remove an element from.
     * @return The removed element.
     * @throws IndexOutOfBoundsException when removing from index less than zero or greater than or equal to size.
     */
    public E remove(int index) {
        return null;
    }
}

Solutions

Expert Solution

//Linked list class

public class LinkedList<E> {
   private Node<E> head;
   private int size;

   public Node<E> getHead() {
       return head;
   }

   public void setHead(Node<E> head) {
       this.head = head;
   }

   public LinkedList(Node<E> head) {
       this.head = head;
       this.size=1;
   }

   public LinkedList() {
       this.size=0;
   }
  
   /**
   * This method will add the element in the end of the list.
   * @param value
   */
   public void add(E value){
       Node<E> node = new Node(value);
       Node<E> prev = head;
       while(prev.getNext()!=null){
           prev = (Node<E>) prev.getNext();
       }
       prev.setNext(node);
       ++size;
   }
  
   /**
   * This method will insert the element at the given index. Index value needs to be in the range of 0<=index<=size
   *
   * @param index
   * @param value
   * @throws IndexOutOfBoundsException
   */
   public void add(int index, E value) throws IndexOutOfBoundsException {
       if (index < 0 || index >= size) {
           throw new IndexOutOfBoundsException();
       } else {
           Node node = new Node(value);
           int count = 0;
           if (index == 0) {
               node.setNext(head);
               head = node;
           } else {
               Node prev = head;
               while (true) {
                   if (++count == index) {
                       //Node tmp = prev;
                       //tmp.setNext(node);
                       node.setNext(prev.getNext());
                       prev.setNext(node);
                       break;
                   }
                   prev = prev.getNext();
               }
           }
           ++size;
       }
   }
  
   /**
   * This method will check whether the linked list is empty or not.
   * @return true/false
   */
   public boolean isEmpty(){
       if(size>0){
           return false;
       }
       return true;
   }
  
   /**
   * This method will return the size of linked list.
   * @return
   */
   public int size(){
       return this.size;
   }
  
   /**
   * This method will return the element value at the specified index.
   * @param index
   * @return
   * @throws IndexOutOfBoundsException
   */
   public E get(int index) throws IndexOutOfBoundsException{
       if(index>=0 && index<size){
           Node temp = head;
           int count = 0;
           while(true){
               if(count++==index){
                   return (E) temp.getValue();
               }
               temp = temp.getNext();
           }
          
       }else
           throw new IndexOutOfBoundsException();
          
   }
  
   /**
   * This method will return the remove the first element from the linked list.
   * @return
   * @throws IndexOutOfBoundsException
   */
   public E remove() throws IndexOutOfBoundsException {
       if(size==0)
           throw new IndexOutOfBoundsException();
       else{
           Node temp = head;
           head = (Node<E>) head.getNext();
           --size;
           return (E) temp.getValue();
       }
   }
  
   /**
   * This method will return the element at the given element.
   * @param index
   * @return
   * @throws IndexOutOfBoundsException
   */
   public E remove(int index) throws IndexOutOfBoundsException {
       if(index>=0 && index<size){
           E value = head.getValue();
           int count = 0;
           if(index==0){
               head = (Node<E>) head.getNext();
               --size;
               return value;
           }else{
               Node prev = head;
               while(true){
                   if(++count==index){
                       Node n = prev.getNext();
                       value = (E) n.getValue();
                       prev.setNext(n.getNext());
                       break;
                   }
                   prev = prev.getNext();
               }
               --size;
               return value;
              
           }
          
          
       }else{
           throw new IndexOutOfBoundsException();
       }
   }
  
  
}

//Node class

class Node<E>{
   private E value;
   private Node<?> next;
   public Node() { }
   public Node(E value) {
       super();
       this.value = value;
       this.next = null;
   }
   public E getValue() {
       return value;
   }
   public void setValue(E value) {
       this.value = value;
   }
   public Node<?> getNext() {
       return next;
   }
   public void setNext(Node<?> next) {
       this.next = next;
   }
  
}

//Main class for testing

public class Main {

   public static void main(String[] args) {
       Node<Integer> node = new Node<Integer>(5);
       LinkedList<Integer> l = new LinkedList<Integer>(node);      
       l.add(7);
       l.add(9);
       l.add(1, 3);
       System.out.println("Size: "+l.size());
       l.remove(2);
       System.out.println("Size: "+l.size());
       l.remove();
       System.out.println("Size: "+l.size());
       System.out.println("IS LINKEDLIST EMPTY: "+l.isEmpty());
       System.out.println("Element at index 0: "+l.get(0));

   }

}

//Output:

Size: 4
Size: 3
Size: 2
IS LINKEDLIST EMPTY: false
Element at index 0: 3


Related Solutions

Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides...
Write in Java! (Not Javascript) Consider the LinkedList class we discussed in class (see the slides for lecture 8). Add the following methods to the class and submit the completed LinkedList class. int size() which returns the size of the linked list Link getElementByIndex(int index) which returns the Link/node specified by the index. For example, getElementByIndex(0) should return the first Link, getElementByIndex(2) should return the third Link. If index is not in range, your method should return null. boolean hasDuplicate()...
This week we really want to learn about a file I/O in Java. In Java: 1)...
This week we really want to learn about a file I/O in Java. In Java: 1) Create a plain empty text file named contacts. 2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts. 3) Store that file in the same location as your project 4) Write a program that will find that file and load it into the computers memory. 5) Read each line of the file and print...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
1.Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from this list, followed by values from list1, and then followed by values from list2. For example, if E is Integer type and this list has (5,3,1), list1 has (8, 10,12,14), and list2 has (22,23,24,25,26) in that order, then the returned...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this...
This is a JAVA assignment and i dont have the SinglyLinkedList class   Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    1. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the...
Write Java code for extending the LinkedList<E> class of java.util.* to ExtLinkedList<E> that would include the following method: public ExtLinkedList <E> mergeThreeLists (ExtLinkedList<E> list1, ExtLinkedList<E> list2) { } that returns an ExtLinkedList<E> which is the merged version of values from thislist, followed by values from list1, and then followed by values from list2.    For example, if E is Integer type and this listhas (5,3,1), list1has (8, 10,12,14), and list2has (22,23,24,25,26) in that order, then the returned list from a call to...
For this Assignment: Write a 2- to 3-page critique of the article. In your critique, include...
For this Assignment: Write a 2- to 3-page critique of the article. In your critique, include responses to the following: Why did the authors use multiple regression? Do you think it’s the most appropriate choice? Why or why not? Did the authors display the data? Do the results stand alone? Why or why not?
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the...
Find a node in a LinkedList In Java: 1. (2 pts) Define the nodes in the LinkedList. 2. (2 pts) Create the LinkedList using the ListNode class. 3. (4 pts) Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. 4. (2 pts) Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT