0

I have a class called Polynomial with an ArrayList made up of term objects, there is an external file that is read by a Scanner object in my test class. The Scanner reads the line for 4 different key words and acts accordingly. ex. INSERT 3 2. Would call my insert method and print out 3x^2. Now I have a delete method with two parameters. When I call the method in the test class nothing happens, the same thing gets printed and nothing has been removed. Am I missing something or doing it wrong all together? Any help is greatly appreciated.

public void delete (int coeff, int expo)
{
  for (int i = 0; i<terms.size(); i++)
  {
      Term current = terms.get(i);
      terms.remove(current.getCoeff());
      terms.remove(current.getExpo());
  }

}

I also have a Term class that creates a term object, and has two methods to get the coefficient and exponent.

Here is a snippet of my test class:

public static void main(String[] args) throws IOException
{
    // TODO code application logic here
    Polynomial polyList = new Polynomial();



    Scanner inFile = new Scanner(new File("operations2.txt"));

    while(inFile.hasNext())
    {
       Scanner inLine = new Scanner(inFile.nextLine());

       String insert = inLine.next();

        if(insert.equals("INSERT"))
        {

           int coeff = inLine.nextInt();
           int expo = inLine.nextInt();            
           polyList.insert(coeff, expo);
        }
        if(insert.equals("DELETE"))
        {
            int coeff = inLine.nextInt();
            int expo = inLine.nextInt();
            polyList.delete(coeff, expo);
        }
    }
     System.out.println(polyList.toString());
   }
}

Edit: this is a sample of the .txt file that is being read by the scanner class:

INSERT 3 2
INSERT 4 4
INSERT 1 6
INSERT 2 0
INSERT 5 2
INSERT 6 3
PRODUCT
DELETE 3 2
INSERT 2 7
DELETE 4 4
INSERT 4 10

Edit: Here is the Term class:

class Term
{
//instance vars
private int coefficient;
private int exponent;


 public Term(int coeff, int expo)
 {
  coefficient = coeff;
  exponent = expo;

 }
 public int getCoeff()
 {
   return coefficient;
 }
 public int getExpo()
 {
   return exponent;
 }
 @Override
 public int hashCode()
 {
   return  coefficient + exponent;
 }

   @Override
   public boolean equals(Object o)
   {

     if (!(o instanceof Term))
     {
       return false;
     }
     Term t = (Term)o;
     return coefficient == t.coefficient && exponent == t.exponent;
   }
}
4

3 に答える 3

0

項リストからを削除しようとしているのTermではなく、係数と指数を削除しようとしています。

  for (int i = 0; i<terms.size(); i++)
  {
      Term current = terms.get(i); // Your list contains Term objects
      terms.remove(current.getCoeff()); // but you are try to removing a coefficient
      terms.remove(current.getExpo()); // and an exponent
  }

iが大きくなり、リストが小さくなるため、この方法を削除しても機能しないという一般的な注意事項もあります。したがって、たとえば最後の用語 (where i = terms.size() - 1) を削除するまでには、リストには 1 つの項目しか残っていません。すべてのアイテムを削除しようとしている場合は、リストのclear方法を検討してください。

于 2013-04-21T22:53:04.773 に答える
0

delete メソッドが引数 coeff と expo を取るのはなぜですか .... ...それらに対して何もしません。

実際、delete メソッドは非常に疑わしいように見えます。用語の配列がどのように見えるかについてもっと詳しく説明する必要がありますが、現時点では意味がありません。

ロルフル

于 2013-04-21T22:58:16.873 に答える
0

メソッドが指定された係数で Twrm を削除しようとしている場合delete()は、次のことをお勧めします。

  1. 引数が同じ係数と指数を持つ Term である場合equals()に返すメソッドをオーバーライドしますtrue
  2. メソッドをオーバーライドしhashCode()て、同じ 2 つの値に基づいてハッシュを返す

equals()メソッドはの比較を行う必要があるため、このような実装は非常に合理的です。

これが完了すると、delete メソッドは 1 行になります。

terms.remove(new Term(coeff, expo));

実装は次のようになります。

// in the Term class
@Override
public boolean equals(Object o) {
    if (!(o instanceof Term)
        return false;
    Term t = (Term)o;
    return coeff == t.coeff && expo == t.expo;
}

メソッドをオーバーライドするhashCodeことは、コードを機能させるために厳密に必要というわけではありませんが、良い習慣であるため、実装の例を次に示します。

@Override
public int hashCode() {
    return 31 * coeff + expo;
}
于 2013-04-21T23:05:14.123 に答える