I'm pretty stuck on this assignment for school I was given. I made the Insert method and I'm pretty sure it's right but I don't understand why the object Polynomial doesnt have any parameters and why it needs a body?
I don't know how to delete an item off the array list that is based on 2 parameters either. If i need to delete something with a coefficient and exponent, i cant do poly.remove(coeff, expo), so how could I make it delete the EXACT term im looking for.
Also, one of the methods i have to make is the product of the terms in the list. How am i supposed to get all the coefficients and exponents and multiply to each other?
package assignment9;
import java.util.ArrayList;
/**
* 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
*/
public class Polynomial
{
// instance variable declarations go here
private int coeff;
private int expo;
ArrayList <String> poly = new ArrayList <String>();
/**
* Creates a new Polynomial object with no terms
*/
public Polynomial()
{
this.coeff = coeff;
this.expo = expo;
this.poly = poly;
// 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)
{
if(expo == 0)
{
poly.add(coeff + " ");
}
if(expo == 1)
{
poly.add(coeff + "x");
}
else
poly.add(coeff+ "x^"+ expo);
}
/**
* 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)
{
if (coeff != 0)
poly.remove(coeff);
else
System.out.println("The coefficient you are looking for does not exist");
// 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) ;
}
/**
* 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") ;
return "product method is under construction" ;
}
/**
* 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") ;
return "toString method is under construction" ;
}
/**
* 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") ;
}
}