java

时间:2015-12-04 02:15:34

标签: java arrays

我很顽固地坚持完成这项任务。我制作了Insert方法,我很确定它是对的,但我不明白为什么对象Polynomial没有任何参数以及它为什么需要一个体?

我不知道如何从数组列表中删除基于2个参数的项目。如果我需要删除带有系数和指数的东西,我就不能做poly.remove(coeff,expo),所以我怎么能删除我正在寻找的EXACT术语。

此外,我必须制作的方法之一是列表中术语的乘积。我怎么能得到所有系数和指数并相互相乘?

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 个答案:

答案 0 :(得分:2)

构造函数需要参数:

public Polynomial(int coeff, int expo, ArrayList<String> poly)
{
    this.coeff = coeff;
    this.expo = expo;
    this.poly = poly;
}

然后,您将通过在构造函数调用中传递参数来创建Polynomial对象:

Polynomial myPolynomial = new Polynomial (someCoefficient, someExponent, someStringArray);

我们的想法是,this.coeff = coeff;行执行以下操作:

1)使用coeff的最接近(在本例中为参数,而不是私有成员)参考。因此coeffcoeff的值作为参数传递,而不是对象内的私有值。

2)this.coeff将使用对象内部的coeff(私有)。

您可以在线阅读更多内容,只需搜索有关构造函数和基本OOP的任何教程。

希望它有所帮助! :)

编辑:忘了提这个:你的问题真的(非常非常)长。我建议你一步一步地开始创建一些对象并玩弄它们。在理解了基本概念之后,首先要解决一些问题并从那里开始继续。

答案 1 :(得分:0)

目前还不清楚你究竟想要做什么,首先你需要在构造函数中接受参数,因为目前你没有你的coeff,expo和poly变量的值。

其次,你需要为你的删除方法做的是迭代你的ArrayList,并通过比较ArrayList中的每个术语和插入时使用的语法来查找与你传递给delete方法的参数相匹配的术语。 item放入ArrayList。

例如,如果您以“(coeff)_(exp)”格式将项目插入ArrayList(在本例中为5_2),那么当您想要删除该项目时,您需要遍历ArrayList并且查看ArrayList中的每个术语是否与“(coeff)_(exp)”语法匹配,其中(coeff)和(exp)将是您的方法参数,如果是,则可以将其删除。在这种情况下,()不会包含在您的代码中,只是表示占位符值。