Results 1 to 2 of 2
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Basic File Question
    By McChill in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 08:21 AM
  2. Basic Netbeans question
    By McChill in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 10:22 PM
  3. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  4. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •