Results 1 to 2 of 2
Thread: Basic Class Interaction Question
- 05-09-2009, 09:35 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 47
- Rep Power
- 0
Basic Class Interaction Question
These are my two classes --
Java 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; } }Java 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?
-
Your Function constructor could theoretically accept a String and an array of doubles for its parameters. The Terms all use the same String for variable, so one String will do, and if the constructor also accepts an array of double, you can use each number in the array for the coefficient and each index for the power of each Term.
Similar Threads
-
Basic File Question
By McChill in forum New To JavaReplies: 6Last Post: 03-20-2009, 08:21 AM -
Basic Netbeans question
By McChill in forum New To JavaReplies: 4Last Post: 02-20-2009, 10:22 PM -
Basic question on OOP.
By madthinker in forum New To JavaReplies: 7Last Post: 01-26-2009, 07:09 PM -
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks