Results 1 to 9 of 9
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post How to Cal methods within a method in the same class

    Hi pple,
    I am writing a program that does calcution of certain functions.The functions are related and somehow i have to interlink the methods.
    So my question is, How can i call a method within another method in the same class

    Your response will be higly appreciated
    thnx

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you can call by this.methodName(yyy,zzz)
    or just methodName (yyy, zz)

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    Am stil confused. Let me give u an example so tht u can know what am talking about
    Java Code:
    public class XenonConcUI extends javax.swing.JFrame {
    
    public XenonConcUI() {
            initComponents();
    }
    public void calEqulibrium(double Phi_t, double IodConc_t_1, double XenConc_t_1) {
    
    //here i calculate Phi_t, IodConc_t_1 and XenConc_t_1
    
    }
    public double result(double xenPoison) {
    
    //In this method, i calculate xenPoist but to calculate it i need the values of 
    Phi_t, IodConc_t_1 and XenConc_t_1 from the method above
    Now my question is ,how do i access the values from the other method(calcEquillibrium)?.
    I just hope my explanation is clear.

    thanx

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    You're methods don't seen t be static, so probably you have to instantiate the class and then call the method:
    Java Code:
    XenonConcUI xc = new XenonConcUI();
    xc.calEqulibrium(x, y z);
    Now... calEquilibreium doesn't return any values ... you might want to change that for your purposes.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  6. #6
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    You can call a method in the same class w/ this example

    public class foo {

    public int method1(int number) {

    int num1 = this.method2();
    int num2 = this.method3();

    return 0;
    }

    private int method2() {
    return 0;
    }

    public int method3() {
    return 0;
    }
    }

    or if your class is a main

    public class Runner {

    public static void main(String[] args) {

    method1();
    method2();

    }

    private static void method1() {

    }

    public static void method2() {

    }

    }

    hope this helps

  7. #7
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

    Post

    My dear friends,thanks for your response but am still in darkness. I just want to get this concept once and for all and i tried to creat a practice program and its still giving me problems. I have added the code below and i kindly request tht u edit my code to the way it should be without bugs. this way i will get the concept.
    Java Code:
    package MonthlyPayments;
    
    /**
     *
     * @author MUNYA00M
     */
    public class Payments {
        static double rate;
        static double loanAmt;
        static double futureValue;
        static double numPeriods;
    public double input(double rate, double loanAmt,double futureValue,double numPeriods){
        rate = 10;
        loanAmt = 2000;
        futureValue = 2500;
        numPeriods = 24;
    }  
    public double computePayment(double loanAmt,
                                 double rate,
                                 double futureValue,
                                 int numPeriods) {
        double interest = rate / 100.0;
        double partial1 = Math.pow((1 + interest), -numPeriods);
        double denominator = (1 - partial1) / interest;
        double answer = (-loanAmt / denominator)
                        - ((futureValue * partial1) / denominator);
        return answer;
    }
    
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            Payments py = new Payments();
            double rate =this.input();
            double loanAmt =this.input(loanAmt);
    
            py.input(rate,loanAmt,futureValue,numPeriods);
    
            py.computePayment(answer);
            
            System.out.println(answer);
        }
    
    }
    Just correct where am going wrong
    thanks a bunch
    Last edited by Manfizy; 02-27-2009 at 09:47 AM.

  8. #8
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Smile :D

    :) try this

    Java Code:
    public class Payments {
    
    	static double rate;
    	static double loanAmt;
    	static double futureValue;
    	static double numPeriods;
    
    	/**
    	 * @param args
    	 *            the command line arguments
    	 */
    	public static void main(String[] args) {
    		// TODO code application logic here
    
    		rate = 10;
    		loanAmt = 2000;
    		futureValue = 2500;
    		numPeriods = 24;
    
    		double answer = computePayment(loanAmt, rate, futureValue, numPeriods);
    
    		System.out.println(answer);
    
    	}
    
    	private static double computePayment(double loanAmt, double rate,
    			double futureValue, double numPeriods) {
    		double interest = rate / 100.0;
    		double partial1 = Math.pow((1 + interest), -numPeriods);
    		double denominator = (1 - partial1) / interest;
    		double answer = (-loanAmt / denominator)
    				- ((futureValue * partial1) / denominator);
    		return answer;
    	}
    
    }

  9. #9
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    7

Similar Threads

  1. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  2. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 11:38 AM
  3. Calling a method on original class from created class
    By kpedersen in forum Advanced Java
    Replies: 4
    Last Post: 08-20-2008, 01:25 AM
  4. Class Reflection: Showing methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:11 PM
  5. Getting methods of a Class - Reflection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-15-2007, 04:18 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
  •