The program is not displaying the response I am looking for. The terms are getting added into the Arraylists, and are being differentiated then added into the other ArrayList properly. However, whenever I run the program it only displays the last term. For example, if I put in "3 x 3 + 2 x 2", it will display--

The derivative of 3 x 3 + 2 x 2 is

The term 3.0x3.0was added to terms.

The term 9.0x ^2.0was added to derivative.

The term 2.0x2.0was added to terms.

The term 4.0x ^1.0was added to derivative.

4.0x ^1.0 +

I want it to display this --

The derivative of 3 x 3 + 2 x 2 is

The term 3.0x3.0was added to terms.

The term 9.0x ^2.0was added to derivative.

The term 2.0x2.0was added to terms.

The term 4.0x ^1.0was added to derivative.

9.0x ^2.0 + 4.0x ^1.0 +

Any help is appreciated...been trying to get this to work for too long now..Code:`import java.util.Scanner;`

import java.util.ArrayList;

import java.util.regex.Pattern;

public class Derivative {

private double power;

private double coefficient;

private String variable;

public Scanner scanner;

public ArrayList<String> terms;

public ArrayList<String> derivative;

static Derivative d = new Derivative();

public static void main(String args[]) {

System.out.println("The derivative of " + args[0] + " is\n");

System.out.println(d.getDerivative(args[0]));

}

public void setPower(double power) {

this.power = power;

}

public double getPower() {

return power;

}

public void setCoefficient(double coefficient) {

this.coefficient = coefficient;

}

public double getCoefficient() {

return coefficient;

}

public void setVariable(String variable) {

this.variable = variable;

}

public String getVariable() {

return variable;

}

public String getDerivative(String function)

{

analyze(function); //analyze the function given

String answer = "";

for(String each: derivative) {

answer = answer + each; //put each term in derivative into the answer String

answer = answer + " + "; //add the + signs...adds an extra one right now until I find a better way.

}

return answer;

}

public void analyze(String function) {

scanner = new Scanner(function); //pass the function to the scanner

scanner.useDelimiter("\\W+");

terms = new ArrayList<String>();

int x = 0;

while(scanner.hasNext()) { //while loop through the function

if(scanner.hasNextDouble()) {

setCoefficient(scanner.nextDouble()); //set the coefficient as the first double

}

setVariable(scanner.next()); //set the variable as the next token after first double

if(scanner.hasNextDouble()) {

setPower(scanner.nextDouble()); //set the exponent power as the next double

}

else setPower(1); //if the next token wasn't a double, set the power to 1

terms.add(getCoefficient() + getVariable() + getPower()); //set the term into the arraylist

System.out.println("The term " + getCoefficient() + getVariable() + getPower() + "was added to terms."); //just for testing purposes

differentiate(terms.get(x));

x++;

}

}

public ArrayList<String> differentiate(String analyzedFunction) {

derivative = new ArrayList<String>();

double newCoefficient = getPower() * getCoefficient(); //exponent times coefficient

double newPower = getPower() - 1.0; //exponent power minus 1

derivative.add(newCoefficient + getVariable() + " ^" + newPower);

System.out.println("The term " + newCoefficient + getVariable() + " ^" + newPower + "was added to derivative."); //just for testing purposes

return derivative;

}

}