Polynomial.java operation.text Please show me all detail and comments! Thats rea
ID: 3694184 • Letter: P
Question
Polynomial.java
operation.text
Please show me all detail and comments! Thats really help me to review. Appreciated it !
The ArraVLnSC ClasS I. The Term Class » in the term 4x*, the coefficient is 4 and the exponent 2 · in -6x®, the coefficient is -6 and the exponent 8 Your Term class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, accessor methods that return the coefficient and the exponent Your class will also have atoStrina) method that returns a Term object as a String, formatted as shown in these examples: returns Exponent 3 Coefficient 4 0 4 II. The Polynomial Class Now create a class to represent a polynomial here, a polynomial is a sequence of terms. E.q. As defined 1. 3x2 + 4x4 + x6 2. 2 + 5x2 + 6x3 + 2x'7 3. 4x10 The terms of polynomial 1 are (3.2), (4, 4) and (1,6). The terms of polynomial 2 are (2JQ), (5,2), (6,3) and (2,7) Polynomial 3 has only one term (4,10)Explanation / Answer
implemented except reverse function;
import java.util.ArrayList;
public class PolynomialTest {
public static void main(String[] args) {
Polynomial1 polyn = new Polynomial1();
polyn.insert(3, 2);
polyn.insert(4, 4);
polyn.insert(1,6);
System.out.println(polyn.toString());
System.out.println(polyn.product());
}
}
/**
* A class to implement a Polynomial as a list of terms, where each term has
* an integer coefficient and a nonnegative integer exponent
* @author your name
*/
class Polynomial1
{
// instance variable declarations go here
private ArrayList<Integer> exponent;
private ArrayList<Integer> coefficient;
/**
* Creates a new Polynomial object with no terms
*/
public Polynomial1()
{
exponent = new ArrayList<Integer>();
coefficient = new ArrayList<Integer>();
// TO DO: Write constructor body here
}
/**
* Inserts a new term into its proper place in a Polynomial
* @param coeff the coeffiecent of the new term
* @param expo the exponent of the new term
*/
public void insert(int coeff, int expo)
{
// TO DO: write method body here. The following statement is included
// only for development purposes. Remove after implementing the method
System.out.println("insert method called for " + coeff + " " + expo) ;
boolean added = false;
//lower to higher power - 0 - index of exponent
for(int i = 0; i < exponent.size(); i++) {
if(expo < exponent.get(i)) {
exponent.add(i, expo);
coefficient.add(i, coeff);
added = true;
break;
}
}
if(!added) {
//adds at the end
exponent.add(expo);
coefficient.add(coeff);
}
}
/**
* Deletes the first occurrence of a specified term from a Polynomial, or
* prints an appropriate message if the term does not appear in the
* Polynomial
* @param coeff the coeffiecent of the term to be deleted
* @param expo the exponent of the term to be deleted
*/
public void delete (int coeff, int expo)
{
// TO DO: write method body here. The following statement is included
// only for development purposes. Remove after implementing the method
System.out.println("delete method called for " + coeff + " " + expo) ;
for(int i = 0; i < exponent.size(); i++) {
if(exponent.get(i) == expo) {
exponent.remove(i);
coefficient.remove(i);
}
}
}
/**
* Returns the product of all the terms of a Polynomial, as a String
* E.g. for the polynomial 3x^2 + 7x^3 + 2x^5, will return 42x^10
* @return the polynomial product, as a String
*/
public String product()
{
// TO DO: write method body here. The following statements are included
// only for development purposes. Remove after implementing the method
System.out.println("product method called") ;
//multiply coefficients and add exponents
int esum = 0;
int cprod = 1;
for(int i = 0; i < exponent.size(); i++) {
esum += exponent.get(i);
cprod *= coefficient.get(i);
}
return cprod+"x^"+esum ;
}
/**
* Returns a polynomial as a String in this form: 3x^2 + 7x^3 + 2x^5
* @return the polynomial as a String
*/
public String toString()
{
// TO DO: write method body here. The following statements are included
// only for development purposes. Remove after implementing the method
System.out.println("toString method called") ;
String poly = "";
for(int i = 0; i < exponent.size(); i++) {
poly = (coefficient.get(i)+" x ^"+exponent.get(i))+" +"+poly;
}
return poly;
}
/**
* Reverses the order of the terms of a Polynomial.
* E.g. the polynomial 3x^2 + 7x^3 + 2x^5 would be 2x^5 + 7x^3 + 3x^2 after
* reversal
*/
public void reverse()
{
// TO DO: write method body here. The following statement is included
// only for development purposes. Remove after implementing the method
System.out.println("reverse method called") ;
}
}
--output-----------
insert method called for 3 2
insert method called for 4 4
insert method called for 1 6
toString method called
1 x ^6 +4 x ^4 +3 x ^2 +
product method called
12x^12