Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a code to accept input numbers for calculation?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I'm a fresh learner of Java language.
I encountered problem in doing the following task:

'Write an applet that asks the user to enter two float-point numbers, and draws the sum, product, difference, and quotient of the two numbers.'

How should i write it? Do i need to write something like sending message to another applet? or else?

Thx in advance =)
 
Well I think this is your assignment...but anyways please be specific where you are having problem....

SR
 
Ostensibly you need an input field and a few labels for the input and output as well as a button or two. I imagine that your instructor has provided some framework for you to create an applet.

The important code that your are going to have to write is the methods to perform the calculations and to parse and convert the string input into FP values. This is all very straightforward.

As the previous poster said, what is your specific problem? Are you clear what the assignment is and what you are expected to do?
 
its very easy, check out this code:
Assume u have 2 textfields t1 and t2 which take in the double values, and a button 'b' to calculate. have an actionlistener for 'b' and in the method actionPerformed(), do this

double d1 = Double.parseDouble(t1.getText());
double d2 = Double.parseDouble(t2.getText());
then something like,
double d3 = d1+d2;

to display the results, have another textfield or label. suppose u have a textfield 't3' to show the sum of the 2 no.s, then u display the result as

t3.setText(d3);

mind u, the method parseDouble() will throw an exception, so put it within try-catch. this method actually converts the string from the textfield to double value. u can have more calculations for difference, product etc. and show them in other textfields or labels. that finishes ur assignment ;-)

luv
Karthik.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top