Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here are the codes to be worked on: Please add the codes to where it says- 'to do: your code here' Polynomial import java.util.Map; import

image text in transcribed

image text in transcribed

Here are the codes to be worked on:

Please add the codes to where it says- 'to do: your code here'

Polynomial

import java.util.Map; import java.util.TreeMap;

/** A class to represent a polynomial. */ public class Polynomial { private Map polynomial;

/** Constructs an empty polynomial */ public Polynomial() { polynomial = new TreeMap(); }

/** Constructs a new polynomial with the given term @param t the term to initialize the polynomial with */ public Polynomial(double coefficent, int power) { this(); // to do: your code here }

/** Adds the polynomial such that the terms are in sorted order from highest power to lowest @param p the polynomial to add */ public void add(Polynomial p) { // to do: your code here }

/** Multiplies the given polynomial with this one and returns the result @param p the polynomial to multiply @return this * p */ public Polynomial multiply(Polynomial p) { Polynomial result = new Polynomial();

// to do: your code here: complete this method

return result; }

/** Prints the polynomial "nicely" so that it reads from highest term to lowest and doesn't have a leading "+" if the first term is positive. */ public void print() { boolean firstTerm = true; // to do: your code here: complete this method } }

Term

/** A class to represent an algebraic term. */ public class Term { private double coefficient; private int power;

/** A constructor to initialize a single term with a given coefficient and power @param coefficient the coefficient @param power the power */ public Term(double coefficient, int power) { // to do: your code here }

/** @return the coefficient */ public double getCoefficient() { // to do: your code here }

/** @return the power */ public int getPower() { // to do: your code here }

/** Multiplies two coefficient together and returns the result @param t the other term @return this * t as a term */ public Term multiply(Term t) { // to do: your code here }

/** Adds the term to this term if the powers are the same @param t the term to attempt to add */ public void addIfSamePower(Term t) { if (t.power == power) { coefficient += t.coefficient; } }

/** Returns a string representation of the term with a ^ representing the exponent @return a string representation of a term */ public String toString() { // to do: your code here. Hints: beware if power is 0 or 1. For example: 5X^0 = 5 or 5X^1 = 5X } }

PolynomialTest

/** A class to to test the Polynomial class. */ public class PolynomialTest { public static void main(String[] args) { Polynomial p = new Polynomial(-10.0, 0); p.add(new Polynomial(-1.0, 1)); p.add(new Polynomial(9.0, 7)); p.add(new Polynomial(5.0, 10));

Polynomial q = p.multiply(p); q.print();

/** for above example your output must be: 100.0 + 20.0x + 1.0x^2 - 180.0x^7 - 18.0x^8 - 100.0x^10 - 10.0x^11 + 81.0x^14 + 90.0x^17 + 25.0x^20 */

// try different examples and print output: for example: p = x^2-10X + 1 and q = x^3 + 5x - 20 // print p and q and print p*q

} } }

Write a class Polynomial that stores a polynomial such as: p(x)=5x10+9x7x10 or (p(x)=5x10+9x7x10) as a Map or Hashtable item of terms (it means each term is a pair in your Map (power, coefficient): Map). A term contains the coefficient and the power of x. For example, you would store p(x) as: (5,10),(9,7),(1,1),(10,0). Tasks: 1) Complete class Polynomial.java [ [partial code is provided, compete to do parts] Supply methods to add, multiply, and print polynomials. 2) Complete class [partial code is given) Supply a constructor that makes a polynomial from a single term. For example, the polynomial p can be constructed as p.add(new Polynomial(new Term(5, 10))); For example, the polynomial p can be constructed as Then compute p(x)p(x). Note: test class ; is given 3) Make sure to document and write comments for each part of your code[Anyone who reads your code should know what your code does!] 4) Write the complexity for each method and the complexity of your project overall

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions

Question

5. Develop the succession planning review.

Answered: 1 week ago