Question: LANGUAGE: JAVA CREATE A PROGRAM THAT HAS SAME PURPOSE OF CODE BELOW: Source Code: public class TestPolynomial { public static void main(String[] args) { try
LANGUAGE: JAVA

CREATE A PROGRAM THAT HAS SAME PURPOSE OF CODE BELOW: Source Code:
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(); } } }-----------------------------------------------------------------------------------
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 /** * This constructor creates a LinkedList with no Terms */ public Polynomial() { terms = new LinkedList(); } /** * This method adds a Term to the polynomial such that the terms are arranged * following a decreasing order of coefficients. This method inserts a Term * in the polynomial at the appropriate location if the degree of the term is * not equal to a degree of an existing Term. If the degree of the Term is * equal to a degree of an existing Term, the coefficient of the existing * Term is updated by adding the coefficient of the Term being added. If the * updated coefficient equals zero, the Term is removed from the polynomial. */ public void addTerm(Term newTerm) throws Exception { int ctr; boolean found = false; Term currTerm = null; for (ctr = 0; ctr 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 -----------------------------------------------------------------------------------
/** * 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 */ 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 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). 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) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
