Java Program: here is the link for the program that we want to extend http://www
ID: 662710 • Letter: J
Question
Java Program:
here is the link for the program that we want to extend
http://www.chegg.com/homework-help/questions-and-answers/write-program-represent-polynomial-linked-list-node-integer-degree-double-coefficient-refe-q7634737
Explanation / Answer
public class Polynomial {
private Node first;
private Node last;
private static class Node {
int exponent;
double coefficient;
Node next;
Node(double c, int e) {
this.coefficient = c;
this.exponent = e;
}
}
public Polynomial()
{
}
public Polynomial(double c, int e) {
first = new Node(c, e);
last = first;
}
public void addNewTerm(double c, int e)
{
last.next = new Node(c, e);
last = last.next;
}
//add
public Polynomial add(Polynomial poly)
{
Polynomial a = this;
Polynomial c = new Polynomial();
Node x = a.first;
Node y = poly.first;
while (x != null || y != null) {
Node t = null;
if (x == null) { t = new Node(y.coefficient, y.exponent); y = y.next; }
else if (y == null) { t = new Node(x.coefficient, x.exponent); x = x.next; }
else if (x.exponent < y.exponent) { t = new Node(x.coefficient, x.exponent); x = x.next; }
else if (x.exponent > y.exponent) { t = new Node(y.coefficient, y.exponent); y = y.next; }
else {
double coef = x.coefficient + y.coefficient;
int exp = x.exponent;
x = x.next;
y = y.next;
if (coef == 0) continue;
t = new Node(coef, exp);
}
if(c.first == null)
{
c.first = c.last = t;
continue;
}
c.last.next = t;
c.last = c.last.next;
}
return c;
}
// multiply
public Polynomial multiply(double num) {
Polynomial a = this;
Polynomial temp = new Polynomial();
for (Node x = a.first; x!= null; x = x.next) {
if(temp.first == null)
{
temp.last =temp.first= new Node(x.coefficient * num, x.exponent);
continue;
}
temp.last.next = new Node(x.coefficient * num, x.exponent);
temp.last = temp.last.next;
}
return temp;
}
//evaluate
public double evaluate(double value)
{
Polynomial a = this;
double val = 0;
for (Node x = a.first; x!= null; x = x.next) {
val += x.coefficient * (Math.pow(value, x.exponent));
}
return val;
}
//comapare
public boolean equals(Polynomial poly)
{
Polynomial a = this;
Node x = a.first;
Node y = poly.first;
while (x != null || y != null) {
Node t = null;
if (x == null) { return false; }
else if (y == null) { return false; }
else if (x.exponent < y.exponent) { return false; }
else if (x.exponent > y.exponent) { return false; }
else {
x = x.next;
y = y.next;
continue;
}
}
return true;
}
// convert to string representation
public String toString() {
String s = "";
for (Node x = first; x != null; x = x.next) {
if (x.coefficient > 0) s = s + " + " + x.coefficient + (x.exponent == 0 ? "" : (x.exponent == 1 ? "x" : "x^" + x.exponent));
else if (x.coefficient < 0) s = s + " - " + (-x.coefficient) + (x.exponent == 0 ? "" : (x.exponent == 1 ? "x" : "x^" + x.exponent));
}
return s;
}
public static void main(String[] args) {
Polynomial p1 = new Polynomial(1.3, 0);
p1.addNewTerm(2.1, 1);
p1.addNewTerm(1, 3);
p1.addNewTerm(2.3, 4);
System.out.println("First polynomial: " + p1);
Polynomial p2 = new Polynomial(-1.3, 0);
p2.addNewTerm(0.3, 1);
p2.addNewTerm(1, 2);
p2.addNewTerm(-2.3, 4);
System.out.println("Second polynomial: " + p2);
System.out.println("Add polynomials: " + p1.add(p2));
System.out.println("Multiply first by 0.2: " + p1.multiply(0.2));
System.out.println("evaluate first at x = 1.5: " + p1.evaluate(1.5));
System.out.println("comapare first with second: " + p1.equals(p2));
System.out.println("comapare first with first: " + p1.equals(p1));
}
}
--------------------------------------------------------------------------------
OUTPUT
--------------------------------------------------------------------------------
First polynomial:
+ 1.3 + 2.1x + 1.0x^3 + 2.3x^4
Second polynomial:
- 1.3 + 0.3x + 1.0x^2 - 2.3x^4
Add polynomials:
+ 2.4x + 1.0x^2 + 1.0x^3
Multiply first by 0.2:
+ 0.26 + 0.42000000000000004x + 0.2x^3 + 0.45999999999999996x^4
evaluate first at x = 1.5:
19.46875
comapare first with second:
false
comapare first with first:
true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.