View the Most Wanted LQ Wiki articles.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-12-2003, 10:36 PM   #1
Registered: Oct 2003
Posts: 131

Rep: Reputation: 15
java -- methods -- please help

Hi, I am currently writing a small calculator program, and I would to have some help in creating +, -, *, /, to be in methods.

This method is created in class MIrrational
public Irrational add(Irrational t, Irrational z)
{   return (z+t);
I do not understand how to make this method work from another class called MainIrrational

The Calculator will be implemented
public class Icalculator
{ public static void main (String []args)
    {EasyReader n= new EasyReader();
     int choice;
     System.out.print("Select a number for operation (1,2,3,4,5,6)\n"+
                      "Your operation choice is: ");
     choice= n.readInt();
     if (choice==1)
        {System.out.println ("Enter irrational numbers in the format:x,y");
Please tell me what the next line should be to make the program to call the addition method. I know that simply doing this without methods would be easier, but I am learning methods. Please respond soon.
Old 12-13-2003, 12:45 AM   #2
Registered: Nov 2002
Location: CH-3416 Affoltern
Distribution: (SuSE 8.0), Mandrake 9.2
Posts: 58

Rep: Reputation: 15
For this kind of calling add, you have to declare it public static Irrational add(Irratonal t, Irrational z){}

This means, you call a Method of the CLASS.

Or you could it use object-oriented, this would mean, you would tell one Irrational to add a given other one:

public Irrational add(Irrational t){
//if you want directly to use this one as result, you can do that:
this.value = this.value + t //depending how you store the values...
//or you can return a new value
return this.value + t.value
} //add

this means always the active object, on which a method runs

I think, first is what you want, second could possibly help to look a bit further... ;-)

hope it helps


Old 12-13-2003, 12:46 AM   #3
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
How is the Irrational class setup?

I'm assuming it's something you created yourself, since i've never personally encountered it.

If Irrational is another class you created, there really isn't much use in it since you can just use doubles (or BidDecimal for higher precision) instead directly.

So instead i will answer your quesions using doubles for simplicity:

In your MIrrational you have this:
public double add(double t, double z) { 
  return t+z;
Usually the form you would use to call this method is by creating an instance of the object that contains the method first. Like this:

MIrrational myObject= new MIrrational();
double t= 1.2;
double z= 2.3;
double return = myObject.add(t,z);
So in your case you would need to read 2 doubles and then pass 'em to the function and hold the result for display to user.

Another approach would be to have the add method in the same class as your main. Then you won't need to actually create an instance of the object and will just be able to do "add(t,z);".

Finally, if you must use a method from another class, then you can make it static, by doing:
public static double add(double t, double z) { ... }
This time you would need to specify where the method is coming from, but don't need to create an object of that type:

result = MIrrational.add(t,z);
Hope this helps.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between Java compare and compareTo methods kpachopoulos Programming 2 09-19-2004 04:08 AM
Other install methods? Icon Mandriva 7 11-01-2003 04:14 AM
Boot methods Nasty Linux - Newbie 1 10-11-2003 04:29 PM
search methods odious1 Linux - Newbie 4 07-10-2003 11:11 PM
Logging methods rob_roman23 Linux - General 6 08-01-2002 10:10 AM

All times are GMT -5. The time now is 01:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration