Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 prev, next;

publicDNode(T d, DNode p, DNode n) {

data = d;

next = n;

prev = p;

}

publicT getData() {

returndata;

}

publicDNode getNext() {

returnnext;

}

publicDNode getPrev() {

returnprev;

}

publicvoidsetData(T d) {

data = d;

}

publicvoidsetNext(DNode n) {

next = n;

}

publicvoidsetPrev(DNode p) {

prev = p;

}

}

classDList {

privateDNode header, trailer;

privateintsize;

publicDList() {

size = 0;

header =newDNode(null,null,null);

trailer =newDNode(null, header,null);

header.setNext(trailer);

}

// utility methods

publicintsize() {

returnsize;

}

publicbooleanisEmpty() {

returnsize == 0;

}

// give clients access to nodes, but not to the header or trailer

publicDNode getFirst()throwsException {

if(isEmpty())

thrownewException("Empty");

returnheader.getNext();

}

publicDNode getLast()throwsException {

if(isEmpty())

thrownewException("Empty");

returntrailer.getPrev();

}

publicDNode getNext(DNode v)throwsException {

DNode ans = v.getNext();

if(ans ==null|| ans == trailer)

thrownewException("No such node");

returnans;

}

publicDNode getPrev(DNode v)throwsException {

DNode ans = v.getPrev();

if(ans ==null|| ans == header)

thrownewException("No such node");

returnans;

}

// methods to change the list

publicvoidaddBefore(T d, DNode v) {

DNode u = v.getPrev();

DNode x =newDNode(d, u, v);

u.setNext(x);

v.setPrev(x);

size++;

}

publicvoidaddAfter(T d, DNode v) {

DNode w = v.getNext();

DNode x =newDNode(d, v, w);

v.setNext(x);

w.setPrev(x);

size++;

}

publicvoidaddFirst(T d) {

addAfter(d, header);

}

publicvoidaddLast(T d) {

addBefore(d, trailer);

}

publicT remove(DNode v)throwsException {

if(v == header || v == trailer)

thrownewException("Sentinel");

DNode u = v.getPrev();

DNode w = v.getNext();

w.setPrev(u);

u.setNext(w);

size--;

returnv.getData();

}

// LinkedList testing methods:

publicString toString() {

String ans = "";

DNode n = header;

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 data =null;

publicPolynomial() {

data =newDList<>();

}

publicfinalString toString() {

String ans = "";

booleanstarting =true;

try{

DNode n = data.getFirst();

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

Students also viewed these Databases questions