Question

Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute....

Java

Create a class named OrderedList. It will use a LinkedList of integers as its attribute. The constructor will simply create an empty list. This will be a different LinkedList, as all of the items will be in ascending numerical order. That means items will not necessarily be placed at the end of the list, but placed where it should be located. Note that the iterator simply uses the next() method, so if you use the iterator to find the location, you may need to keep track of the current index yourself. The following are the methods to create for this class:

1. add(value) : Add the specified integer value to its appropriate place on the list

2. remove(value) : Remove the specified value from the list

3. list() : This method uses the iterator to print the contents of the list. It should appear on the same line, separated by commas. For instance: 1, 3, 4, 6, 8 Note that the last item will NOT have a comma following it.

4. maxgap() : This method will return the largest difference between items on the list. For instance, in the sample list displayed in #3 above, the largest gap is 2 (between 4 and 6 and between 6 and 8)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.*;

class OrderedList {

    private LinkedList<Integer> list;
    public OrderedList(){
        list = new LinkedList<Integer>();
    }
    public void add(int a){
       for (int i = 0; i<list.size(); i++){
           if (list.get(i) > a){
              list.add(i,a);
              return;
           }
       }
       list.add(a);
     }
    public void remove(int a){
         list.remove((Object)a);
    }
    public int maxgap(){
        int max = 0;
        for (int i = 0; i<list.size()-1; i++){
           if ((list.get(i+1) - list.get(i)) >max)
              max = list.get(i+1) - list.get(i);
        }     
        return max;
    }
    public void list(){
       Iterator it = list.iterator();
       int count = 0;
       while(it.hasNext()){
           if (count < list.size() -1)
               System.out.print(it.next() + ",");
           else
               System.out.print(it.next());
           count++;
       }
       System.out.println();
      
    }

}

public class Demo207{

   public static void main(String[] args){
        OrderedList list = new OrderedList();
        list.add(8);
        list.add(1);
        list.add(3);
        list.add(4);
        list.list();
        System.out.println(list.maxgap());
        list.remove(8);
        list.list();
   }
}

Add a comment
Know the answer?
Add Answer to:
Java Create a class named OrderedList. It will use a LinkedList of integers as its attribute....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public...

    Create a nested inner class inside a file called LinkedList.java called the ListIterator. ListIterator: This public class is nested within an LinkedList data structure and allows us to traverse the elements in any collection, access any data element and remove any data elements of the collection. It also adds the functionality to move forward and backward, which is new functionality. We are going to build this data structure from scratch. Instance variables (fields) are: mPrevious (Node) – the previous node...

  • 1. Create a class MLL, a singly linked, non-circular list where each node only has one...

    1. Create a class MLL, a singly linked, non-circular list where each node only has one link next and the list has a head and a tail link (think about implementation of node). The MLL class also implements the Iterable interface. The following should be implemented as well: 2. Add the methods to the MLL class: a. iterator() to implement the Iterable interface. This method returns an instance of MLLI initialized appropriately. b. addFirst( value) that adds a value to...

  • In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList)...

    In NetBeans Create a new Java Application to manage Linked Lists: (Note: Do not use java.util.LinkedList) a) Create a DateTime class 1. Add day, month, year, hours, minutes as attributes 2. Add a constructor and a toString() method 3. Implement the Comparable interface, and add a CompareTo() method 4. Add methods to get and set all attributes. b) Add to MyLinkedList class the following methods: 1. Insert a Node to a particular position in the List 2. Insert a Node...

  • JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList...

    JAVA you have been given the code for Node Class (that holds Strings) and the LinkedList Class (some methods included). Remember, you will use the LinkedList Class that we developed in class not Java’s LinkedList Class. You will add the following method to the LinkedList Class: printEvenNodes – this is a void method that prints Nodes that have even indices (e.g., 0, 2, 4, etc). Create a LinkedListDemo class. Use a Scanner Class to read in city names and store...

  • Please code this in java: The purpose of this assignment is to: Read double data in...

    Please code this in java: The purpose of this assignment is to: Read double data in to a TreeSet where each datum inserted is Math.abs(datum) (i.e., no negative values are inserted). NOTE: This will (i) sort the data placed into the TreeSet, and, (ii) remove duplicate values since no duplicates are allowed (i.e., duplicates will not be inserted). Create a list iterator. Iterate backwards through the TreeSet (i.e., from the largest value to the smallest) outputting all elements * -1.0....

  • Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test...

    Complete P16.1 and P16.4 (Class Name: NewMethodDemo) Once complete, upload all .java files. For primary test class: Remember your header with name, date, and assignment. Also include class names that will be tested. Psuedocode (level 0 or mixture of level 0 and algorithm [do not number steps]) is required if main() contains more than simple statements (for example, your program includes constructs for decisions (if/else), loops, and methods. For Secondary class(es): Include a JavaDoc comment that describes the purpose of...

  • java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then...

    java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> {    LinkedList<T> list;       public Stack() {        list = new LinkedList<T>();    }       public boolean isEmpty() {        return list.isEmpty();   ...

  • if we have following List classes: public class LinkedList<T> { class ListNode { protected T value;...

    if we have following List classes: public class LinkedList<T> { class ListNode { protected T value; protected ListNode next; public ListNode(T val, ListNode nxt) { value = val; next = nxt; } public ListNode(T val) { this(val, null); } public ListNode() { this(null, null); } } can you write the folowing methods in java: 1.Write a method for the LinkedList class called int indexOf(T val) which returns the integer index indicating the location of val in the list, or -1...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT