当前位置: 首页 > 知识库问答 >
问题:

java中的ArrayLists和方法

蒋啸
2023-03-14

还有,我要做的方法之一是列表中术语的乘积。我应该如何得到所有的系数和指数,并相互乘?

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") ;
}
}

共有1个答案

秋博容
2023-03-14

构造函数需要参数:

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

然后,通过在构造函数调用中传递参数来创建多项式对象:

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

其思想是,this.coeff=coeff;行执行以下操作:

 类似资料:
  • 问题内容: 好,所以我只占了库存的一小部分。我记不清了。我试图将字符串项添加到ArrayList,然后将其添加到JList。但是,在编译时出现此错误: 我可能正在犯一些菜鸟错误…:/ 码: 问题答案: JList* 提供了一个构造函数 JList(Object []) ,您可以在使用 toArray() 解 压缩ArrayList 后调用该构造函数: *

  • 我试图通过合并另外两个列表来生成ArrayList。我允许重复对象,但是我的结果ArrayList必须包含两个初始列表之间的差异。我意识到这听起来可能很复杂,所以这里有一个例子: ArrayList 1:[obj1,obj1,obj1,obj2,obj4,obj4] ArrayList 2:[obj1,obj2,obj2,obj3] 我觉得这应该很简单,但我似乎想不通。我将使用ArrayList1

  • 我不明白为什么ArrayList的ArrayList中的“alofCompoundedPrimean”中的每个元素最后都是相同的。因此,尽管ArrayList“TheNewPrimeFactorSal”看起来是正确的,但for循环的每次迭代似乎都将AlofCompoundedPrimean的所有元素更改为最新迭代的元素;也就是说,AlofCompoundedPrimean不是索引唯一的元素,而是将

  • 我正在使用以下代码创建ArrayLists的数组: 如前所述,泛型数组是不允许的。我的代码是怎么编译的?

  • 问题内容: 我正在学习Java 8 lambda表达式,我想问一下以下与我遇到的函数接口中的方法有关的Java代码。 在IDE上执行程序时,它不输出。我期望它会给。 问题答案: 我假设您正在Java 9下运行它?您没有更改流的属性,因此不需要执行任何一个或全部执行。 换句话说,您关心的只是最终结果,但与此同时,您不以 任何 方式(例如通过或)更改的初始大小。这是在Streams中完成的优化。 顺便

  • 问题内容: 我有一个自定义对象的ArrayList。我要删除重复的条目。 对象具有三个字段:和。如果字幕多次出现,那么我只需要带有thats字幕的第一项(忽略带有该字幕的其余对象)。 问题答案: 您可以使用自定义Comparator将ArrayList的内容放入TreeSet中,如果两个字幕相同,则应返回0。之后,您可以将Set转换为列表,并使列表中没有“重复项”。这是对象的示例,您当然应该使用正