Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on methods  RSS feed

 
RJ Cavender
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still am struggling with understanding "passing to and passing BACK from" methods. This code "has" worked. Tinkering with it over and over I think has affected it (or me?). The idea is to create a main program that will call to methods that are just stubs. All they will do is say "the modify member method has been called" with a System.out.println. Im trying to find out where i went wrong on getting it to call the first method by pressing the number one in the menu. I would appreciate any input.



[fbr - edited to put in code tags]
[ March 02, 2007: Message edited by: Fred Rosenberger ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method Menu returns a double, and this is what (I think) you want to assign to menuAnswer. So instead of...

...I think you want...

A few notes:
  • Java classes conventionally begin with an uppercase letter, and methods with a lowercase letter.
  • Is there a reason for floating point values here? You might want to use ints instead of doubles.
  • Using ints (or even Enums), you could use a switch/case instead of multiple if/elseif/elseif/else...
  • Code Tags will keep your formatting intact when you post.

  • [ March 02, 2007: Message edited by: marc weber ]
     
    RJ Cavender
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    HOLY cow! This is what I love (and DONT love) about Java. Sometimes its just the simplest things. Now at least i have more to work with and can build off of this. Thank you!
     
    RJ Cavender
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Oh by the way, the reason I used doubles.............at this stage in my learning......they worked, so i left them. Stupid answer but thats all i got now. I changed to int. JCreator has a nice find/replace feature.
     
    Campbell Ritchie
    Marshal
    Posts: 55681
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    HOLY cow! This is what I love (and DONT love) about Java.
    You don't think that applies to other programming languages???
     
    Niall Loughnane
    Ranch Hand
    Posts: 209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The reason that the code doesn't work is that you create a menuAnswer in the main method and a second one in the Menu() method.

    So when you assign the menuAnswer in the Menu() method this doesn't assign the value to the menuAnswer in the main() method.

    Possible solutions are:
    1.Have a global double static menuAnswer variable.
    You can then call Menu() method and the value set in Menu() method can be used by the
    Menu() method.
    2.Call Menu() as marc described :-) i.e. double menuAnswer = Menu();

    just a few suggestions :-)
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Niall Loughnane:
    The reason that the code doesn't work is that you create a menuAnswer in the main method and a second one in the Menu() method...

    Yes, the variable is shadowed, so if the expected behavior was for the method to directly modify the variable, there would be a problem.

    In this case, the question was about passing and returning values from methods, so I'm not sure what was expected. The method does return the proper value, but that value wasn't being captured. The shadowed variable might have been a shot in the dark to achieve this.

    RJ: This is an important concept, so I want to make sure we're clear. When you declare menuAnswer inside the method, you have two separate variables called "menuAnswer." One of these is "local" -- limited to the scope of the method -- so when you use the simple name "menuAnswer" inside the method, it refers only to that local variable. The other menuAnswer is an instance variable of the class.

    If you are inside a method calculating a value to return, then it's probably best to use a different variable name to avoid confusion. For example, inside the method you might use "tempMenuAnswer" in your calculations, and then return that value at the end of the method (where it's assigned to the instance variable "menuAnswer").
    [ March 03, 2007: Message edited by: marc weber ]
     
    RJ Cavender
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marc, I understand (I think, pardon my greenhorn-ness) where you're coming from with the same variable in main as it is in the menu method. I got that illustrated to me when I set the value to 0 in main and couldnt wonder why the system.out.println never ran. It never ran because if the user entered a one, it returned control to main WHICH set it back to zero.

    The reason i mention this is Ive been assigning my variables 0 because JCreator LE gives me an error if I dont. Ive since found that the program will run anyway.

    If I could trouble you for one more question, I have to put a method called start_program and one called exit_program in. They are both only to run once, start_program of course at the beginning. My problem is, if i put start_program(); in Main, it never runs. I have a method with a stub saying "start method called" but i never see it. I "do" see the menu that comes up though. Its like it ignores start_program(); but doesnt ignore menu();. Any thoughts:


    import javax.swing.JOptionPane;

    public class ymcaProject {

    public static void main(String[] args) {

    start_program();

    //Call to the menu method
    double menuAnswer=Menu();


    switch (menuAnswer){

    case 1: Modify_Member();

    case 2: Modify_Enrollment();

    case 3: Report();

    case 4: exit_program();
    }

    }//end of main method
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by RJ Cavender:
    ...I have a method with a stub saying "start method called" but i never see it. I "do" see the menu that comes up though. Its like it ignores start_program(); but doesnt ignore menu();...

    Hmmm... Could you post the complete code?
     
    RJ Cavender
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, that would be helpful wouldnt it. Im supposed to, on top of having a menu calling methods that are just stubs, have a method called start_program and exit_program that run only once. It makes sense that it HAS to run in main, am I right? But I put it there, perfect syntax and it doesnt run, it skips straight to Menu and that runs fine.


    EDIT by mw: Added Code Tags.
    [ March 03, 2007: Message edited by: marc weber ]
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This code won't compile, because menuAnswer is still a double, and the switch statement requires an int. As a quick and dirty way to get around this, I just inserted an explicit cast on the returned method value...

    int menuAnswer=(int)Menu();

    After that, the start_program method works fine for me. My output is...
    The start program Method has been called
    The Report Method has been called
    The exit program Method has been called
     
    RJ Cavender
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. I really appreciate the help here. Sometimes all you need to get over that hump in your head is a question or two answered.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!