This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Help Creating Class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I am having trouble with this.
> If someone could help me and give a reason behind their answer,...

it is far better for you to describe the trouble you are having.

post the code you have tried, and 'what you get' vs 'what you expect'
[ October 08, 2006: Message edited by: Michael Dunn ]
 
Cortney Parsons
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I am able to write the code up to giving the int a and int b a value

show us your program that does this much, and we'll try to give you
a nudge in the right direction for the rest.
 
Cortney Parsons
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you try to compile and run the program -- meaning what is the exception that you are getting?

As a hint, take a look at the signature of the main() methods of other programs. Notice how is it different from yours.

Henry
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic