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

Java: Using a Node class that forms the components of a linked list and a class

ID: 3670578 • Letter: J

Question

Java: Using a Node class that forms the components of a linked list and a class that has a data field of Orderlet type and a next field of Node type.

Implement the void addAll(Order order) method to add the contents of the parameter order to the current order. You should add the content of the argument order to the end of the linked list of the current order.

public class Order {
   private Node start, end;
   public Order() {
       start = end = null;
   }
   public Order(Order that) {
       this();
       addAll(that);
   }
   public String toString() {
       String ret = "[";
      
       Node current = start;
       while(current != null) {
           ret += current.getData();
           if (current.getNext() != null) ret += ", ";
           current = current.getNext();
       }
       return ret + "]";
   }

   public void add(int number, int width, int height) {
       argumentPositivityCheck(number, width, height);
      
       add(new Orderlet(number, new PictureWindow(width, height)));
   }
   public void add(int number, Window window) {
       argumentPositivityCheck(number);
       argumentNotNullCheck(window);
      
       add(new Orderlet(number, window));
       manynodes++;
   }
   void add(Orderlet orderlet) {
       Node n = new Node(orderlet, null);
       if (start==null)
           start=end=n;
       else{
           end.setNext(n);
           end=n;
       }
   }
  
   public void addAll(Order order){
      
   }

public class Main {
   public static void main(String[] args) {

       Window w1 = new PictureWindow(40, 40),
               w2 = new PictureWindow(36, 54),
               w3 = new PictureWindow(30, 60);

       Order order1 = new Order();
       order1.add(10, w1);
       order1.add(20, w2);
       order1.add(5, w3);
       System.out.println("order1");
       print(order1);
      
       Order order2 = new Order();
       order2.add(5, w1);
       order2.add(10, w3);
       System.out.println("order2");
       print(order2);
      
       order1.addAll(order2);
       System.out.println("order1 + order2");
       print(order1);
   }

public class Node {
   private Orderlet data;
   private Node next;
  
   public Node(Orderlet data, Node next) {
       this.data = data; this.next = next;
   }
   public Orderlet getData() { return data; }
   public Node getNext() { return next; }
   public void setData(Orderlet data) { this.data = data; }
   public void setNext(Node next) { this.next = next; }
}


class Orderlet {
   final int number;
   final Window window;
  
   Orderlet(int number, Window window) {
       this.number = number;
       this.window = window;
   }
   public String toString() { return number + " " + window; }
}

Explanation / Answer

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication28;

/**

*

* @author HARE KRISHNA

*/

public class Node {

        private Orderlet orderlet;

        private Node link;

        private Node head;

        private Object orderCurrent;

        public Node(){

            link =null;

       }

        public Node(Orderlet orderlet,Node link){

            orderlet = orderlet;

            link = link;

        }

    private Node(Order order) {

        throw new UnsupportedOperationException("Not yet implemented");

    }

    public Orderlet getOrderlet() {

        return orderlet;

    }

    public void setOrderlet(Orderlet orderlet) {

        this.orderlet = orderlet;

    }

    public Node getLink() {

        return link;

    }

    public void setLink(Node link) {

        this.link = link;

    }

    // appends the specified element to the end of this list.

    public void addAll(Order order){

        // Initialize Node only incase of 1st element

        if(head==null){

            head = new Node(order);

        Node orderTemp = new Node(order);

        Node orederCurrent = head;

        if(orderCurrent!=null){

            // starting at the head node, crawl to the end of the list and then add element after last node

                while (orderCurrent.getNext() != null) {

            orderCurrent = orderCurrent.getNext();

                }

        // the last node's "next" reference set to our new node

                orderCurrent.setNext(orderTemp);

}          }

        } }

    

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