Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Value-returning methods with and without a parameter (http://www.programmingforums.org/showthread.php?t=15651)

kewlgeye Apr 18th, 2008 8:56 PM

Value-returning methods with and without a parameter
 
How do I do this?

Combine into one program 2 different created loops with 2 distinct methods.

1.) one value returning method with a parameter and
2.) one void method without a parameter

I have two of my loop programs that I created last week. But I don't know how to modify them or combine them to do what I asked above. How can I do this. These are my loops.

BREAK LOOP

:

  1. import java.util.*;
  2.  
  3. public class loop
  4. {
  5.  
  6. public static void main (String[] args){
  7.  
  8. for(int i = 30; i > 0; i--)
  9. {
  10. if(i == 27)
  11. continue;
  12. else if(i < 5)
  13. break;
  14. else
  15. System.out.println(i);
  16. }
  17. }
  18. }



and a DO-WHILE LOOP

:

  1. import java.util.*;
  2.  
  3. public class loop
  4. {
  5.  
  6. public static void main (String[] args){
  7.  
  8. int loopcount = 31;
  9. do
  10. {
  11. loopcount--;
  12. if(loopcount == 27)
  13. continue;
  14. System.out.print(loopcount + " ");
  15. }
  16.  
  17. while (loopcount > 5);
  18.  
  19. }
  20. }


I don't understand how to do this, could someone tell me how?

Fall Back Son Apr 19th, 2008 2:01 PM

Re: Value-returning methods with and without a parameter
 
Your question is unclear. Explain what exactly you're looking for. Also, you can always call a method from one class if you're in a different class, assuming that you know what package it is in. If they are in the same package, you can use TheClass.theMethodName(); so combining things into the same class isn't necessary much of the time. If they aren't in the same package, use thePackageName.TheClass.theMethodName();


If that didn't make any sense, consider putting your loops from each of your classes into methods. Then, in main, all you have to do is call the method name. After you do that, then go back and read what I just said above.



[Side note: you can also treat main like any other method, although I don't recommend doing so.]

kewlgeye Apr 19th, 2008 2:10 PM

Re: Value-returning methods with and without a parameter
 
Quote:

Originally Posted by Fall Back Son (Post 144130)
Your question is unclear. Explain what exactly you're looking for. Also, you can always call a method from one class if you're in a different class, assuming that you know what package it is in. If they are in the same package, you can use TheClass.theMethodName(); so combining things into the same class isn't necessary much of the time.


I don't know how to say it any other way.

the two loops should all be in one program instead of 2 seperate programs with the methods I described above.

Jimbo Apr 19th, 2008 2:12 PM

Re: Value-returning methods with and without a parameter
 
So you need to refactor the two programs you have each into a function (as described in the first post) and put these functions into a single program?

kruptof Apr 19th, 2008 2:59 PM

Re: Value-returning methods with and without a parameter
 
Doesn't Java support function overloading

Fall Back Son Apr 19th, 2008 5:36 PM

Re: Value-returning methods with and without a parameter
 
Yes, Java supports method overloading. But that can't possibly be the problem he's having, since he didn't say anything about the classes being in the same hierarchy. Also, OP, if you're unwilling to rephrase the problem you're having, then how can you expect to be helped?

kewlgeye Apr 19th, 2008 6:35 PM

Re: Value-returning methods with and without a parameter
 
Quote:

Originally Posted by Jimbo (Post 144134)
So you need to refactor the two programs you have each into a function (as described in the first post) and put these functions into a single program?


Jimbo - Yes you said it exactly. Thank you.


All times are GMT -5. The time now is 1:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC