Reading a String Polynomial in Java and Adding Polynomials
-
October fifth, 2014,07:32 PM #1
Inferior Member
I'm having trouble calculation polynomials using a singly linked list I've looked everywhere online and can't notice the answer i'm looking for. I don't know how to import the scanner and loop information technology properly to have the user's inputs and plow them into the polynomials and i have a lot of trouble with the syntax of doing so... I don't know much Coffee at all, but this is the assignment and what i accept so far... god bless anyone willing to help!
My consignment is:You are asked to write a Java program that helps the user add and print polynomials of any
caste. If possible, students are encouraged to brand the programme help the user multiply two
polynomials of any degree.1. Polynomial Class
This course stores polynomials equally a linked list of terms. Each term contains a coefficient and a
ability of an unknown variable x. The implementations of linked lists in chapter 3 of the
textbook or the LinkedList class in the Java collections parcel can be in this assignment.
For example, the polynomial P(x) = 5x
10 + 9x
7
– ten – x can be stored as list of terms (5, x), (9,
vii), (-1, 1) and (-10, 0) in the linked list. This course should have one or more constructors and
methods to add, multiply, and print polynomials. For example, a polynomial P above tin can be
constructed as:Polynomial p = new Polynomial();
p.addTerm(-10, 0);
p.addTerm(-1, 1);
p.addTerm(9, vii);
p.addTerm(five, 10);For example, to multiply the polynomial P(x) by another polynomial Q(x), the following method
tin can be used:Polynomial r = p.multiply(q);
Of form, this multiply method tin can be subsequently both polynomials were instantiated. To add together the
polynomial P(ten) to another polynomial Q(10), the following method tin be used:Polynomial r = p.add(q);
To print the polynomial P(10) on the output console, the following method can be used:
p.impress();
In this consignment, the program should enquire the user for the polynomial(s) and the functioning to
utilise. You lot can parse the line input past the user or apply a menu-driven input. Students are complimentary to
choose whichever is easier to implement. For simplicity, the main method must be implemented
inside the Polynomial grade to make the plan 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 chemical element; individual Node<E> next; public Node(East e, Node<East> n) { element = e; next = n; } public E getElement( ) { return element; } public Node<E> getNext( ) { return side by side; } public void setNext(Node<Eastward> n) { next = n; } } private Node<East> head = naught ; private Node<E> tail = null ; private int size = 0 ; public PolynomialMath( ) { } public int size( ) { return size; } public boolean isEmpty( ) { render size == 0 ; } public E first( ) { if (isEmpty( ) ) return null ; return head.getElement ( ) ; } public Due east last( ) { if (isEmpty( ) ) return null ; render tail.getElement ( ) ; } public void addFirst(E e) { head = new Node<> (due east, head) ; if (size == 0 ) tail = head; size++; } public void addLast(E e) { Node<Due east> newest = new Node<> (due east, null ) ; if (isEmpty( ) ) head = newest; else tail.setNext (newest) ; tail = newest; size++; } public E removeFirst( ) { if (isEmpty( ) ) return null ; E respond = head.getElement ( ) ; head = head.getNext ( ) ; size--; if (size == 0 ) tail = nothing ; return answer; } public static void primary( String args[ ] ) { ArrayList l = new ArrayList ( ) ; System.out.println ( "Enter the input" ) ; Scanner input= new Scanner( System.in ) ; Cord a =input.nextLine ( ) ; l.add together (a) ; for ( int i = 0 ; i < l.size ( ) ; i++ ) { System.out.println (50.get (i) ) ; } System.out.println (50) ; } }
Last edited by onestepahead; October 5th, 2014 at 08:24 PM.
-
Related threads:
-
October 5th, 2014,08:01 PM #2
Re: I'm having trouble adding polynomials using a singly linked list Please edit your mail and wrap your code with code tags:
[lawmaking=java]
YOUR Lawmaking GOES Here
[/lawmaking]
to get highlighting and preserve formatting.If you don't empathise my respond, don't ignore it, enquire a question.
-
October fifth, 2014,08:nineteen PM #3
Junior Member
Re: I'g having trouble calculation polynomials using a singly linked list thank you for the tip, i'g very new to java and programming forums...
-
October 5th, 2014,08:30 PM #4
Re: I'thousand 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 cavalcade that should be indented.
Piece of work on that part first. Afterward add together code to put the data into the listing.scanner and loop it properly to take the user's inputs
When getting user input at that place needs to exist a way for the user to tell the program he's done entering data.
What technique has your instructor talked nearly?If you don't sympathise my answer, don't ignore it, ask a question.
-
October 5th, 2014,09:00 PM #five
Junior Member
Re: I'm having trouble adding polynomials using a singly linked list Thank you for your input, i capeesh your response! I am really struggling with this assignment because i cannot become past the office you just mentioned: user input and information gathering.
To answer your question: My professor has non gone over methods this circuitous for taking user inputs. This was supposed to be a competency test for linked lists and turned into something else in my instance.
-
October 5th, 2014,09:22 PM #six
Re: I'thousand having trouble adding polynomials using a singly linked list Two common techniques for ending user input:
the user tells the plan the number of entries and the program loops that number of times
the user enters a special/not-data value (picket) that the program looks for to stop the loopChose i of those and write a loop to get user input. Put the input into an arraylist for at present. When finished getting user input, impress out the contents of the arraylist to testify what was read and saved.
If yous don't empathise my respond, don't ignore it, inquire a question.
-
October eighth, 2014,12:xxx AM #7
Junior Member
Re: I'm having trouble adding polynomials using a singly linked list Alright! That's adept advice, give thanks you!
Source: https://www.javaprogrammingforums.com/object-oriented-programming/40046-im-having-trouble-adding-polynomials-using-singly-linked-list.html
0 Response to "Reading a String Polynomial in Java and Adding Polynomials"
Publicar un comentario