0

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") ;
}
}
2
  • What is the purpose of the coeff and expo member variables? I don't see them being used anywhere. You don't really need anything in your constructor; you can just initialize poly to an empty list, which you're already doing. Commented Dec 4, 2015 at 2:22
  • it was a misunderstanding from the notes in the assignemnts, it said not to use terms in the constructor but I have to do that in order to use coeff and expo. Commented Dec 4, 2015 at 2:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

the assignment specifies that the object had no terms, so i was confused as to why it had no parameters. I dont know why she wrote that in. So if i were to remove something from the list, i would do poly.remove()? but not have anything in it?
@RafaelLeal-Mccormack It is unclear as to what the poly list is for, is it supposed to be a collection of all Polynomial objects? If so then you wouldn't want it to be inside your Polynomial class. If it stores specific attributes about the specific polynomial then you would need something like poly.remove(coeff, exp) for example to have values to check against all terms in the ArrayList.
the poly list is for adding different polynomials into the list. Youre supposed to be able to add polynomials so you would have a list like (4x^2, 2x^7, 7x^3, etc.) the .remove doesnt allow me to put in 2 parameter because it expects to be looking for the index of the number and it tells me i'm wrong when i type in .remove(coeff, exp), i dont know how else to do it.
@RafaelLeal-Mccormack Then instead of having the ArrayList in your Polynomial class, you would likely need an ArrayList of type Polynomial outside of your Polynomial class that would store Polynomial objects. You would need to create Polynomial objects and add them to the ArrayList then have something like Polynomial#getName() which would return the formatted syntax of your polynomial (ex. 4x^2) based on the instance variables and you would then compare that to each Polynomial instance in the ArrayList checking if it matches the one you want to delete.
I understand what youre saying, and I made a test class and i made it give me the list, so i know im saving the numbers into a list. The only thing i dont understand is what exactly i could use in code using the .remove method of the array import. I was thinking something like poly.remove(poly.get(SOMETHING)) but that doesnt work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.