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

Write java codes for the following LinkedList base program: A one-variable polyn

ID: 3564830 • Letter: W

Question

Write java codes for the following LinkedList base program:

A one-variable polynomial of degree n is an arithmetic expression of the form:

a0+a1 x+a2 x^2+..........+an x^n

where a0 a1,a2, ......, an    are the coefficients. Design and implement a class named Polynomial that uses a singly-linked list to store the coefficients (so there is virtually no limit on n ). Include a method that adds two polynomials and another method that multiplies two polynomials. For example, the following two polynomials

2 + x^2 - 3x^3 and 1 - x - 2x^2 + 3x^3

are represented by 2,   0,   1, -3 and 1, -1, -2, 3, respectively. The sum of these two polynomials is

3 - x - x^2

which is represented by 3, -1, -1; and the product of the two polynomials is

2 - 2x - 3x^2 + 2x^3 + x^4 + 9x^5 - 9x^6

which is represented by 2, -2,   -3,   2,   1,   9,   -9.

Note that you must write your code to maintain and manipulate the linked list data structure (i. e., do not use a utility library such as java.util.LinkedList). Email one week before the due date to get a data file to test your program. The data file will contain several pairs of polynomials. Each polynomial is given on a separate line (with coefficients separated by space instead of comma), and there is an empty line between two pairs. Your Java program should read and echo each pair of polynomials, compute and report their sum and product, and go on to process the next pair until the end of input. Specifically, the main method must look like:

while (not end of input) {

                read and echo a pair of polynomials P1 and P2;

            output sum (P1, P2);     // Static method returning a polynomial

            output product (P1, P2)   // Static method returning a polynomial

}

Hand in a program listing with clear internal documentation and a printout of the results of execution using the test data file. Also email all JAVA program files (Node.java - the same as the one given in class except for the element field that becomes int type for this project, Polynoial.java, Project1.java)   so your work can be easily recompiled and tested.

Data file will be like:

2 3 0 1 -3

1 -1 -2 3, 9

Output will be in same formate with sum and product:

2 3 0 1 -3

1 -1 -2 3, 9

sum: ...

product:...

...........

......etc

Explanation / Answer

import java.lang.*;
import java.util.*;

public class Node
{
   Node data;
   node next;
}

public class SinglyLinkeList
{
   Node start;
   int size;
}

public SinnglyLinkedList()
{
start=null;
size=0;
}

public void add(Node data)
{
   if(size=0)
   {
      start=new Node();
      start.next=null;
      start.data=data;
   }
   else
   {
      Node currentnode=getnode(size-1);
      Node newnode=new Node();
      newnode.data=data;
      newnode.next=null;
      currentnode.next=newnode;
   }
   size++;
}

public void insertfront(Node data)
   {
     if(size==0)
    {
      Node newnode=new Node();
      start.next=null;
      start.data=data;
    }
    else       
    {
        Node newnode=new Node();
         newnode.data=data;
        newnode.next=start;
   }
   size++;
}

   public void insertAt(int position,Node data)
   {
      if(position==0)
      {
        insertatfront(Node data);
       }
       else if(position==size-1)
      {
        insertatlast(data);
      }
       else
      {
         Node tempnode=getNodeAt(position-1);
         Node newnode= new Node();
         newnode.data=data;
         newnode.next=tempnode.next;
         size++;
      }
   }

public Node getFirst()
{
    return getNodeAt(0);
}
public Node getLast()
{
     return getNodeAt(size-1);
}
public Node removeAtFirst()
{
     if(size==0)
     {
        System.out.println("Empty List ");
     }
     else
     {
         Node tempnode=getNodeAt(position-1);
         Node data=tempnode.next.data;
         tempnode.next=tempnode.next.next;
         size--;
           return data;
     }
}

      Node data=start.data;
      start=start.next;
      size--;
      return data;
}
}
public Node removeAtLast()
{
   if(size==0)
   {
      System.out.println("Empty List ");
   }
   else
   {
       Node data=getNodeAt(size-1);
       Node data=tempnode.next.data;
       size--;
       return data;
   }
}

   public string void main(string[] args)
{

    LinkedList l1=new LinkedList();
    BufferReader bf=new BufferReader(new InputStreamReader(Sysyem.in));
    System.out.println("1->Add Element 2->Remove Last 3->Insert Front 4->Insert at        position 5->REmove Front 6-> Remove At Last 7->Exit ");
string choice;
choice.readline();
int choiceNum= Integer.parseInt(choice);
string str;
switch(choiceNum)
{
    case 1: System.out.println("Enter the element to be inserted ");
        str =bf.readline();
        l1.add(str);
        break;

    case 2: System.out.println("Linked List before removing element : "+l1);
        System.out.println("Linked List after removing element : ");
        l1.removeLast();
        break;

    case 3: System.out.println("Linked List before inserting element at first: "+l1);
        System.out.println("Enter the element to be inserted at first: ");
        str.readline();
        l1.insertfront(str);
        System.out.println("Linked List after inserting at first : "+l1);
        break;

    case 4: System.out.println("Linked List before inserting element at particular position: "+l1);
        System.out.println("Enter the element position & element : ");
        str.readline();
        string str1;
        str1.readline();
        l1.insertAt(str,str1);
        System.out.println("Linked List after inserting at particular Position :"+l1);
        break;

    case 5: System.out.println("Linked LIst before removing front element: "+l1);
        System.out.println("Linked List after removing front element : ");
        l1.removeAtFirst();
        break;

    case 6: System.out.println("Linked List before removing last element: "+l1);
        System.out.println("Linked List after removing last element :");
        l1.removeAtLast();

    default:break;
    }
   }
   }

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