Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.58 KB | None | 0 0
  1. /**
  2.  * This class represnts a Polynom object.
  3.  * @author (Eyal Oren)
  4.  * @version (2020a, 30.1.2020)
  5. **/
  6. public class Polynom
  7. {
  8.     private PolyNode _head; //Instane variable to point to the Polynom first Node.
  9.    
  10.     //Constructors:
  11.     /**
  12.      * Creates a new Polynom object.
  13.      */
  14.     public Polynom() //Deafult constructor, assigns the head of the polynom to be null.
  15.     {
  16.         _head = null;
  17.     }
  18.    
  19.     /**
  20.      * Creates a new Polynom object.
  21.      * @param p, the head of the Polynom to be set.
  22.      */
  23.     public Polynom(PolyNode p) //Constructor to assign a given PolyNode for Polynom head to point at.
  24.     {
  25.         _head = p;
  26.     }
  27.    
  28.     /**
  29.      * Returns the head of the Polynom.
  30.      * @return the head of the Polynom.
  31.      *
  32.      * Time Complexity O(1)
  33.      * Space Complexity O(1)
  34.      */
  35.     public PolyNode getHead() //Returns the head of the polynom.
  36.     {
  37.         return _head;
  38.     }
  39.    
  40.     /**
  41.      * Sets the head of the Polynom
  42.      * @param p, the head of the Polynom to be set.
  43.      *
  44.      * Time Complexity O(1)
  45.      * Space Complexity O(1)
  46.      */
  47.     public void setHead(PolyNode p) //Sets the head of the polynom to be the node given in the method.
  48.     {
  49.        _head = p;  
  50.     }
  51.    
  52.     /**
  53.      * Adds a new PolyNode to the Polynom, in the correct place.
  54.      * @param p, the PolyNode to insert.
  55.      * @return the Polynom after instering the new PolyNode.
  56.      *
  57.      * Time Complexity O(n)
  58.      * Space Complexity O(1)
  59.      */
  60.     public Polynom addNode (PolyNode p) //Adds a node to the polynom, keeping the polynom sorted by powers.
  61.     {
  62.         if (this._head == null) //If the polynom is empty of nodes, adding the given node to be the head of the polynom.
  63.         {
  64.             _head = p;
  65.             return this;
  66.         }
  67.        
  68.         if (p.getPower() > this._head.getPower()) //If the power of the given power is bigger than the head of the polynom, making it the new head.
  69.         {
  70.             p.setNext(_head);
  71.             _head = p;
  72.             return this;
  73.         }
  74.      
  75.         PolyNode h = _head; //Assigns a pointer to the head of the polynom.
  76.        
  77.         while (h != null) //Going through all the nodes in the polynom and checking the following terms:
  78.         {
  79.             if (h.getNext() == null) //If the loop reached the end of the polynom, instering the given node as the last node in the polynom.
  80.             {
  81.                 if (h.getPower() == p.getPower())        //If the powers are the same, adding the coefficients.
  82.                 {
  83.                     h.setCoefficient(h.getCoefficient() + p.getCoefficient());
  84.                 }
  85.                
  86.                 else
  87.                 {
  88.                     h.setNext(p);  //Otherwise, setting the node recieved in the method to be the last node in the Polynom.
  89.                 }
  90.                 return this;
  91.             }
  92.            
  93.             else if (p.getPower() == h.getPower()) //If the polynom contains a node with the same power, adding the given node power to the node that is already in.
  94.             {
  95.                 h.setCoefficient(h.getCoefficient() + p.getCoefficient());
  96.                 break; //Exiting the loop, considering the instertion has been completed.
  97.             }
  98.            
  99.             else if (p.getPower() < h.getPower() && p.getPower() > h.getNext().getPower()) //If the given node power is between two adjacent nodes, inserting it before it.
  100.             {
  101.                 PolyNode temp = h.getNext(); //Temp pointer to hold.
  102.                 h.setNext(p); //Making the given node the next node of the first node.
  103.                 p.setNext(temp); //Making the second node as the next of the given node.
  104.                 break;  //Exiting the loop, considering the instertion has been completed.
  105.             }
  106.            
  107.             else
  108.             {
  109.                 h = h.getNext(); //If none of the above terms happen, checking the next node to insert at.
  110.             }
  111.         }
  112.         return this;
  113.     }
  114.    
  115.     /**
  116.      * Multiplies every PolyNode in the Polynom with a given scalar.
  117.      * @param num, the scalar to multiply with.
  118.      * @return the Polynom after multiplication.
  119.      *
  120.      * Time Complexity O(n)
  121.      * Space Complexity O(1)
  122.      */
  123.     public Polynom multByScalar (int num) //Multiplies every PolyNode in the Polynom with a given scalar.
  124.     {
  125.         PolyNode p = _head; //Assigns a pointer to the head of the polynom.
  126.         while (p != null)   //Going through all the nodes in the polynom. and mutiplying their coefficient with the given scalar.
  127.         {
  128.             p.setCoefficient(p.getCoefficient() * num);
  129.             p = p.getNext();
  130.         }
  131.         return this;
  132.     }
  133.    
  134.     /**
  135.      * Adds the given Polynom in the method, to this Polynom.
  136.      * @param other, the Polynom to add to this Polynom.
  137.      * @return this Polynom after adding the given Polynom to it.
  138.      *
  139.      * Time Complexity O(n)
  140.      * Space Complexity O(n)
  141.      */
  142.     public Polynom addPol (Polynom other)
  143.     {
  144.         if (other._head == null) //If the polynom recived in the method is empty, returning this polynom.
  145.         {
  146.             return this;
  147.         }
  148.        
  149.         if (this._head == null) //If this polynom is empty, assigning this polynom to be the polynom recived and returning it.
  150.         {
  151.             this._head = new PolyNode(other._head);
  152.             return this;
  153.         }
  154.        
  155.         PolyNode h = other._head;      //Pointer to the other polynom head.
  156.         PolyNode origin = this._head;  //Pointer to this polynom head.
  157.        
  158.         if (h.getPower() > origin.getPower())   //If the power of the other head is bigger than the head of this head, inserting it as the new head.
  159.         {
  160.             PolyNode temp = new PolyNode(h);  //New node for the node to insert.
  161.             h = h.getNext();  
  162.             temp.setNext(origin);             //Inserting the specific node in the polynom that has been reciived, between the two nodes.
  163.             this._head = temp;
  164.             origin = this._head;
  165.         }
  166.          
  167.         while (h != null)                               //Going through every node in the other polynom
  168.         {
  169.            while (origin != null)                       //Checking in this polynom, where to enter the specific node of the other polynom.
  170.            {
  171.                if (origin.getNext() == null)            //If we have reached to the end of this polynom:
  172.                {
  173.                    PolyNode temp = h;
  174.                    h = h.getNext();
  175.                    if (origin.getPower() == temp.getPower()) //If the powers are equal, adding the coefficients.
  176.                    {
  177.                        origin.setCoefficient(origin.getCoefficient() + temp.getCoefficient());
  178.                        origin.setNext(temp.getNext());
  179.                    }
  180.                    else
  181.                    {
  182.                        origin.setNext(temp); //Otherwise, setting the specific node to be the last node in the Polynom at the moment.
  183.                    }
  184.                    break;
  185.                }
  186.                
  187.                else if (h.getPower() < origin.getPower() && h.getPower() > origin.getNext().getPower()) //If the power of the other node is just between two powers of this polynom, inserting it between them.
  188.                {
  189.                   PolyNode temp = new PolyNode(h); //New node for the node to insert.  
  190.                   temp.setNext(origin.getNext());
  191.                   origin.setNext(temp);
  192.                   h = h.getNext();
  193.                }
  194.                
  195.                else if (h.getPower() == origin.getPower())  //If the other node power is equal to this power, adding it's power to this power.
  196.                {
  197.                    origin.setCoefficient(origin.getCoefficient() + h.getCoefficient());
  198.                    h = h.getNext();
  199.                }
  200.                
  201.                
  202.                else
  203.                {
  204.                    origin = origin.getNext(); //If none of the above reached, moving the origin pointer one node further.
  205.                }
  206.            }
  207.         }
  208.         return this;
  209.     }
  210.     /**
  211.      * Multiplies the given Polynom with this Polynom.
  212.      * @param other, the Polynom to multiply this Polynom with.
  213.      * @return this Polynom after multiplying it with the given Polynom.
  214.      */
  215.     public Polynom multPol (Polynom other)
  216.     {
  217.         if (other == null || other._head == null) //If the given Polynom or it's head are null, returning this Polynom.
  218.         {
  219.             return this;
  220.         }
  221.        
  222.         if (this._head == null) //If this Polynom doesnt point on any vaild nodes, returning this Polynom.
  223.         {
  224.             return this;
  225.         }
  226.        
  227.         Polynom p = new Polynom();     //Creating new Polynom to hold the multiplied nodes.
  228.        
  229.         //Assigning pointers to each Polynom head: ש
  230.         PolyNode origin = this._head;  
  231.         PolyNode r = other._head;
  232.        
  233.         while (origin != null) //Going through all the nodes in this Polynom
  234.         {
  235.             while (r != null) //GOing through all the nodes in the given Polynom, for each Node in this polynom.
  236.             {
  237.                 PolyNode toAdd = new PolyNode(origin.getPower() + r.getPower(), origin.getCoefficient() * r.getCoefficient()); //Creating a new Node, which is the product of multiplying 2 nodes.
  238.                 p.addNode(toAdd); //Adding the node to the temporary Polynom, with the method to sort the nodes.
  239.                 r = r.getNext();
  240.             }
  241.             if (r == null) //If the 2nd Polynom has reached it's end, pointing it back to the head. to multiply the next origin node with this Polynom nodes.
  242.             {
  243.                 r = other._head;
  244.             }
  245.             origin = origin.getNext(); //After the iteration, moving the origin node to be the next one.
  246.         }
  247.         this.setHead(p._head);  //Setting this Polynom head to be the temporary Polynom head.
  248.         return this;
  249.     }
  250.    
  251.     /**
  252.      * Returns the differential Polynom of this Polynom.
  253.      * @return the differential Polynom of this Polynom.
  254.      *
  255.      *  Time Complexity O(n)
  256.      *  Space Complexity O(1)
  257.      */
  258.     public Polynom differential() //Returns the differential Polynom of this Polynom.
  259.     {
  260.        PolyNode p = _head; //Assigns a pointer to the head of this Polynom.
  261.        
  262.        while (p != null) //Going through each node in the Polyonm, to differante it.
  263.        {
  264.            if (p.getPower() == 0) //If the power of a node is 0, setting it's coefficient to be 0.
  265.            {
  266.                p.setCoefficient(0);
  267.                p = p.getNext();
  268.            }
  269.            
  270.            else if (p.getPower() == 1) //If the power of a node is 1, setting it's power to be 0.
  271.            {
  272.                p.setPower(0);
  273.                p = p.getNext();
  274.            }
  275.            
  276.            else //Otherise using the basic differential formula.
  277.            {
  278.                p.setCoefficient(p.getCoefficient() * p.getPower());
  279.                p.setPower(p.getPower() - 1);
  280.                p = p.getNext();
  281.            }  
  282.        }
  283.        return this;
  284.     }
  285.     /**
  286.      * Returns a String that represents this Polynom.
  287.      * @return a String that represnts this Polynom.
  288.      *
  289.      * Time Complexity O(n)
  290.      * Space Complexity O(1)
  291.      */
  292.     public String toString()
  293.     {
  294.         if (_head == null)
  295.         {
  296.             return ""; //If the Polynom is empty, returning an empty string.
  297.         }
  298.        
  299.         String str = "";        
  300.         boolean isFirst = true;
  301.         PolyNode p = _head;
  302.         while (p != null)             //Going through all the nodes in the Polyonm.
  303.         {
  304.             if (p.getCoefficient() != 0) //Adding a node only if it's coefficient is not zero.
  305.             {
  306.                 if (isFirst)  //If it's the first node, won't add a '+' sign.
  307.                 {
  308.                     str += p;
  309.                     isFirst = false; //The first node has been added.
  310.                 }
  311.                
  312.                 else
  313.                 {
  314.                     if (p.getCoefficient() > 0)  //If it's not the first node.
  315.                     {
  316.                         str += "+" + p;
  317.                     }
  318.                     else
  319.                     {
  320.                         str += p;
  321.                     }
  322.                 }
  323.             }
  324.             p = p.getNext();
  325.         }
  326.         return str;
  327.     }
  328. //End of class Polynom.
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement