Reputation: 63
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") ;
}
}
Upvotes: 0
Views: 543
Reputation: 1328
The constructor needs parameters:
public Polynomial(int coeff, int expo, ArrayList<String> poly)
{
this.coeff = coeff;
this.expo = expo;
this.poly = poly;
}
You would then create a Polynomial
object by passing parameters in the constructor call:
Polynomial myPolynomial = new Polynomial (someCoefficient, someExponent, someStringArray);
The idea is that the line this.coeff = coeff;
does the following:
1) Uses the closest (in this case the parameter, not the private member) reference for coeff
. So coeff
will take the value of the coeff
passed as a parameter, not the private one inside the object.
2) this.coeff
will use the coeff
from inside the object (the private one).
You can read a lot more online, just search for any tutorial on constructors and basic OOP in general.
Hope it helps! :)
edit: forgot to mention this: your question is really (as in very very) long. I suggest you take it step by step and start by creating a few objects and playing around with them. After you understand the basic concepts better start by solving a bit of the problem at a time and move on from there.
Upvotes: 2
Reputation: 313
It is unclear what exactly you are looking to do, firstly you would need to take in parameters in your constructor as currently you have no values for your coeff, expo and poly variables.
Secondly, what you would need to do for your delete method is iterate through your ArrayList and look for a term that matches the parameters you passed to you delete method by comparing each term in your ArrayList to the syntax you used when inserting the item into the ArrayList.
For example if you inserted items into your ArrayList in the format of "(coeff)_(exp)" (in this case for example 5_2) then when you want to delete the item you would need to iterate through your ArrayList and see if each term in the ArrayList matches the "(coeff)_(exp)" syntax where (coeff) and (exp) would be your method parameters, if so you can then delete it. In this case the () would not be included in your code and are just there to indicate a placeholder value.
Upvotes: 0