简体   繁体   中英

I am not able to add an element to my array list using ".add" operator in Java (Polynomial function)

I'm very new to coding so still struggling and having a bit of problem with my code. For this code, I'm supposed to create a polynomial function using ArrayList by adding polyterm with parameters c (coefficient) and e (exponent) to the list. I was able to construct the code; however, whenever I try running the test code it always says that my polyterm was not added to the list. I wonder if one of you guys could kindly assist me?

Here are my codes.

package polynomials;
import java.util.ArrayList;

public class Polynomial {
    public ArrayList<PolyTerm> terms;
    /**
     * initialize terms to be an empty list
     */
    public Polynomial() {
        //to be completed
        terms = new ArrayList<PolyTerm>();
    }

    /**
     * add a term with the given coefficient and exponent to the list.
     * if a term with given exponent already exists, its coefficient should be updated accordingly. 
     * @param c (coefficient)
     * @param e (exponent)
     */
    public void addTermBasic(double c, int e) { 
        //to be completed
        PolyTerm p = new PolyTerm(c, e);
        for (int i = 0; i < terms.size(); i++) {
            if(p.exponent == terms.get(i).exponent) {
                if (terms.get(i).coefficient + p.coefficient == 0) {
                    terms.remove(i);
                } else {
                    terms.get(i).coefficient = terms.get(i).coefficient + p.coefficient;
                }
            } else {
                terms.add(new PolyTerm(p.coefficient, p.exponent));
            }
        }   
    }
}

and here are the text code

package polynomials;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.*;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PolynomialTest {
    public static int score = 0;
    public static String result = "";
    public static String currentMethodName = null;
    ArrayList<String> methodsPassed = new ArrayList<String>();

    Polynomial p1, p2, p3, p4;

    @BeforeEach
    public void setUp() throws Exception {
        currentMethodName = null;

        p1 = new Polynomial();
        p1.addTermBasic(new PolyTerm(1, 2));
        p1.addTermBasic(new PolyTerm(-2, 1));
        p1.addTermBasic(new PolyTerm(1, 0));
        //p1 represents x^2 - 2x + 1

        p2 = new Polynomial();
        p2.addTermBasic(new PolyTerm(-2, 5));
        p2.addTermBasic(new PolyTerm(5, 3));
        p2.addTermBasic(new PolyTerm(1, 2));
        //p2 represents -2x^5 + 5x^3 + x^2

        p3 = new Polynomial();
        //p3 represents an empty polynomial

        p4 = new Polynomial();
        p4.addTermBasic(new PolyTerm(6, 4));
        //p4 represents 6x^4
    }

    @Test @Order(1) @Graded(description="Polynomial()", marks=2)
    public void testPolynomial() {
        assertNotNull(p3);
        assertNotNull(p3.terms); 
        assertEquals(0, p3.terms.size());
        currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
    }

    @Test @Order(2) @Graded(description="addTermBasic(double, int)", marks=20)
    public void testAddTermBasicDoubleInt() {
        assertNotNull(p1.terms);        
        **assertEquals(3, p1.terms.size());**       
        assertEquals(1, p1.terms.get(0).coefficient, 0.001);
        assertEquals(2, p1.terms.get(0).exponent);
        assertEquals(-2, p1.terms.get(1).coefficient, 0.001);
        assertEquals(1, p1.terms.get(1).exponent);
        assertEquals(1, p1.terms.get(2).coefficient, 0.001);
        assertEquals(0, p1.terms.get(2).exponent);

        p1.addTermBasic(1.5, 2);
        assertEquals(3, p1.terms.size());       
        assertEquals(2.5, p1.terms.get(0).coefficient, 0.001);
        assertEquals(2, p1.terms.get(0).exponent);

        p1.addTermBasic(-5, 0);
        assertEquals(3, p1.terms.size());       
        assertEquals(-4, p1.terms.get(2).coefficient, 0.001);
        assertEquals(0, p1.terms.get(2).exponent);

        p1.addTermBasic(4, 0); //adding 4x^0 to -4x^0
        assertEquals(2, p1.terms.size());       
        assertEquals(2.5, p1.terms.get(0).coefficient, 0.001);
        assertEquals(2, p1.terms.get(0).exponent);
        assertEquals(-2, p1.terms.get(1).coefficient, 0.001);
        assertEquals(1, p1.terms.get(1).exponent);


        p1.addTermBasic(-1.2, 6);
        assertEquals(3, p1.terms.size());       
        assertEquals(-1.2, p1.terms.get(2).coefficient, 0.001);
        assertEquals(6, p1.terms.get(2).exponent);

        p2.addTermBasic(1, 4);
        assertEquals(4, p2.terms.size());       
        assertEquals(1, p2.terms.get(3).coefficient, 0.001);
        assertEquals(4, p2.terms.get(3).exponent);  

        p2.addTermBasic(-1, 4);
        assertEquals(3, p2.terms.size());       
        assertEquals(1, p2.terms.get(2).coefficient, 0.001);
        assertEquals(2, p2.terms.get(2).exponent);  

        p2.addTermBasic(0, 8);
        assertEquals(3, p2.terms.size());       
        assertEquals(1, p2.terms.get(2).coefficient, 0.001);
        assertEquals(2, p2.terms.get(2).exponent);  
        currentMethodName = new Throwable().getStackTrace()[0].getMethodName();
    }
}

The error that I always receive is AssertionFailedError: exected <3> but was: <0> at the bolded line in the test code line 54

I've tried both of the suggestion; however, the error is still the same. Therefore, I tried to use one single .add() to see whether or not any polyterm get added but I still ended up with an empty list.

public void addTermBasic(double c, int e) { 
        //to be completed
        PolyTerm p = new PolyTerm(c, e);
        terms.add(p);
    }

Learn to debug. Step through the code (with a debugger, or if you must, with a lot of System.out statements) using some simple-ish but realistic inputs, and check what the computer does, vs. what you think it should do - use pen and paper if you must. There where you and computer disagree? You found a bug.

HINT: You call .add() within the for loop of addTermBasic . That's not right.

Your logic is flawed.

    for (int i = 0; i < terms.size(); i++) {
        if(terms.get(i)...) {
            ... 
        } else {
            terms.add(...);
        }
    } 

If the list is initially empty, this loop will not do anything, and the else clause will also not occur, so no terms ever get added.

A for loop does not have an else clause, but you can use a boolean to fix it:

   boolean found = false;
   for (int i = 0; i < terms.size(); i++) {
        if(terms.get(i)...) {
            ... 
            found = true;
        }
    }  
    if (!found) {
        terms.add(...);            
    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM