Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## I'm having trouble adding polynomials using a singly linked list

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();

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:

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> 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;
}

public E last() {
if (isEmpty()) return null;
return tail.getElement();
}

if (size == 0)
size++;
}

Node<E> newest = new Node<>(e, null);
if (isEmpty())
else
size++;
}

public E removeFirst() {
if (isEmpty()) return null;
size--;
if (size == 0)
tail = null;
}

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();

for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}
System.out.println(l);

}
}```  Reply With Quote

3. ## Re: I'm having trouble adding polynomials using a singly linked list

[code=java]
[/code]
to get highlighting and preserve formatting.  Reply With Quote

4. ## Re: I'm having trouble adding polynomials using a singly linked list

thank you for the tip, i'm very new to java and programming forums...  Reply With Quote

5. ## Re: I'm having trouble adding polynomials using a singly linked list

The code has lost its formatting in places. There are many statements starting in the first column that should be indented.
scanner and loop it properly to take the user's inputs
Work on that part first. Later add code to put the data into the list.
When getting user input there needs to be a way for the user to tell the program he's done entering data.  Reply With Quote

6. ## Re: I'm having trouble adding polynomials using a singly linked list

Thank you for your input, i appreciate your response! I am really struggling with this assignment because i cannot get past the part you just mentioned: user input and data gathering.

To answer your question: My professor has not gone over methods this complex for taking user inputs. This was supposed to be a competency test for linked lists and turned into something else in my case.  Reply With Quote

7. ## Re: I'm having trouble adding polynomials using a singly linked list

Two common techniques for ending user input:
the user tells the program the number of entries and the program loops that number of times
the user enters a special/non-data value (sentinel) that the program looks for to end the loop

Chose one of those and write a loop to get user input. Put the input into an arraylist for now. When finished getting user input, print out the contents of the arraylist to show what was read and saved.  Reply With Quote

8. ## Re: I'm having trouble adding polynomials using a singly linked list

Alright! That's good advice, thank you!   Reply With Quote 