• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

extreme beginner question

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this may seem stupid, but I'm stuck. I'm trying to learn how to create methods, so I thought I would start with a simple example of adding 2 numbers together. However, my code won't compile and I don't know enough about Java to figure out why or what I'm doing wrong. Could someone please take a look at my code and just give me a hint on what I need to change? Thanks.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check your parentheses. It looks like your main is outside of the class.
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it's inside. Thanks, but any other ideas?
 
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took the liberty of giving you more than a hint. I hacked up your code to make it work, hope it's useful:

You probably want to get some free stuff to walk you through this junk, Check out Sun's tutorial @ http://java.sun.com/docs/books/tutorial
Have fun,
Michael
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very very much. It's frustrating to work with the book and get their examples to work, but then to try something on my own and it not work and the book doesn't explain enough for me to figure out why. Thanks again!
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know exactly what you mean, been there. Stick with it though, you just had some very basic problems that will make sense after you understand how Java works.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tyler,
The code you posted had several problems... the solution I am posting is only one of several possible solutions, and I hope it does what you originally intended :
<code>
class AddNum {
private static void SumReturn(int d, int e) {
if ((d > 0) && (e > 0))
{
int f = d + e;
System.out.println(d + " + " + e + " = " + f);
}
}

public static void main(String args[])
{
int d = Integer.parseInt( args[0] );
int e = Integer.parseInt( args[1] );
AddNum a = new AddNum();
a.SumReturn( d, e );
}
}
</code>
Hopefully the code above came out and does not look like gobbledy-gook... Anyway, lets go over the changes...
1.) I moved the integer declarations of d and e into the main() method. I did this because the values in args[] are not available until after you call these values "args" in main.
2.) Next, I had to add a return type to the SumReturn method. All methods need some sort of return type even if they don't actually return anything, so I made this method "void" - a special return type which means that this method doesn't return a value. (You could have had it return the sum of the two values just as easily, by making the return type "int" and putting "return f;" in the method, but since you were printing out the values inside the method this didn't need to be done.) By the way, this method doesn't have to be static since you create an instance of this class in main. ( This probably doesn't make sense now, but it will as you learn more about what "static" does.)
3.) I took the "return this;" line out of the SumReturn method. Since I changed its return type to void, it does not need a return statement now.
4.) In the main method I changed the line "a.SumReturn();" to "a.SumReturn( d, e );". This passes the variables d and e into the SumReturn method. This is needed since the SumReturn method expects two integer values to be passed into it, since the method was declared as "private static void SumReturn( int d, int e )".
As you practice more Java programming you will learn more of this, but I hope this helps you out!
- Nathan
P.S. - Dang! Michael beat me to the explanation! (and I forgot to explain the bit about concatenation, too... )
[This message has been edited by Nathan Pruett (edited December 05, 2000).]
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> Dang! Michael beat me to the explanation!
Yes, but you did a much better job of explaining
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nathan Pruett:
(You could have had it return the sum of the two values just as easily, by making the return type "int" and putting "return f;" in the method, but since you were printing out the values inside the method this didn't need to be done.)


I'm not sure I understand how to do this. Could you give a simple example?

By the way, this method doesn't have to be static since you create an instance of this class in main. ( This probably doesn't make sense now, but it will as you learn more about what "static" does.)


Correct me if I'm wrong, but aren't you supposed to set something to static so that it's only available inside the class itself...or am I thinking of something else. If so, what is the purpose of static? Thanks again for all the help. I really appreciate it
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, I figured it out
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tyler,
Here's an example of how to make your function return an integer value :
<code>
class AddNum {
private int SumReturn(int d, int e) {
if ((d > 0) && (e > 0))
{
int f = d + e;
return f;
}
else
{
return 0;
}
}

public static void main(String args[])
{
int d = Integer.parseInt( args[0] );
int e = Integer.parseInt( args[1] );
AddNum2 a = new AddNum2();
System.out.print( d + " + " + e + " = " );
int f = a.SumReturn( d, e );
System.out.println( f );
}
}
</code>
See, now SumReturn is defined as "private int SumReturn( int d, int e )", and inside the method we either return f or we return 0. This is another thing to notice... For methods with any return type other than void you must return something of the return type specified. Also, instead of simply calling the method, you must make use of the value the method returns... In this example I put the returned value into a variable named "f", but I could just as easily dumped it directly into the println statement (like this - "System.out.println(a.SumReturn( d, e );" ). You can, of course, ignore the return type and just run the method, lots of programmers have used int return types to indicate if an error occured in the method, and the returned value was secondary to what the method did. ( This was used more in C... Java has better error handling, and you shouldn't have to do this anymore...)
By the way... a return type can be anything that Java recognizes... primitive types (int, float, char, etc.) or objects like Strings, Images, or any other default or user-defined classes.
Anyway, to move onto the static keyword... the easiest way to describe what it does is to view your class as a plan for the type of objects you will be creating with it. static is a keyword which means that there will only be one copy of the variable or method it is applied to that all objects created from that class will use. For example, if you had a class that had a static int x, and then you created two objects of that type, a and b, and then you said: "a.x = 10;" this would automatically make b.x = 10. The easiest way to visualize this is to think of the static variable or method as being "shared" among all the objects of that type. If one changes it, it changes for all of them...
Anyway, that's probably more than you wanted to know about the static keyword...
Hope it helps,
Nathan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic