Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to link two classes in java to use it method

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to link two classes in java to use it method

    These are my two classes --

    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;			
     
    	}
     
    }

    /*/
     * 
     *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
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Basic Class Interaction Question

    Hello Sterzerkmode,

    Just to confirm, you need to use:

    Term x = new Term();
    But you want the user to be able to input the value for x?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Class Interaction Question

    Quote Originally Posted by JavaPF View Post
    Hello Sterzerkmode,

    Just to confirm, you need to use:

    Term x = new Term();
    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.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

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

    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]);                    
                }
            }
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM
  2. [SOLVED] How to narrow down the range of input in java?
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2009, 12:38 PM
  3. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 07:01 AM
  4. help us in eclipse basic codes
    By bil_Imma in forum AWT / Java Swing
    Replies: 1
    Last Post: January 24th, 2009, 06:02 PM