Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2008, 8:56 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
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

java Syntax (Toggle Plain Text)
  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

java Syntax (Toggle Plain Text)
  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?
kewlgeye is offline   Reply With Quote
Old Apr 19th, 2008, 2:01 PM   #2
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 257
Rep Power: 2 Fall Back Son is on a distinguished road
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.]
Fall Back Son is offline   Reply With Quote
Old Apr 19th, 2008, 2:10 PM   #3
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Re: Value-returning methods with and without a parameter

Quote:
Originally Posted by Fall Back Son View Post
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.
kewlgeye is offline   Reply With Quote
Old Apr 19th, 2008, 2:12 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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?
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 19th, 2008, 2:59 PM   #5
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 330
Rep Power: 3 kruptof is on a distinguished road
Re: Value-returning methods with and without a parameter

Doesn't Java support function overloading
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old Apr 19th, 2008, 6:35 PM   #6
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Re: Value-returning methods with and without a parameter

Quote:
Originally Posted by Jimbo View Post
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.
kewlgeye is offline   Reply With Quote
Old Apr 19th, 2008, 5:36 PM   #7
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 257
Rep Power: 2 Fall Back Son is on a distinguished road
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?
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:16 AM.

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