Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 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.

Notices

 12-12-2003, 10:36 PM #1 Laptop2250 Member   Registered: Oct 2003 Posts: 131 Rep: 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 Code: ```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 Code: ```/** 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"+ "1.Add\n"+ "2.Subtract\n"+ "3.Mutiply\n"+ "4.Divide\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.
 12-13-2003, 12:45 AM #2 ajk Member   Registered: Nov 2002 Location: CH-3416 Affoltern Distribution: (SuSE 8.0), Mandrake 9.2 Posts: 58 Rep: 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 greets ajk
 12-13-2003, 12:46 AM #3 coolman0stress Member   Registered: Jun 2003 Location: Toronto, Ontario, Canada Posts: 288 Rep: 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: Code: ```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: Code: ```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: Code: `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: Code: `result = MIrrational.add(t,z);` Hope this helps.

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post kpachopoulos Programming 2 09-19-2004 04:08 AM Icon Mandriva 7 11-01-2003 04:14 AM Nasty Linux - Newbie 1 10-11-2003 04:29 PM odious1 Linux - Newbie 4 07-10-2003 11:11 PM rob_roman23 Linux - General 6 08-01-2002 10:10 AM

LinuxQuestions.org

All times are GMT -5. The time now is 05:09 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -