Hello everyone!!! I need to implement methods for addition, subtraction, multipl
ID: 3872878 • Letter: H
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 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:
class DNode<T> {
private T data;
private DNode<T> prev, next;
public DNode(T d, DNode<T> p, DNode<T> n) {
data = d;
next = n;
prev = p;
}
public T getData() {
return data;
}
public DNode<T> getNext() {
return next;
}
public DNode<T> getPrev() {
return prev;
}
public void setData(T d) {
data = d;
}
public void setNext(DNode<T> n) {
next = n;
}
public void setPrev(DNode<T> p) {
prev = p;
}
}
class DList<T> {
private DNode<T> header, trailer;
private int size;
public DList() {
size = 0;
header = new DNode<T>(null, null, null);
trailer = new DNode<T>(null, header, null);
header.setNext(trailer);
}
// utility methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
// give clients access to nodes, but not to the header or trailer
public DNode<T> getFirst() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return header.getNext();
}
public DNode<T> getLast() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return trailer.getPrev();
}
public DNode<T> getNext(DNode<T> v) throwsException {
DNode<T> ans = v.getNext();
if (ans == null || ans == trailer)
throw new Exception("No such node");
return ans;
}
public DNode<T> getPrev(DNode<T> v) throwsException {
DNode<T> ans = v.getPrev();
if (ans == null || ans == header)
throw new Exception("No such node");
return ans;
}
// methods to change the list
public void addBefore(T d, DNode<T> v) {
DNode<T> u = v.getPrev();
DNode<T> x = new DNode<T>(d, u, v);
u.setNext(x);
v.setPrev(x);
size++;
}
public void addAfter(T d, DNode<T> v) {
DNode<T> w = v.getNext();
DNode<T> x = new DNode<T>(d, v, w);
v.setNext(x);
w.setPrev(x);
size++;
}
public void addFirst(T d) {
addAfter(d, header);
}
public void addLast(T d) {
addBefore(d, trailer);
}
public T remove(DNode<T> v) throws Exception {
if (v == header || v == trailer)
throw new Exception("Sentinel");
DNode<T> u = v.getPrev();
DNode<T> w = v.getNext();
w.setPrev(u);
u.setNext(w);
size--;
return v.getData();
}
// LinkedList testing methods:
public String toString() {
String ans = "";
DNode<T> n = header;
ans += "(H)<-->";
do {
n = n.getNext();
if (n == trailer)
ans += "(T)";
else
ans += (n.getData() + "<-->");
} while (n != trailer);
return ans;
}
}
class Term {
Double coefficient;
int degree;
public Term() {
this(null, 0);
}
public boolean isPositive() {
return coefficient > 0;
}
public Term(Double coefficient, int degree) {
this.coefficient = coefficient;
this.degree = degree;
}
public Double getCoefficient() {
return coefficient;
}
public void setCoefficient(Double coefficient) {
this.coefficient = coefficient;
}
public int getDegree() {
return degree;
}
public void setDegree(int degree) {
this.degree = degree;
}
public String toString() {
String ans = "";
if (coefficient.doubleValue() == 0) return "";
if (degree == 0) return coefficient.toString();
if (coefficient != 1) {
if (coefficient == -1) ans += "- ";
else ans += coefficient + " ";
}
ans = ans + "X";
if (degree == 1) return ans;
return ans + "^" + degree;
}
}
abstract class Polynomial {
DList<Term> data = null;
public Polynomial() {
data = new DList<>();
}
public final String toString() {
String ans = "";
boolean starting = true;
try {
DNode<Term> 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";
}
return ans;
}
abstract public Polynomial add(Polynomial p);
abstract public Polynomial subtract(Polynomial p);
abstract public Polynomial multiply(Polynomial p);
abstract public Polynomial divide(Polynomial p) throws Exception;
abstract public Polynomial remainder(Polynomial p) throws Exception;
}
class Utility {
public static void run(Polynomial p, Polynomial q) throws Exception {
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));
}
}
public class X00000000 extends Polynomial {
public static void main(String args[]) throwsException {
Polynomial p = new X00000000(" X^5"),
q = new X00000000("X^2 - X + 1");
Utility.run(p, q);
}
public X00000000(String s) {
//parse string character by character
double coef = 0; //coefficient of term
int deg = 0; //degree of term
String[] terms = s.split(" ");
for(int i = 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));
}
else if (term.length() == 1)
{
deg=1;
if(prevTerm.equals("-"))
coef=-1;
else
coef=1;
}
}
else if(term.startsWith("-") || term.startsWith("+"))
continue;
else if(term.length()==1 && Character.isDigit(term.charAt(0)))
{
coef=Integer.parseInt(term);
if(prevTerm.equals("-"))
coef*=-1;
deg=0;
}
Term T = new Term(coef, deg);
if(data.isEmpty())
{
data.addFirst(T);
}
else
{
data.addLast(T);
}
}
}
public X00000000() {
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;
}
public Polynomial remainder(Polynomial p) throws Exception {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
}
Explanation / Answer
Answer:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
class node1
{
int cf;
int pw;
node1 nxt;
node1(int cf1,int pw1)
{
cf=cf1;
pw=pw1;
nxt=null;
}
void show()
{
System.out.println("cf="+cf+" pw="+pw);
}
}
class linklist
{
node1 fst=null;
void enter(int cf1,int pw1)
{
node1 node11=new node1(cf1,pw1);
node1 current;
current=fst;
if(current==null)
fst=node11;
else
{
node11.nxt=fst;
fst=node11;
}
}
void show()
{
node1 current;
current=fst;
while(current!=null)
{
current.show();
current=current.nxt;
}
}
}
public class Polynomial {
public static void main(String[] args)
{
DataInputStream sc=new DataInputStream(System.in);
int c1,i1,a1,n1;
node1 current1,current2;
linklist list1=new linklist();
linklist list2=new linklist();
linklist list3=new linklist();
linklist list4=new linklist();
try
{
System.out.println("Enter the degree of ploynomial:");
n1=Integer.parseInt(get.readLine());
System.out.println(" This is first polynomial:");
for(i1=0;i1<=n1;i1++)
{
System.out.println("Enter coeff. of term of power "+i1);
c1=Integer.parseInt(get.readLine());
list1.enter(c1,i1);
}
System.out.println("second polynomial:");
for(i1=0;i1<=n1;i1++)
{
System.out.println("Enter coeff. of term of power "+i1);
c1=Integer.parseInt(get.readLine());
list2.enter(c1,i1);
}
current1=list1.fst;
current2=list2.fst;
while((current1!=null)&&(current2!=null))
{
a1=current1.cf+current2.cf;
int p1=current1.pw;
list3.enter(a1,p1);
current1=current1.nxt;
current2=current2.nxt;
}
System.out.println("Polynomial after addition");
list3.show();
while((current1!=null)&&(current2!=null))
{
a1=current1.cf-current2.cf;
int p1=current1.pw;
list4.enter(a1,p1);
current1=current1.nxt;
current2=current2.nxt;
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("Polynomial after Subtraction");
list4.show();
}
}
program for add and sub of polynomial.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.