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

I am doing a program that takes 2 polynomials and add,sub them using 4 different

ID: 3696678 • Letter: I

Question

I am doing a program that takes 2 polynomials and add,sub them using 4 different methods. I have already done the first 2 but need help making 3 and 4. i posted the code on http://pastebin.com/zbMuFxHW 3rd one is to use a linked list of terms using pointers with a node class 4th one polynomials are linked lists in one static array. If you need me to email you the whole thing just leave ur email and i will. http://pastebin.com/zbMuFxHW Code is here. I posted the Demo, The 3rd class how much i have, the node class which needs a pointer.

Explanation / Answer

import java.io.*;
class Node
{
    public int exp,coeff;
    public Node next;
    public Node(int x,int y)
    {
        coeff=x;
        exp=y;
    }
}
class LinkList
{
    public Node first;
    public LinkList()
    {
        first=null;
    }
    public void insertFirst(int x,int y)
    {
        Node newNode=new Node(x,y);
        newNode.next=first;
        first=newNode;
    }
    public void insertPos(int x,int y,int p)
    {
        Node current=first;
        Node newNode=new Node(x,y);
        for(int i=p;i>1;i–)
            current=current.next;
        newNode.next=current.next;
        current.next=newNode;
    }
    public void insertLast(int x,int y)
    {
        Node newNode=new Node(x,y);
        newNode.next=null;
        if(isEmpty())
            first=newNode;
        else
        {
            Node current=first;
            while(current.next!=null)
                current=current.next;
            newNode.next=current.next;
            current.next=newNode;
        }
    }
    public boolean find(int key)
    {
        Node current=first;
        while(current!=null)
        {
            if(current.exp==key)
                return true;
            current=current.next;
        }
        return false;
    }
    public boolean isEmpty()
    {
        return(first==null);
    }
}
class Polynomial
{
    private LinkList l1;
    public Polynomial()
    {
        l1=new LinkList();
    }
    public boolean insert(int x,int y)
    {
        Node current=l1.first;
        int pos=0;
        while(current!=null)
        {
            if(current.exp==y)
            {
                System.out.println(“Not a valid term. Insert again”);
                return false;
            }
            else if(current.exp<y
                break;
            pos++;
            current=current.next;
        }
        if(pos==0)
            l1.insertFirst(x,y);
        else
            l1.insertPos(x,y,pos);
        return true;
    }
    public void displayPoly()
    {
        int f=0;
        Node current=l1.first;
        while(current!=null)
        {
            if(f!=0&&current.coeff>0&¤t.coeff!=-1
                System.out.print(“+”);
            if(current.coeff!=0)
            {
                if(current.coeff>1||current.coeff<-1||current.exp==0
                    System.out.print(current.coeff);
                else if(current.coeff==-1)
                    System.out.print(“-“);
                if(current.exp==1)
                    System.out.print(“X”);
                else if(current.exp>1||current.exp<0
                    System.out.print(“X^”+current.exp);
                f=1;
            }
            current=current.next;
        }
        System.out.println(“”);
    }
    public void add(Polynomial poly1,Polynomial poly2)
    {
        int x,y;
        Node current1=poly1.l1.first;
        Node current2=poly2.l1.first;
        while(current1!=null&&current2!=null)
        {
            if(current1.exp==current2.exp)
            {
                x=current1.coeff+current2.coeff;
                y=current1.exp;
                current1=current1.next;
                current2=current2.next;
            }
            else if(current1.exp>current2.exp)
            {
                x=current1.coeff;
                y=current1.exp;
                current1=current1.next;
            }
            else
            {
                x=current2.coeff;
                y=current2.exp;
                current2=current2.next;
            }
            l1.insertLast(x,y);
        }
        while(current1!=null)
        {
            x=current1.coeff;
            y=current1.exp;
            current1=current1.next;
            l1.insertLast(x,y);
        }
        while(current2!=null)
        {
            x=current2.coeff;
            y=current2.exp;
            current2=current2.next;
            l1.insertLast(x,y);
        }
    }
}
class PolynomialAdd
{
    public static void main(String args[])throws IOException
    {
        String ch=”y”;
        DataInputStream inp=new DataInputStream(System.in);
        int n,co,ex;
        while(ch.equals(“y”))
        {
            Polynomial p1=new Polynomial();
            Polynomial p2=new Polynomial();
            Polynomial p3=new Polynomial();
            System.out.println(“Enter the no: of terms of 1st polynomial”);
            n=Integer.parseInt(inp.readLine());
            while(n!=0)
            {
                System.out.println(“Enter the coefficent “);
                co=Integer.parseInt(inp.readLine());
                System.out.println(“Enter the exponent”);
                ex=Integer.parseInt(inp.readLine());
                if(p1.insert(co,ex))
                    n—;
            }
            System.out.println(“Enter the no: of terms of 2nd polynomial”);
            n=Integer.parseInt(inp.readLine());
            while(n!=0)
            {
                System.out.println(“Enter the coefficent “);
                co=Integer.parseInt(inp.readLine());
                System.out.println(“Enter the exponent”);
                ex=Integer.parseInt(inp.readLine());
                if(p2.insert(co,ex))
                    n—;
            }
            System.out.print(“1st Polynomial:- “);
            p1.displayPoly();
            System.out.print(“2nd Polynomial:- “);
            p2.displayPoly();
            p3.add(p1,p2);
            System.out.print(“Added Polynomial:- “);
            p3.displayPoly();
            System.out.print(“Enter y to continue “);
            ch=inp.readLine();
        }
    }
}