Question
Hello everyone!!! I need to implement methods for addition, subtraction, multiplication, division, and reminder of two polynomials that have been stored in a Java doubly
Hello everyone!!!
I need to implement methods for addition, subtraction, multiplication, division, and reminder of two polynomials that have been stored in a Java doubly linked list. The polynomials data has been entered as two objects of the type string. I already created the constructor to convert the string data into digits. I can only work based on the given code. I have to work only on the methods in order to achieve the correct output. I'm running out of time. Please, any help would be appreciated, thanks...
Current code:
classDNode
privateT data;
privateDNode
publicDNode(T d, DNode
data = d;
next = n;
prev = p;
}
publicT getData() {
returndata;
}
publicDNode
returnnext;
}
publicDNode
returnprev;
}
publicvoidsetData(T d) {
data = d;
}
publicvoidsetNext(DNode
next = n;
}
publicvoidsetPrev(DNode
prev = p;
}
}
classDList
privateDNode
privateintsize;
publicDList() {
size = 0;
header =newDNode
trailer =newDNode
header.setNext(trailer);
}
// utility methods
publicintsize() {
returnsize;
}
publicbooleanisEmpty() {
returnsize == 0;
}
// give clients access to nodes, but not to the header or trailer
publicDNode
if(isEmpty())
thrownewException("Empty");
returnheader.getNext();
}
publicDNode
if(isEmpty())
thrownewException("Empty");
returntrailer.getPrev();
}
publicDNode
DNode
if(ans ==null|| ans == trailer)
thrownewException("No such node");
returnans;
}
publicDNode
DNode
if(ans ==null|| ans == header)
thrownewException("No such node");
returnans;
}
// methods to change the list
publicvoidaddBefore(T d, DNode
DNode
DNode
u.setNext(x);
v.setPrev(x);
size++;
}
publicvoidaddAfter(T d, DNode
DNode
DNode
v.setNext(x);
w.setPrev(x);
size++;
}
publicvoidaddFirst(T d) {
addAfter(d, header);
}
publicvoidaddLast(T d) {
addBefore(d, trailer);
}
publicT remove(DNode
if(v == header || v == trailer)
thrownewException("Sentinel");
DNode
DNode
w.setPrev(u);
u.setNext(w);
size--;
returnv.getData();
}
// LinkedList testing methods:
publicString toString() {
String ans = "";
DNode
ans += "(H)<-->";
do{
n = n.getNext();
if(n == trailer)
ans += "(T)";
else
ans += (n.getData() + "<-->");
}while(n != trailer);
returnans;
}
}
classTerm {
Double coefficient;
intdegree;
publicTerm() {
this(null, 0);
}
publicbooleanisPositive() {
returncoefficient > 0;
}
publicTerm(Double coefficient,intdegree) {
this.coefficient = coefficient;
this.degree = degree;
}
publicDouble getCoefficient() {
returncoefficient;
}
publicvoidsetCoefficient(Double coefficient) {
this.coefficient = coefficient;
}
publicintgetDegree() {
returndegree;
}
publicvoidsetDegree(intdegree) {
this.degree = degree;
}
publicString toString() {
String ans = "";
if(coefficient.doubleValue() == 0)return"";
if(degree == 0)returncoefficient.toString();
if(coefficient != 1) {
if(coefficient == -1) ans += "- ";
elseans += coefficient + " ";
}
ans = ans + "X";
if(degree == 1)returnans;
returnans + "^" + degree;
}
}
abstractclassPolynomial {
DList
publicPolynomial() {
data =newDList<>();
}
publicfinalString toString() {
String ans = "";
booleanstarting =true;
try{
DNode
while(n !=null) {
if(!starting && n.getData().isPositive()) ans += " +";
starting =false;
ans += " " + n.getData().toString();
n = data.getNext(n);
}
}catch(Exception e) {
if(starting)return"0";
}
returnans;
}
abstractpublicPolynomial add(Polynomial p);
abstractpublicPolynomial subtract(Polynomial p);
abstractpublicPolynomial multiply(Polynomial p);
abstractpublicPolynomial divide(Polynomial p)throwsException;
abstractpublicPolynomial remainder(Polynomial p)throwsException;
}
classUtility {
publicstaticvoidrun(Polynomial p, Polynomial q)throwsException {
System.out.println("Polynomials p = " + p + " q = " + q);
System.out.println("Sum " + p.add(q));
System.out.println("Difference " + p.subtract(q));
System.out.println("Product " + p.multiply(q));
System.out.println("Quotient " + p.divide(q));
System.out.println("Remainder " + p.remainder(q));
}
}
publicclassX00000000extendsPolynomial {
publicstaticvoidmain(String args[])throwsException {
Polynomial p =newX00000000(" X^5"),
q =newX00000000("X^2 - X + 1");
Utility.run(p, q);
}
publicX00000000(String s) {
//parse string character by character
doublecoef = 0; //coefficient of term
intdeg = 0; //degree of term
String[] terms = s.split(" ");
for(inti = 0; i < terms.length; i++)
{
String term= terms[i];
//System.out.println("term:"+term);
String prevTerm="";
if(i != 0)
prevTerm=terms[i-1];
if(term.startsWith("X"))
{
if(term.length()==3 && term.contains("^"))
{
coef=1;
deg=Integer.parseInt(term.substring(2));
}
elseif(term.length() == 1)
{
deg=1;
if(prevTerm.equals("-"))
coef=-1;
else
coef=1;
}
}
elseif(term.startsWith("-") || term.startsWith("+"))
continue;
elseif(term.length()==1 && Character.isDigit(term.charAt(0)))
{
coef=Integer.parseInt(term);
if(prevTerm.equals("-"))
coef*=-1;
deg=0;
}
Term T =newTerm(coef, deg);
if(data.isEmpty())
{
data.addFirst(T);
}
else
{
data.addLast(T);
}
}
}
publicX00000000() {
super();}
public Polynomial add(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial subtract(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial multiply(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial divide(Polynomial p) throws Exception {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
publicPolynomial remainder(Polynomial p) throws Exception {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
}
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