• Post Reply Bookmark Topic Watch Topic
  • New Topic

passing variables between main and a test class  RSS feed

 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

I'm working on a larger project for a class. It requires me to have a main.java, and create a class in a separate file. I wrote a big program up but am having trouble passing variables to the class, modifying them, and then returning them to the main.java. I thought I could pass and return variables like I normally do for any method contained in the main.java, but it is not working. To try and figure this out for myself, I created another simple program to see what I could do to get variables passed from main.java and the testclass.java as shown below.

Can you guys help point me in the right direction how to get the class to modify the "i" variable, then return it to main.java? The output of this program just keeps repeating "5" for every status and result.

Thanks!

Steve



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,
You are really close!

Consider how you are calling this method.


You are calling the method which returns the new value of i. Then you are ignoring the return value. What if you set it in the main method to the existing i variable.

To make this a bit clearer, I've renamed i to temp. Now in main, we have "i" and in testclass, we have "temp". This makes it more obvious we are dealing with two variables. And Java has no way of knowing you want to assign iminus(i) back to i.

package assignment7_testclasses;


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you didn't ask this, but I'd like to mention that "testclass" should be named "TestClass" to follow standard Java conventions.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, interesting. Are you suggesting that I create another variable called temp, and set "i = temp" in the main.java? I'm still a tad lost.

When you say "it ignores the 'i' coming back form the class," I guess my trick is getting the main to recognize that it is new... but how do I get the main to recognize the temp that is coming back?

What I tried is creating "int counter = i;" in the main, then sent the counter to the method. the method is set to return 'counter'. then in the 'while' loop in main, i told it to make i = counter, but it still doesn't work. Maybe my logic or way of doing things is very far off?



To give you an idea of what is going on, I've got the ubiquitous "vending machine" as one of my final programs to write for the semester. I made a flowchart and laid it out all the different things to do in methods in a separate class file. It works beautifully, but none of the variables are going back and forth between the main.java and class file.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven,
No. I was trying to rename one to temp so it would be more obvious they were separate variables. That didn't work out as I intended.

Consider the difference between the following two lines:


What is the difference between the two? Why does it matter?

I'm asking a lot of questions to make you think about rather than just saying the answer. I hope this makes it more memorable and easier to understand in the long run.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne... Thanks for that. I like your approach. No, I definitely don't want the answer. The tough thing about online classes is I don't have any examples to go on other than google and the book (which, I must add, isn't exactly inspiring LOL).

I think you just flipped a bulb in my head. Results soon, I hope!

Thanks!
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see what's going on now. It didn't cross my mind about assigning a value by calling a method... Is that how you would word that? I think I need to re-write my vending machine program from scratch, but maybe this time it will work for a change!

Too bad I can't buy you a drink over the internet, lol.

Thanks again!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Hofmann wrote:A It didn't cross my mind about assigning a value by calling a method... Is that how you would word that?

I would word it as assigning the result of the method to a value. But you get it now and that's the important thing.

Steven Hofmann wrote:Too bad I can't buy you a drink over the internet, lol.

If you have the opportunity to link to the ranch, it's a great thank you to all the helpful folks here. (see the social networking icons on the bottom of coderanch.com for an easy way to do so.)
 
Matthew Mellott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into variable scope. The i in your main method and the i in the other class exist in two completely different scopes based upon where they are declared.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!