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 can I refer methods ??

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How can I refer methods ??

    Hello guys ..

    first this is part of my code.. there are few mistakes and missing stuff but i am still working on it

    private static boolean isNowOverdrawn() 
    	{
    		if ( dollars < 0 )
    		{
    		return true;
    		}
    	else
    		{
    		return false;
    		}
    	}
     
    	private static void deposit(double amount) 
    	{
     
    			dollars += amount;
    			// check to see if not overdrawn and update isNowOverdrawn variable
     
    			JOptionPane.showInputDialog("Enter amount: $");
    			if( amount < 0 )
    		{
    			JOptionPane.showMessageDialog(null, "It's overdrawn");		
    		}
    		else
    		{
    			return; // here I want to update the balance
    		}
    	}

    the question is ..

    - A void method called deposit that takes a double amount as a parameter. This amount will be added to the dollars in the account (and could of course influence whether it is now overdrawn or not).
    - A Boolean method called isNowOverdrawn (with no parameters to it) that returns a true if the account is currently overdrawn and a false otherwise.
    - A double returning method called balance (with no parameters to it) that returns the dollars now in the account.


    I am a beginner programmer so please be patient with me ^^ ..
    my Question is :

    How can I refer one method to another ?
    to clarify .. when I deposit money I have to check if it's overdrawn or not ? and then update the overdrawn .. ??
    the deposit is method and the overdrawn is another one .. when I write ( return overdrawn ) i've error message said ( void method can not be return to void method )


    anyone has a clue .. could help ..

    Thanks


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    36
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: How can I refer methods ??

    First, to call the method of isNowOverdrawn in deposit, you can do like this:

    dollars += amount;
    boolean res = isNowOverdrawn();
    if (res == true){
    ... // is overdrawn
    } else{
    ...
    }

    ...

    Second, form your words, I think the method of deposit is not a void method, it should be a double method.

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    30
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I refer methods ??

    Thanks for replying ..

    Ok now if I want to get the deposit from the user and update the balance method ..

    this is what i am stack in ..

    how can I connect two methods together ??

  4. #4

    Default Re: How can I refer methods ??

    how can I connect two methods together ??
    Methods do not get "connected". One gets called and can call another.

    If I have a class, Foo, and two methods in that class, barOne() and barTwo(), then I can choose to call them like so:

    Separately,
    Foo.barOne();
    Foo.barTwo();

    Or as one calling the other

    public void barOne()
    {
         barTwo();
    }
     
    public void barTwo()
    {
         System.out.println("Inside two");
    }

    or...


    public void barOne()
    {
         System.out.println("Inside one");
    }
     
    public void barTwo()
    {
         barOne();
    }
    Last edited by kenster421; October 13th, 2011 at 08:21 AM.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Overriding Methods
    By tcstcs in forum Java Theory & Questions
    Replies: 5
    Last Post: June 2nd, 2011, 04:23 AM
  2. Methods and Exceptions
    By Mandraix in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2011, 07:15 PM
  3. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  4. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM
  5. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM