I've looked everywhere online and can't find the answer i'm looking for. I don't know how to import the scanner and loop it properly to take the user's inputs and turn them into the polynomials and i have a lot of trouble with the syntax of doing so... I don't know much Java at all, but this is the assignment and what i have so far... god bless anyone willing to help!
My assignment is:
You are asked to write a Java program that helps the user add and print polynomials of any
degree. If possible, students are encouraged to make the program help the user multiply two
polynomials of any degree.
1. Polynomial Class
This class stores polynomials as a linked list of terms. Each term contains a coefficient and a
power of an unknown variable x. The implementations of linked lists in chapter 3 of the
textbook or the LinkedList class in the Java collections package can be in this assignment.
For example, the polynomial P(x) = 5x
10 + 9x
7
– x – 10 can be stored as list of terms (5, 10), (9,
7), (-1, 1) and (-10, 0) in the linked list. This class should have one or more constructors and
methods to add, multiply, and print polynomials. For example, a polynomial P above can be
constructed as:
Polynomial p = new Polynomial();
p.addTerm(-10, 0);
p.addTerm(-1, 1);
p.addTerm(9, 7);
p.addTerm(5, 10);
For example, to multiply the polynomial P(x) by another polynomial Q(x), the following method
can be used:
Polynomial r = p.multiply(q);
Of course, this multiply method can be after both polynomials were instantiated. To add the
polynomial P(x) to another polynomial Q(x), the following method can be used:
Polynomial r = p.add(q);
To print the polynomial P(x) on the output console, the following method can be used:
p.print();
In this assignment, the program should ask the user for the polynomial(s) and the operation to
apply. You can parse the line input by the user or use a menu-driven input. Students are free to
choose whichever is easier to implement. For simplicity, the main method must be implemented
inside the Polynomial class to make the program consist of only a single file.
What i have so far is:
import java.util.ArrayList;
import java.util.Scanner;
public class PolynomialMath<E> {
public static class Node <E> {
private E element;
private Node<E> next;
public Node(E e, Node<E> n){
element = e;
next = n;
}
public E getElement() { return element; }
public Node<E> getNext() { return next; }
public void setNext(Node<E> n) { next = n; }
}
private Node<E> head = null;
private Node<E> tail = null;
private int size = 0;
public PolynomialMath() { }
public int size() { return size; }
public boolean isEmpty() {return size == 0;}
public E first() {
if (isEmpty()) return null;
return head.getElement();
}
public E last() {
if (isEmpty()) return null;
return tail.getElement();
}
public void addFirst(E e) {
head = new Node<>(e, head);
if (size == 0)
tail = head;
size++;
}
public void addLast(E e) {
Node<E> newest = new Node<>(e, null);
if (isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size++;
}
public E removeFirst() {
if (isEmpty()) return null;
E answer = head.getElement();
head = head.getNext();
size--;
if (size == 0)
tail = null;
return answer;
}
public static void main(String args[]){
ArrayList l = new ArrayList();
System.out.println("Enter the input");
Scanner input=new Scanner(System.in);
String a =input.nextLine();
l.add(a);
for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
System.out.println(l);
}
}