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

Please make a Java program. . works with linked chains of nodes . creates a comp

ID: 3916329 • Letter: P

Question

Please make a Java program.

. works with linked chains of nodes

. creates a component of mathematics solving software

Create a class, named Polynomial, and a class named Node. Each Polynomial object will be responsible for maintaining a chain of linked nodes. You can assume the polynomial is in standard, decreasing term form. For example, a polynomial will always be read from file in the correct order, and constructed as for example, 4x2 + 2x + 3 and not something like 2x + 3 + 4x2 . Each term (monomial) will be represented by a single Node object. For example, 4x2 would be one Node object, 2x is another, and 3 is another

Assume all polynomials represent a function of x, and no other mathematical variables. In other words, x will be the only character encountered representing a mathematical variable.

The Node class represents an individual term of a polynomial. You must at least contain data representing the Polynomial, and a link to the next Node object in the chain. The chain should be singly-linked, with each node pointing to the next node. To keep things simple, we assume all powers and coefficients are integers

The fields you must maintain will consist of:

• exponent, an integer

• coefficient, an integer

• nextNode, a reference to the next Node object

For polynomial class:

Polynomial() : No-argument constructor. Sets the polynomial’s linked chain head node to null .

Polynomial (String poly): The poly argument is a string representing a polynomial, in standard, decreasing term form, such as 4x^2+2x+3. The constructor is ultimately responsible for creating the individual nodes and chaining them appropriately.

Polynomial (Polynomial otherPoly) : A copy constructor. This constructor will perform a deep copy on the otherPoly, making an exact copy of it.

print() : The print method simply prints out the polynomial represented by the object. Given a Polynomial object, myPoly, the call to myPoly will simply print out the polynomial by walking down the linked chain. Assuming all printing is done to the console

add(Polynomial poly1, Polynomial poly2): This static method takes two Polynomial objects as arguments and returns a new Polynomial object, that is the sum of the other two. Remember that liketerms must be combined. Also, note that not all polynomial will be of the same degree

File and user input:

The polynomials will be constructed from one file, polynomials.txt. Each Polynomial object may be placed in an ArrayList or other data structure. The contents of all Polynomials should be listed to the screen, each on their own line immediately after they are done being constructed.

For example, if the file contains:

4x^2+2x+3

5x^3+15

2x^2+2x+5

The following is printed out to the screen based on an iteration through the ArrayList:

List of Polynomials:

0: 4x^2+2x+3

1: 5x^3+15

2: 2x^2+2x+5

Which do you wish to add? Press -1 to Exit.

The numbers in front of the polynomials are just the indices in the ArrayList at which each polynomial resides

The prompt asks the user which ones they wish to add, or if they want to exit. -1 causes an exit. Any other input will be in the form: int1 int2, where each is an integer.

If the user enters an invalid non-zero positive number (i.e., an index out of range), just print that the input was invalid, and prompt them again.

If the user enters two integers indicating the polynomials to add, your program should add them, creating another Polynomial object, and will automatically add it to the ArrayList, and print out all the polynomials again, prompting the user once again for their input.

For example, "before" the addition:

List of Polynomials:

0: 4x^2+2x+3

1: 5x^3+15

2: 2x^2+2x+5

Which do you wish to add? Press -1 to Exit.

0 2

This will cause polynomials at 0 and 2 to be added, and the screen should display the following:

List of Polynomials:

0: 4x^2+2x+3

1: 5x^3+15 2:

2x^2+2x+5 Which do you wish to add? Press -1 to Exit.

0 2

List of Polynomials:

0: 4x^2+2x+3

1: 5x^3+15

2: 2x^2+2x+5

3: 6x^2+4x+8

Which do you wish to add? Press -1 to Exit.

This will continue until the user exists with a -1. (Technically, you can make any negative number cause an exit, but you don’t need to tell the user that.)

Explanation / Answer

here is your code : -------------->>>>>>>>>>

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;


public class Polynomial{
private class Node{
  public int coef = 0;
  public int expo = 0;
  public Node next = null;

  public Node(){

  }

  public String toString(){
   String s = "";
   if(coef < 0){
    s = "("+coef+")";
   }else{
    s = coef+"";
   }
   if(expo == 1){
    s += "x";
   }else if(expo < 0){
    s += "x^("+expo+")";
   }else if(expo > 0){
    s += "x^"+expo;
   }
   return s;
  }
}

private Node head;

public Polynomial(){
  head = null;
}

public Polynomial(String poly){
  head = null;
  String[] str = poly.split("\+");
  for(int i = 0;i<str.length;i++){
   String[] data = str[i].split("[x]");
   Node n = new Node();
   n.coef = Integer.parseInt(data[0]);
   n.expo = 0;
   if(str[i].indexOf('x') != -1){
    n.expo = 1;
   }
   if(data.length == 2){
    data[1] = data[1].replace('^',' ');
    n.expo = Integer.parseInt(data[1].trim());
   }
   add(n);
  }
}

public Polynomial(Polynomial poly){
  head = null;
  Node cur = poly.getHead();
  while(cur != null){
   Node n = new Node();
   n.coef = cur.coef;
   n.expo = cur.expo;
   add(n);
  }
}

public void add(Node n){
  if(head == null){
   head = n;
   return;
  }
  Node cur = head;
  while(cur.next != null){
   cur = cur.next;
  }
  cur.next = n;
}

public void add(int c,int e){
  if(head == null){
   head = new Node();
   head.coef = c;
   head.expo = e;
   return;
  }
  Node cur = head;
  while(cur.next != null){
   cur = cur.next;
  }
  cur.next = new Node();
  cur.next.coef = c;
  cur.next.expo = e;
}

public void print(){
  Node cur = head;
  while(cur != null){
   System.out.print(cur);
   if(cur.next != null){
    System.out.print("+");
   }
   cur = cur.next;
  }
}

public static Polynomial add(Polynomial poly1,Polynomial poly2){
  Polynomial pol = new Polynomial();
  Node h1 = null;
  Node h2 = null;
  if(poly1 != null){
   h1 = poly1.getHead();
  }
  if(poly2 != null){
   h2 = poly2.getHead();
  }
  int coef = 0;
  int expo = 0;
  while(h1 != null && h2 != null){
   if(h1 == null){
    coef = h2.coef;
    expo = h2.expo;
    pol.add(coef,expo);
    h2 = h2.next;
   }else if(h2 == null){
    coef = h1.coef;
    expo = h1.expo;
    pol.add(coef,expo);
    h1 = h1.next;
   }else if(h1.expo > h2.expo){
    coef = h1.coef;
    expo = h1.expo;
    pol.add(coef,expo);
    h1 = h1.next;
   }else if(h1.expo < h2.expo){
    coef = h2.coef;
    expo = h2.expo;
    pol.add(coef,expo);
    h2 = h2.next;
   }else{
    coef = h2.coef + h1.coef;
    expo = h2.expo;
    pol.add(coef,expo);
    h1 = h1.next;
    h2 = h2.next;
   }
  }
  return pol;
}

public Node getHead(){
  return head;
}

public static void main(String[] args) {
  ArrayList<Polynomial> polList = new ArrayList<>();
  try{
   Scanner sc = new Scanner(new File("polynomials.txt"));
   while(sc.hasNextLine()){
    polList.add(new Polynomial(sc.nextLine()));
   }
   sc.close();
  }catch(Exception e){
   e.printStackTrace();
   System.exit(-1);
  }
  int input = 0;
  int input1;
  Scanner sc = new Scanner(System.in);
  while(true){
   System.out.println(" List Of All Polynomials : ");
   for(int i = 0;i<polList.size();i++){
    System.out.print(i+": ");
    polList.get(i).print();
    System.out.println();
   }
   System.out.println("Which do you wish to add? Press -1 to Exit");
   input = sc.nextInt();
   if(input == -1){
    break;
   }
   input1 = sc.nextInt();
   if(input >= 0 && input < polList.size() && input1 >= 0 && input1 < polList.size()){
    Polynomial p1 = Polynomial.add(polList.get(input),polList.get(input1));
    polList.get(input).print();
    System.out.print(" + ");
    polList.get(input1).print();
    System.out.print(" = ");
    p1.print();
   }else{
    System.out.println("Error on index of Polynomials. Try Again ? ");
   }
  }
}

}