• Post Reply Bookmark Topic Watch Topic
  • New Topic

calculate 2 number always equal 0.00  RSS feed

 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i coding a java  bean for a simple program, i ask of my bean to return a computing with 2 number, but i don't know the result  !
it always equal to 0.0.
this is my bean 

this is my servlet i use dopost



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IN your doPost() method, you don't assign resultat a value after you set it to 0. Remember that Java doesn't update primitives when you pass them as parameters. Which means passing resultat to the constructor isn't helping you.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:IN your doPost() method, you don't assign resultat a value after you set it to 0. Remember that Java doesn't update primitives when you pass them as parameters. Which means passing resultat to the constructor isn't helping you.


can i do them because if i'm not initialise the eclipse do a mistake and ask to initialise variable
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the skills needed for Eclipse users is knowing which of its warnings to believe, which to ignore, and which to change. I think this is one to change. I would get rid of the resultat field altogether, because it is not primary data. It is secondary, being dependent on the values of other fields.More options:-
You might use a similar mechanism to the calculate method in that getResultat method. You might use a switch statement instead. You might use an enumerated type, which you can read about in the Java® Language Specification (=JLS) or the Java™ Tutorials. (Beware: the JLS can be difficult to read.)

You said that is a bean class. In that case, you need to be careful about spellings. It sh‍ould read getResultat; the capital letter is part of the bean specification. The symbols +-*/×÷ etc are not called operands; they are called operators. In the expression 1 + 2 the operands are 1 and 2 and + is the operator.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie,
after reading your informations that you are given me, i change my code of my Beans principaly, i anderstand about constructor !
but my issue are still again !
this is my bean :


this is the servlet :



I have personaly issue of the patern MVC, also the jsp is the view, the servlet is the controlor and the model is the beans but i don't understand about mecanics between the controlor and the model, for example if contolor to inherit of all the public code of the model i think yes.  


Regards
philippe
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your output show, from those print() calls?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought it always shows the quotient, because there are important parts missing from the switch statement.
Yesterday, I wrote:
And later yesterday, you wrote:
Apart from the return type, which I think is incorrect, and the missing parts of the switch statement, there is another difference between the two . . . 

Method Names Sho‍uld Not Start With Capital Letters.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i do like this for the switch !



But the constructor after compilation set up the initialysation of the variable, i don't anderstand why when i invoque the class Calculer it doesn't change the value.
i must can change the value of resultat !
how can i do for in the servlet call new class (invocation) for compute ?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch. That code looks horrible. I suggested private access for a reason. You don't want that method called from outwith your object. Putting a calculer() call in the getResultat() method means that resultat is always updated when it is required. The calculer() method does not need to return anything, so the multiple returns are unnecessary. The combination of return and assignment in line 24 is even worse. What is more, that line 24 is probably never reached, because of your inappropriate design of the switch statement.

Please go back to the Java™ Tutorials and remind yourself of the usual structure of a switch statement.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . Putting a calculer() call in the getResultat() method means that resultat is always updated when it is required. . . .
There is another thing. You can create an object and call some method calls on it:Note that class names shou‍ld not be verbs. Calculateur not Calculer please. And remember that method names shouldn't begin with Capital Letters. There are good reasons for those conventions. You have a potential error because your constructor and method have the same name.

Get a sheet of paper and write the method names and field names on it. Give them some values. Call the methods in the order I showed you here, but using your pencil, and change the values of the fields. Does every possibility give resultat the value 2.89373?
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ritchie,
First thank you for your help, i understand better with code examples, so i made the changes that you indicate to me,
i must tell you also that i take a course of java EE, with videos and an associated tutorial To the latter.
Here are my results:
With the parameters set num1 and num2 as you tell me!




output on the brouser
0.0
2.89373
2.89373
le calcul de : 2+ 4 a pour total : 0.0


Now I have problems with getters and setters, I can not read the value of my textbox,
yet in the output I see the numbers read from the jsp, I think I have some problems with my beans,
(This is the first time I write a bean), to tell you, I would like you to read my bean to tell me if you want my errors



Regards
philippe

 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philippe Ponceblanc wrote:. . . i take a course of java EE, with videos and an associated tutorial . . . . Now I have problems with getters and setters . . .
So what problems are you having with  getXXX and setXXX methods? Your setXXX and getXXX methods in the latest code block look correct, but the calculate method still has the problems I told you about last week not corrected. I would say that setXXX and getXXX methods are basic things, which you shou‍ld know about before trying JavaEE programming.
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Philippe Ponceblanc wrote:. . . i take a course of java EE, with videos and an associated tutorial . . . . Now I have problems with getters and setters . . .
So what problems are you having with  getXXX and setXXX methods? Your setXXX and getXXX methods in the latest code block look correct, but the calculate method still has the problems I told you about last week not corrected. I would say that setXXX and getXXX methods are basic things, which you shou‍ld know about before trying JavaEE programming.


Hi Ritchie,
Last week you talked to me about it's getters and setters methods, i've done java with drjava and i'm never encountering this type of method. you've told me that algorithm start with method of Calculating but I find the example not long enough, I do not ask you to write me the whole project, but please you can complete your example with more code!
Here is your code:




So that I assimilate the course of JEE that I am, in more it is complicate to find a course of jee in French, I give some money for this course which and made very well, I hoped develop skills on jee that I find good, in more I develope under Linux (debian) and I carry my projects under BSD (FreeBSD, NetBSD, OpenBSD). Now to understand and of make a know-how transmission I hoped not to be too naïve face has your expertise.I think that to understand fast, the time is account, if you can developper your way of coding getters and setters: an example is more speaking that to discuss.


Regards
Philippe
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's DrJava? Is it an IDE or is it a tutorial? If it is an IDE, then it is irrelevant to your current problems.
I think you shou‍ld forget about JavaEE for the time being because the problems you are having are very basic problems.

You shou‍ld have been taught about getXXX methods and setXXX methods from the very beginning. But I don't know why you are going on about setXXX and getXXX methods; as I told you earlier those methods are all right. What you have done is misunderstand how the resultat field gains its value. I made a suggestion and gave you (incomplete) methods to calculate it and you ignored those suggestions. You are going to have to think about what will happen if you use the following calls:-There are all sorts of ways you can calculate numbers, but the computer is quite capable of doing the wrong calculation if you program it wrongly. What is going to happen if you omit my line 3 above? What resultat will you get?

As Fred Rosenberger says frequently, programming is about thinking. Those of us who are more experienced can see that sort of problem much more quickly. We have done lots of object‑oriented programming and can envisage the program flow more easily. Only because we have done it before. Did your course not teach you about the values of fields? Didn't it teach you about calling methods? Did you miss those topics or ignore them when you saw them because they seem boring? Those are basic things without which you cannot program correctly. If they don't appear early in your course, consider whether you have chosen the wrong course. There are some good courses on the net and some bad courses, and we don't have the time to assess them all.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DrJava is a lightweight IDE.  It would have nothing to do with the type of methods the OP has seen.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Philippe Ponceblanc wrote:
It would seem that your result is transient and is based on the content of num1, num2, and operand (should be operator). I would do away with your 'resultat' field because it will often be out of date as you call the various setters. Have only one of getResultat() or calculer() (should begin with lower case) that returns the result but does not store it. Your current approach of storing resultat is getting you into trouble. Also you should not have a setResult() method.

A toString() method might help here
 
Philippe Ponceblanc
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi campbell Ritchie, kute Snortum and Carey Brown,
some one can tell me a link of tutorial can i read In addition to my course !

Regards
philippe
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the Oracle tutorials on Java:

https://docs.oracle.com/javase/tutorial/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!