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

Implement the Sequence class from Section 4.5. Outline of Java code for this cla

ID: 3816018 • Letter: I

Question

Implement the Sequence class from Section 4.5. Outline of Java code for this class is available in DoubleLinkedSeq.java. Your sequence class must have five private instance variables as described in Section 4.5. You need to use DoubleNode.java for your node class. Follow instructions from Section 4.5, except that you do not need to put your class in a package. You need to write the invariant for your sequence ADT (invariant is explained on p.126). You need to add implementations instead of blank implementations for the following: constructor DoubleLinkedSeq( ) addAfter addBefore addAll advance clone concatenation getCurrent isCurrent removeCurrent size start In addition to that you need to implement the following methods: addAtFront - a method to add a new element at the front of the sequence removeAtFront - a method to remove the element at the front of the sequence toIth - a method that makes the ith element become the current element (assume that the front element is the 1st) toString - a method that represents the sequence as a string (in any reasonable format which shows the order of elements) reverse – a method that returns a new sequence that contains the same elements as the original sequence but in reverse order. The original sequence should remain unchanged. For instance, if original sequence contains elements 1.2 - 3.5 - 4.3 - 6.44 in this order, then the new sequence should contain elements 6.44 - 4.3 - 3.5 - 1.2. The header of the method should be the following: public DoubleLinkedSeq reverse( ) everyOther – a method that returns a new sequence that contains every other element of the original sequence. The original sequence should remain unchanged. For instance, if original sequence contains elements 1.2 - 3.5 - 4.3 - 6.44, then the new sequence should contain elements 1.2 - 4.3. If the original sequence contains elements 3.2 - 2.5 - 1.0 - 7.1 - 9.5, then the method should return sequence 3.2 - 1.0 - 9.5. The header of the method should be the following: public DoubleLinkedSeq everyOther( ) removeSmaller – a method that removes from the sequence all elements that are less than a given value which is passed as a parameter. For instance, if original sequence contains elements 5.2 - 3.5 - 4.3 - 6.44 – 2.5 and the value is 5.0, then the sequence should become 5.2 - 6.44. If all elements in the sequence are less than the value, then the sequence should become empty. The header of the method should be the following: public void removeSmaller(double value) 2: Write a test program to test your code. Name your test program TestSequence. Your test program should test all the methods in your DoubleLinkedSeq class.

I need help with the method/testing of reverse, everyOther, concatenation, removeSmaller, advance, and toIth.

Thanks!

Explanation / Answer


public class TestSequence {

   public static void main(String[] args) {
       DoubleLinkedSeq s = new DoubleLinkedSeq();
       s.addFront(new DoubleNode(1.2));
       s.addFront(new DoubleNode(2.2));
       s.addFront(new DoubleNode(3.2));
       s.addFront(new DoubleNode(4.2));
       s.addFront(new DoubleNode(5.2));
       System.out.println("Secuence : "+s);
       DoubleLinkedSeq s2 = s.revers();
       System.out.println("Reverse Secuence : "+s2);
      
   }
}
class DoubleNode{
   double data;
   DoubleNode next;
   public DoubleNode() {
       data = 0.0d;
       next = null;
   }
   public DoubleNode(double data) {
       this.data = data;
       next = null;
   }
}
class DoubleLinkedSeq{
   int currentIndex;
   DoubleNode curentNode;
   int length;
   DoubleNode head;
   public DoubleLinkedSeq() {
       currentIndex =0;
       length = 0;
   }
   public boolean isCurrent(){
       if(currentIndex >=0){
           return true;
       }
       return false;
   }
   public DoubleNode getCurrent(){
       if(isCurrent())
           return curentNode;
       return null;
   }
   public int size(){
       return length;
   }
   public void advance(){
       if(currentIndex<size()-1 || curentNode == null){
           currentIndex++;
           curentNode = curentNode.next;
       }
   }
   public void addFront(DoubleNode n){
       if(head==null){
           head = n;
           currentIndex++;
           curentNode = head;
       }
       else{
           n.next = head;
           head = n;
       }
       length++;
   }
   public DoubleNode removeFront( ){
       if(head==null){
           return null;
       }
       else{
           DoubleNode n = head;
           head = head.next;
           length--;
           if(size()==0){
               currentIndex =-1;
               curentNode = null;
           }
           return n;
       }
   }
   public DoubleNode removeCurrent(){
       DoubleNode n= null;
       DoubleNode pre =head;
       DoubleNode cur = head;
       while(cur!=getCurrent()){
           pre = cur;
           cur = cur.next;
       }
       pre = cur.next;
       curentNode = curentNode.next;
       if(cur==head)
           head = cur.next;
       length--;
       return n;
   }
   public void toIth(int index){
       if(index < size()){
           currentIndex = -1;
           curentNode = head;
           while(currentIndex < index-1){
               curentNode = curentNode.next;
               currentIndex++;
           }
       }
   }
   public void addAfter(int index,DoubleNode n){
       if(index<size()){
           DoubleNode temp = head;
           int i=0;
           while(i<index-1){
               temp = temp.next;
           }
           n.next = temp.next;
           temp.next = n;
           length++;
       }
       else{
           System.out.println("Can not add");
       }
   }
   public void addBefore(int index,DoubleNode n){
       if(index<size()){
           DoubleNode temp = head;
           int i=0;
           while(i<index-2){
               temp = temp.next;
           }
           n.next = temp.next;
           temp.next = n;
           length++;
       }
       else{
           System.out.println("Can not add");
       }
   }
   public void addLast(DoubleNode n){
       DoubleNode temp = head;
       if(head==null)
       {
           head = n;
       }
       while(temp.next != null){
           temp = temp.next;
       }
       temp.next = n;
       length++;
   }
   public DoubleLinkedSeq clone(){
       DoubleLinkedSeq newSeq = new DoubleLinkedSeq();
       DoubleNode temp = head;
       while(temp!=null){
           newSeq.addLast(temp);
       }
       return newSeq;
   }
   public DoubleLinkedSeq revers(){
       DoubleLinkedSeq newSeq = new DoubleLinkedSeq();
       DoubleNode temp = head;
       while(temp!=null){
           newSeq.addFront(new DoubleNode(temp.data));
           temp = temp.next;
          
       }
       return newSeq;
   }
   public void start(){
       currentIndex =0;
       curentNode = head;
   }
   public void removeSmaller(double value){
       start();
       while(isCurrent()){
           DoubleNode node = getCurrent();
           if(node.data < value){
               removeCurrent();
           }
           else{
               advance();
           }
       }
   }
   public String toString(){
       String str ="[ ";
       DoubleNode temp = head;
       while(temp!= null){
           str = str+temp.data+" ";
           temp =temp.next;
       }
       return str+" ]";
   }
}