This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing values from one method to other

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i have two different classes. One for adding 2 nums and other to subtract. i used BufferedReader for user input. My requirement is based on the input given the program must call the method. For example: "add 1 2". This line contains add so the program must pass the two values i.e 1 and 2 to add method. If the line contains sub then the values must be passed to sub method. Can anybody please help me out. I have already diff classes to perform that operations, they are here.

The add class.


The sub class.
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it all in one class (although you could keep the add/sub parts in separate classes if you wish).

Your code isn't far away, you just need to think a bit more about how you structure it.

What code makes the decision whether to call add or sub?
Is this the same code that gets the user's input?
How many classes do you actually need? One, Two or Three?

Without knowing how complex your final program needs to be, I would say you could have one class that gets the user input in its "main" method, parses it and then calls add/sub as needed.
Like I said - you're nearly there.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are supposed to be learning object-oriented programming, don't do everything in one class. And don't do lots in the main method. What you need is a readFromKeyboard method. Then call add(int i, int j) subtract(int i, int j) multiply(int i, int j) and divide(int i, int j) methods. These latter methods should be in a different class, and can probably be declared as static, if they return their result.
 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to create a Main class
and call add and sub class from it.

You can do it like this
from command prompt run

>main add 5 6


In Main class type

# BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
# int a=Integer.parseInt(br.readLine());
# int b=Integer.parseInt(br.readLine());



and on main make comparisions like
if(args[0]=="add")
call add class's method and pass a & b to it
else if(args[0]=="sub")
call sub class's method and pass a & b to it

i think this way your problem got solved

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bhavesh bhatnagar wrote:
and on main make comparisions like

i think this way your problem got solved


You probably don't want to recommend that the original poster compare Strings with == since this doesn't check that two strings contain the same letters but instead checks if they are the exact same object.

Instead compare Strings with a call to the equals(...) method or equalsIgnoreCase(...).
 
bhavesh bhatnagar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks pete

i forgetted that method

You should use equals() instead of "=="
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
and thanks for your replies.
I have tried doing like this

code: for main class


code: for required methods(calc class)



I needed to give the whole output in a single line. How can i achive it.

Thanks and Regards
alexander
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For single-line output, have a look at System.out, which is a PrintStream, and look through its methods.
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry guyz. My question was wrong. I need to give the total input in a single line. As per the above program the user has to enter first the operation i.e add,sub or mul, the the 2 numbers are entered in two different lines. So instead of all that i wan to give the input in a single line just like this : add 1 2

How can i do that.


Thanks and Regards
alexander
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code see readline (this will read an entire line) where you assume there will be a string with an operation and then read two different lines with int's - so you will need to change that - what if the first read, read in the operator and the two ints into a string and you parsed that? IE: split on a space character.



 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
i know that i can split and do and all those operations. But isn't there any other way or method to full fill my need.



Thanks and Regards
alexander
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic