How to link two classes in java to use it method
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?
Re: Basic Class Interaction Question
Hello Sterzerkmode,
Just to confirm, you need to use:
But you want the user to be able to input the value for x?
Re: Basic Class Interaction Question
Quote:
Originally Posted by
JavaPF
Hello Sterzerkmode,
Just to confirm, you need to use:
But you want the user to be able to input the value for x?
Yes, I want Function to take an array list of Terms. But I want the Terms to be predefined by whatever the user inputs into the command line (or some other way! I just only know about the command line). Like have whatever the user inputs be put into an array list then have that array list called when you make a Function.
Re: Basic Class Interaction Question
Good afternoon Sterzerkmode,
Here is an example. The user inputs into the console and the Strings are added to an ArrayList.
You can then extract this data from the ArrayList. Obviousally you will need to adapt this to suite your needs.
Code :
import java.util.Scanner;
import java.util.ArrayList;
public class Sterzerkmode {
/**
* JavaProgrammingForums.com
*/
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
Scanner sc = new Scanner(System.in);
System.out.println("Enter term values. Type quit to exit:");
try{
while(sc.hasNext()){
String item = sc.next();
if(item.equals("quit")){
sc.close();
}else
al.add(item);
}
}catch(Exception e){
System.out.println("ArrayList Values:");
System.out.println("=================");
Object[] elements = al.toArray();
for(int a = 0; a < elements.length; a++){
System.out.println(elements[a]);
}
}
}
}