In Java implement a class called Polynomials:
This class should store the polynomial using a linked list with nodes. (do not use existing list classes in Java).
This class should store only terms with non-zero coefficients.
This class should store the polynomial terms in decreasing order of their powers.
This class should have a constructor with no parameters that creates a polynomial with no terms, i.e. the polynomial 0.
This class should have another constructor that takes a polynomial as a string, parses it and creates the polynomial accordingly. The string contains the polynomial, with each term separated by a space. The following should work when tested:
"4x^3 +3x^1 -5"
"-3x^4 -2x^5 -5 +11x^1"
Do not write any other public methods other than those in the interface and the above two constructors.
Here is the code, well documented and commented:
filename: Polynomials.java
***************************************************************************************************
public class Polynomials{
//we create a node which will be an element of the
linked list
//the node will store the coefficient and power of the
non-zero term
private class node{
public int coeff;
public int power;
//we also need to store reference
to next node as this is a linked list
public node next;
//reference to next element in the list
node(int Coeff, int Power){
coeff =
Coeff;
power =
Power;
next =
null; //next element is null
}
}
//we need to create reference to first element of the
list
private node first;
public Polynomials(){
/*
* this is the empty
polynomial;
* i.e. polynomial with no
terms;
*/
first = null;
}
private int get_pow_coeff(String t, boolean f){
/*
* this function will extract the
power and coefficient in the term t
* boolean f tells whether we want
to extract power(f=true) or coeff.(f=false)
*/
int power;
int coeff;
//t must be of the form: (+/-/
)<coeff>x^<power>
//we check if t contains '+' or
'-'
int i1 = t.indexOf("+");
int i2 = t.indexOf("-");
if(i1 == -1 && i2 ==
-1){
//this means
that t is: <coeff>x^<power>
int i3 =
t.indexOf("x");
if(i3 ==
-1){ //this means that t is a constant term
coeff = Integer.valueOf(t);
power = 0;
}
else{
//we now extract coeff. and power based on
i3(index of "x")
coeff = Integer.valueOf(t.substring(0,
i3));
power =
Integer.valueOf(t.substring(i3+2)); //since i3+1
contains '^'
}
}
else{
//this means t
is either: +<coeff>x^<power> or
-<coeff>x^<power>
//we ignore the
case when '+' is present, however if '-' is present, we multiply
the coeff by -1
int i3 =
t.indexOf("x");
if(i3 ==
-1){ //this means that t is a constant term
coeff = Integer.valueOf(t.substring(1));
if(i2 != -1)
coeff = -1 * coeff;
power = 0;
}
else{
//we now extract coeff. and power based on
i3(index of "x")
coeff = Integer.valueOf(t.substring(1,
i3));
if(i2 != -1) //i.e. '-' is
present
coeff = -1 * coeff;
power =
Integer.valueOf(t.substring(i3+2)); //since i3+1
contains '^'
}
}
if(f) //power
extraction
return
power;
else
return
coeff;
}
public Polynomials(String poly){
/*
* this method parses the string,
poly and accordingly updates the linked list
* we assume that each term has a
different power
* we must store only the non-zero
terms and in decreasing order of their powers
* we use "+" as the connector
connecting the terms in the list.
* our strategy to parse the string
is to first split it into tokens using space(" ") as
delimiter
* we then insert these
tokens(terms) in the list using an insertion sort kind of
algorithm, i.e.
* we take each term and insert it
into its correct(according to its power) position in the
list.
* our search is easy as we always
maintain sorted(decreasing) order in the list
*/
String[] tokens = poly.split("
");
//we now insert the tokens in the
list using insertion sort
for(String t: tokens){
/*
* we now iterate
over the list searching for the correct position of term, t
* since we don't
have back references, it is important that we compare with the
neighbour of the current element.
* so that we can
insert the new element in between if required.
* we first check
if last is null and if so, we just insert t as the list is
empty.
* however if
list contains only one element, then its neighbour will be null and
hence we need to handle this case
* also when we
reach the last element in the list while searching, this means that
all elements in the list are bigger than t and hence its position
is last
* the last two
cases above can be covered by a single condition in which we
compare with the last element and accordingly insert.
*/
int power =
get_pow_coeff(t, true);
int coeff =
get_pow_coeff(t, false);
node temp =
first; //our iterator over the list reference
if(temp ==
null){ //i.e. no element present in the list
//now we just create a new node and insert t in
the list
node n = new node(coeff, power);
first = n;
}
else{
//now we iterate over the sorted list searching
for the correct position of t by comparing its power with other
powers
boolean flag = false;
while(temp.next != null){
int comp_power =
temp.next.power;
if(power >
comp_power){ //i.e. we need to insert t here
node n =
new node(coeff, power);
n.next =
temp.next; //we point t's next element to temp's
neighbour
temp.next
= n; //we point temp to n since it is now in b/w temp
and its old neighbour
flag =
true; //we update this flag to assert that we have
inserted t
break; //we exit the loop since we have inserted
t
}
else{ //we
continue searching further down the list
temp =
temp.next;
}
}
//now we insert t at the end if flag is
false
if(!flag){
int comp_power =
temp.power;
if(power >
comp_power){
node n =
new node(coeff, power);
n.next =
temp;
first =
n;
}
else{
node n =
new node(coeff, power);
n.next =
null;
temp.next
= n;
}
}
}
}
}
/*
* this function is not a part of the code
* but it is useful to test correctness of the
program
* basically it just displays the elements of the list
one by one in the order in which the list was filled
* , i.e. starting from the first and going till the
end
* delete this function when submitting
*/
public void display(){
node temp = first;
while(temp != null){
System.out.println("Term power:= " + temp.power);
System.out.println("Term coefficient:= " + temp.coeff);
System.out.println();
temp =
temp.next;
}
}
}
************************************************************************************************
Testing code: filename: Driver.java
public class Driver{
public static void main(String[] args){
String s = "-3x^4 -2x^5 -5 +11x^1";
//change this according to the string you want to test for
Polynomials p = new Polynomials(s);
//second constructor of polynomials
p.display();
}
}
*************************************************************************************************
Compiling(On ubuntu machines):
> javac Polynomials.java
> javac Driver.java
> java Driver
************************************************************************************************
Sample I/O:
String s = "-3x^4 -2x^5 -5 +11x^1" //as in Driver.java
Output:
Term power:= 5
Term coefficient:= -2
Term power:= 4
Term coefficient:= -3
Term power:= 1
Term coefficient:= 11
Term power:= 0
Term coefficient:= -5
----------------------------------------------------------------------------------------
String s = "4x^3 +3x^1 -5"
Output:
Term power:= 3
Term coefficient:= 4
Term power:= 1
Term coefficient:= 3
Term power:= 0
Term coefficient:= -5
-----------------------------------------------------------------------------------------
In Java implement a class called Polynomials: This class should store the polynomial using a linked...
Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...
Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...
each A polynomial may be represented as a linked list where each node contains the coefficient and exponent of a term of the polynomial. The polynomial 4X-3X-5 would be represented as the linked list. as the linked list the polnomial eql -5 0 Write a program system that reads two polynomials, stores them as linked lists, adds them together, and prints the result as a polynomial. The result should be a third linked list. Hint: Travers both polynomials. If a...
In C++: Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the term and creates the polynomial c x^n. (This constructor can be given default arguments easily to have a...
Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...
Linked List in Java The data node should be modeled somewhat like this: class node{ int node iNum; node next; } Write a program that creates a linked list and loads it with the numbers 0 to 9. Start with an empty list and then use a "for loop" to fill it. Create a linked list class, a node class, etc. Routines like makeNode and findTail should be methods in the linked list class. Create a showList function to display...
the language is java
if you coukd shiwnstep by step on how to get each
section
ebapps/blackboard/execute/content/file?cmd-view&content jid _975287 1&course id:_693311 Lab 3 Directions (linked lists) Program #1 1. Show PolynomialADT interface 2. Create the PolyNodeClass with the following methods: default constructor, . overloaded constructor, copy constructor, setCoefficient, setExponent, setNext, getCoefficient, getExponent, getNext 3. Create the PolynomialDataStrucClass with the following methods: defaut constructor, overloaded constructor, copy constructor, isEmpty. setFirstNode. getfirstNode,addPolyNodeFirst (PolyNode is created and set to the end of polynomial),...
117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...
In Java. Needing help with this homework
assignment.
The following is the class I need to write up and
implement.
two maps. P15.3 Write a class Polynomial that stores a polynomial such as f(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (5,10),(9,7),(-1,1),(?10,0) Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single...
The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic to its source code. (There is no inheritance or polymorphism taking place yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method. public Polynomial add(Polynomial...