In my study book it wants me to create a class named Numbers whose main() method holds two integer variables. It wants me to create two additional methods,sum() and difference(), that compute the sum of and the difference between the two variables, once I have assigned values to the variables. I am having trouble with this. If someone could help me and give a reason behind their answer, that would be great.
The trouble that I am having is I am able to write the code up to giving the int a and int b a value and then I seem to get very confused on the rest. The part about having it add and subtract the values is getting me stuck. [ October 08, 2006: Message edited by: Cortney Parsons ]
1) public static void main (int a, int b) assuming you can get it to compile OK, it won't run because your main() does not match the method signature required to start a program. There should be plenty of sample public static void main(...) in your book, so check how the method should be written.
2) int a=10, int b=8; you cannot specify 'types', separated by commas (even if they are the same) either separate them by a semi-colon, or remove the 'int' from b
3) if you fix (1) you won't have this problem, but at the moment you have 2 x local variables named a, and 2 named b - these will generate "already defined errors"
4) class numbers technically won't affect the running of a program, but Sun's naming conventions have classes starting with a capital letter i.e. Numbers, and following the conventions is a good habit to get into.
if you fix 1 and 2, you should have your opening requirement, a program with 2 int variables (doubtful you are required to pass them to the program as arguments).
from there you want to create and call a method to add the 2 numbers, so your method will look something like this
accessor|return type|method name|arguments
accessor: public private etc return type: String int double etc, can be void method name: in this case sum arguments: can be no arguments () or one, methodName(float f) or more, mthodName(float f,double d)//here is where 'types' are comma-separated
if, in the signature, you have a non-void return type, you must include in the method a return statement that returns a value of a type that matches that in the method signature e.g. if your method signature starts public String...... you must include in the method
so, for your sum(), you would pass to it the 2 arguments of the a and b variables in main(), then within sum() you would create another variable to represent the sum of the 2 arguments, then return the 'sum' variable.
It is important to understand that sum() will be working with the argument names supplied in the method, not the names of the variables passed to the method e.g. it might seem easier for you to write this
but using the same variable names could confuse you when it comes to variable scope. the variable names of a and b here sum(int a, int b) could be any name at all sum(int x, int y) which means //add up a and b becomes //add up x and y
you want to use the value in main(), so it is easier to create a variable to hold the return value from sum() int a = 8, b = 10; int numbersAdded = sum(a,b);//note - you only pass the name, not the type.
now you have the added value in main() and you can do anything you want with it, print it out, to check accuracy, use it in another calculation etc.
repeat above for difference(), and you should just about have it