Question
Please make a Java program. . works with linked chains of nodes . creates a component of mathematics solving software Create a class, named Polynomial,
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 polynomials 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 dont need to tell the user that.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started