Problem description: 1. Below are classes for a project that involves the creation, displaying and evaluation of a polynomial in x. Organize the classes into a project and test the program. 2. Based on the project, create a project for the same purpose such that coefficients of polynomial may be numbers with decimal digits and that the literal coefficient in each term must be consistent with the other terms (i.e. A term can be added in an existing polynomial only if the literal is the same as the literal of the existing terms). /** * The following defines a term of an algebraic polynomial that involves only one * literal. 3x^2 is an example of a term where 3 is the coefficient, x is the * literal and 2 is the degree */ public class Term { private int coef; // data member to hold coefficient of a term private int degree; // data member to hold the degree of a term private char literal; // data member to hold the literal of a term /** * This a constructor that sets coefficient to 0, degree to 0 and literal to * x. */ public Term() { coef = 0; degree = 0; literal = 'x'; } /** * This is a constructor that sets values of the coefficient, literal and * degree to the integer coef, character literal and integer degree specified * during instantiation */ public Term(int coef, char literal, int degree) { this.coef = coef; this.literal = literal; this.degree = degree; } /** * This method sets the value of the coefficent to the specified integer coef */ public void setCoef(int coef) { this.coef = coef; } /** * This method sets the value of the literal to the specified character * literal */ public void setLiteral(char literal) { this.literal = literal; } /** * This method sets the value of the degree to the specified integer degree */ public void setDegree(int degree) { this.degree = degree; } /** * This method returns the coefficient of the term */ public int getCoef() { return this.coef; } /** * This method returns the literal of the term */ public char getLiteral() { return this.literal; } /** * This method returns the degreeof the term */ public int getDegree() { return this.degree; } /** * This method formats the term into a string of the form 3x^2 */ public String toString() { return (coef + literal + "^" + degree); } } // end of Term class import java.util.LinkedList; // access the LinkedList class from package util /** * This class defines a polynomial as a linked list of terms. */ public class Polynomial { private LinkedList terms; // data member to reference a LinkedList // representing a polynomial public Polynomial() { terms = new LinkedList(); } tr; boolean found = false; Term currTerm = null; for (ctr = 0; ctr < terms.size(); ctr++) { // size method of LinkedList // is used currTerm = terms.get(ctr); // get method of LinkedList is used. if (currTerm.getDegree() <= newTerm.getDegree()) { found = true; break; } } if (!found) { terms.add(newTerm);// add method of LinkedList is used. } else { if (currTerm.getDegree() < newTerm.getDegree()) { terms.add(ctr, newTerm); // alternative add method of LinkedList // is used } else { currTerm.setCoef(currTerm.getCoef() + newTerm.getCoef()); if (currTerm.getCoef() == 0) { terms.remove(ctr); // remove method of the LinkList class // is used } } } } /** * This method formats a polynomial into a string of the following * sample form: 3x^2 - 5X + 3 */ public String toString() { String strResult = ""; for (int ctr = 0; ctr < terms.size(); ctr++) { Term currTerm = (Term) terms.get(ctr); if (currTerm.getCoef() > 0) { if (ctr != 0) { strResult = strResult + "+"; } } else { strResult = strResult + "-"; } if (currTerm.getCoef() != 1 || currTerm.getDegree() == 0) { strResult = strResult + Math.abs(currTerm.getCoef()); } switch (currTerm.getDegree()) { case 0: break; case 1: strResult = strResult + "x"; break; default: strResult = strResult + "x^" + currTerm.getDegree(); } } return strResult; } /** * This method evaluates the value of the polynomial if its literal is * substituted by the specified integer value. */ public double evaluate(double value) throws Exception { double sum = 0; for (int ctr = 0; ctr < terms.size(); ctr++) { Term currTerm = terms.get(ctr); sum += currTerm.getCoef() * Math.pow(value, currTerm.getDegree()); } return sum; } } import java.io.*; public class TestPolynomial { public static void main(String[] args) { try { Polynomial p = new Polynomial(); p.addTerm(new Term(4, 'x', 3)); p.addTerm(new Term(-3, 'x', 1)); p.addTerm(new Term(1, 'x', 0)); p.addTerm(new Term(5, 'x', 2)); p.addTerm(new Term(1, 'x', 5)); System.out.println(p.toString()); System.out.println(p.evaluate(2)); } catch (Exception e) { e.printStackTrace(); } // end of catch block } // end of main } // end of class

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Problem description:
1. Below are classes for a project that involves the creation, displaying and
evaluation of a polynomial in x. Organize the classes into a project and test the
program.
2. Based on the project, create a project for the same purpose such that
coefficients of polynomial may be numbers with decimal digits and that the
literal coefficient in each term must be consistent with the other terms (i.e. A term can be added in an existing polynomial only if the literal is the same as
the literal of the existing terms).
/**
* The following defines a term of an algebraic polynomial that involves only one
* literal. 3x^2 is an example of a term where 3 is the coefficient, x is the
* literal and 2 is the degree
*/
public class Term {
private int coef; // data member to hold coefficient of a term
private int degree; // data member to hold the degree of a term
private char literal; // data member to hold the literal of a term
/**
* This a constructor that sets coefficient to 0, degree to 0 and literal to
* x.

*/
public Term() {
coef = 0;
degree = 0;
literal = 'x';
}
/**
* This is a constructor that sets values of the coefficient, literal and
* degree to the integer coef, character literal and integer degree specified
* during instantiation
*/
public Term(int coef, char literal, int degree) {
this.coef = coef;
this.literal = literal;
this.degree = degree;
}
/**
* This method sets the value of the coefficent to the specified integer coef
*/
public void setCoef(int coef) {
this.coef = coef;
}
/**
* This method sets the value of the literal to the specified character
* literal
*/
public void setLiteral(char literal) {
this.literal = literal;
}
/**
* This method sets the value of the degree to the specified integer degree
*/
public void setDegree(int degree) {
this.degree = degree;
}
/**
* This method returns the coefficient of the term
*/
public int getCoef() {
return this.coef;
}
/**
* This method returns the literal of the term
*/
public char getLiteral() {
return this.literal;
}

/**
* This method returns the degreeof the term
*/
public int getDegree() {
return this.degree;
}
/**
* This method formats the term into a string of the form 3x^2
*/
public String toString() {
return (coef + literal + "^" + degree);
}
} // end of Term class
import java.util.LinkedList; // access the LinkedList class from package util
/**
* This class defines a polynomial as a linked list of terms.
*/
public class Polynomial {
private LinkedList<Term> terms; // data member to reference a LinkedList
// representing a polynomial

public Polynomial() {
terms = new LinkedList<Term>();
}
tr;
boolean found = false;
Term currTerm = null;
for (ctr = 0; ctr < terms.size(); ctr++) { // size method of LinkedList
// is used
currTerm = terms.get(ctr); // get method of LinkedList is used.
if (currTerm.getDegree() <= newTerm.getDegree()) {
found = true;
break;
}

}
if (!found) {
terms.add(newTerm);// add method of LinkedList is used.
} else {
if (currTerm.getDegree() < newTerm.getDegree()) {
terms.add(ctr, newTerm); // alternative add method of LinkedList
// is used
} else {
currTerm.setCoef(currTerm.getCoef() + newTerm.getCoef());
if (currTerm.getCoef() == 0) {
terms.remove(ctr); // remove method of the LinkList class
// is used
}
}
}
}
/**
* This method formats a polynomial into a string of the following
* sample form: 3x^2 - 5X + 3
*/
public String toString() {
String strResult = "";
for (int ctr = 0; ctr < terms.size(); ctr++) {
Term currTerm = (Term) terms.get(ctr);
if (currTerm.getCoef() > 0) {
if (ctr != 0) {
strResult = strResult + "+";
}
} else {
strResult = strResult + "-";
}
if (currTerm.getCoef() != 1 || currTerm.getDegree() == 0) {
strResult = strResult + Math.abs(currTerm.getCoef());
}
switch (currTerm.getDegree()) {
case 0:
break;
case 1:
strResult = strResult + "x";

break;
default:
strResult = strResult + "x^" + currTerm.getDegree();
}
}
return strResult;
}
/**
* This method evaluates the value of the polynomial if its literal is
* substituted by the specified integer value.
*/
public double evaluate(double value) throws Exception {
double sum = 0;

for (int ctr = 0; ctr < terms.size(); ctr++) {
Term currTerm = terms.get(ctr);
sum += currTerm.getCoef() * Math.pow(value, currTerm.getDegree());
}
return sum;
}
}
import java.io.*;
public class TestPolynomial {
public static void main(String[] args) {
try {
Polynomial p = new Polynomial();
p.addTerm(new Term(4, 'x', 3));
p.addTerm(new Term(-3, 'x', 1));
p.addTerm(new Term(1, 'x', 0));
p.addTerm(new Term(5, 'x', 2));
p.addTerm(new Term(1, 'x', 5));
System.out.println(p.toString());
System.out.println(p.evaluate(2));
} catch (Exception e) {
e.printStackTrace();
} // end of catch block
} // end of main
} // end of class

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY