Basic Class Interaction Question
These are my two classes --
Code:
import java.util.ArrayList;
/*/
*
* Will contain methods such as
* boolean isAFunction(), takeDerivative...(), takeIntegral...()
* takeDerivative...() will loop through each Term's takeDerivative() method
*/
public class Function {
private String derivative = "";
private ArrayList<Term> theFunction = new ArrayList<Term>();
public void setDerivative(String derivative) {
this.derivative = derivative;
}
public String getDerivative() {
return derivative;
}
public Function(ArrayList<Term> groupOfTerms) {
theFunction = groupOfTerms; //Constructor. make a function and give it some terms. ta-dah
}
public static void main(String args[]) {
}
public boolean isAFunction(Function maybe) {
//soon to come
}
public void displayFunction(ArrayList<Term> function) {
String theFunction = ""; //string to hold the function
for(Term term: function) {
theFunction = theFunction + " + " + term; //put the terms into theFunction
}
System.out.println(theFunction); //display it
}
public String takeDerivativeOfFunction(Function aFunction) {
for(Term term: theFunction) {
term.takeDerivativeOfTerm(term); //Take the derivative of each term.
setDerivative(derivative + " + " + term); //set the derivative to have each new term
}
return derivative;
}
}
Code:
/*/
*
*Terms will have 3 fields -- coefficient, variable, power
* Will take derivatives of themselves
*
*/
public class Term{
private double coefficient;
private String variable;
private double 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 void setPower(double power) {
this.power = power;
}
public double getPower() {
return power;
}
public Term(double coefficient, String variable, double power) {
this.coefficient = coefficient; //constructor. make a term and give it some values.
this.variable = variable;
this.power = power;
}
public void takeDerivativeOfTerm(Term aTerm) {
double newCoefficient = aTerm.getCoefficient() * aTerm.getPower(); //make some holding variables and differentiate
double newPower = aTerm.getPower() - 1.0;
aTerm.setCoefficient(newCoefficient); //set the new values
aTerm.setPower(newPower);
}
}
In the Function main method, I want to create a Function. But to do so I have to have an array list of Terms. I don't really know how to create a group of Terms in Function without just simply putting "Term x = new Test(...)". But I don't want to do that, I want the Terms to be what the user inputs. Can anyone get me started here? Or direct me to somewhere that can help?