• Post Reply Bookmark Topic Watch Topic
  • New Topic

Math Equations  RSS feed

 
Jason Koonce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again everyone. I recently completed an assignment where I had to create a mathematical equation and assign it to a button. I did complete the assignment, but I was wondering if there was a tutorial or somewhere I can research better ways to convert a mathematical equation to Java code.

I have gone through discrete mathematics classes, tutorials on the Oracle site ( was sun then of course ) and the current text book for class as well. I just want a little more guidance or instruction in best practices or techniques. I don't want to spend another several hours working on a calculation conversion that I can do in my head but can't get written out into code too well. I am still learning Java, so i want to spend more time learning code than translating. I understand the fundamentals of mathematics in coding, just not formula conversions as well.

Thanks everyone.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a little vague, don't you think? A "mathematical equation"? There really aren't any "best practices" for that. But perhaps if you started by asking about a particular equation which boggled you, we could talk about that.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me make a guess; if the original poster can please confirm that this is what he means, or that something else is what he means, we can help him. If he's new to posting in forums as well as to programming in Java, then framing the question may be something he (or she) could use some help on as well.

A reasonable school assignment might be "allow a user to enter a mathematical equation as a string and evaluate it when he clicks a button". One could have a space for the user to type in an equation, and a button that parses that equation and provides an answer.

I repeat, I don't know that this is what the OP meant, but perhaps it will help get him/her clarify the question.

rc
 
Jason Koonce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies everyone. What tends to happen is, like my last assignment, we are told to create a GUI that a user enters in a Car Cost, Down Payment, a radial button for an interest rate, and number of years financed. Then create a button to calculate the monthly payment as well as the total amount paid with interest over the life of the loan.

I knew the formula for calculating a car loans monthly rate, but in written form. Is there a tutorial or some sort of guidance that I can look into for converting it from a regular formula into a Java code algorithm or formula?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think I asked the right question anyway. I thought your assignment was much more complicated.

I recommend you look for Java tutorials on "operators"; an operator, in general, is a symbol in the language that takes one or more values and does something with them to produce another value. Java has operators for the standard math operations, which is all I think you will need. There are also math "functions", which are methods that do similar, usually more advanced operations.

If, for example, you want to add two things, you would write a java statement such as;

The obvious operator is "+", and it does what you would expect it to do with y and z. After this statement, x will hold their sum.

You will also need to know about Java numeric types; int, float, and double. You can also look up more detailed explanations of those, but basically int (meaning integer) variables only hold whole numbers, and float and double variables hold "real" numbers, i.e. decimal numbers with whole and fractional parts. So you declare variables of these types with statements such as:

and then use them in statements like the one above.

You can also assign "literal" values to your variables; a statement such as "y = 5.5;" assigns that literal value to the variable y.

Is that a start on what you needed? This says nothing about how you get the values the user has entered, so I hope someone has covered that for you elsewhere.

rc
 
Jason Koonce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In truth, the simple ones I have no problem with, it is the more complicated formulas that I have a problem with. A car loan repayment calculation is fairly simple, but it got me to thinking about more advanced mathematics. What will I do when I have to solve a problem that requires a hundred and eighty pages of graph paper, ten college rule notebooks and a PHD in 17 types of math just to read the problem? OK, that's exaggerated a bit, but I think you see where I am going with it. As they get more complicated, the formulas that is, the more I will need to know about how Java handles these things. I was just hoping for a little more advanced tutorial, if there is one.


Thanks.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are college courses on this general subject ("Numerical Computing"), but without more specification it is difficult to know where to point you. Some problems are easily solved by available math libraries, some require iterative techniques, some are still in the process of being proved computable at all. It doesn't sound like there's a simpler answer to your larger question.

rc
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Koonce wrote:In truth, the simple ones I have no problem with, it is the more complicated formulas that I have a problem with.


I think Ralph covered all you need to know quite well. I don't see why complicated formulas should be that much more difficult, after all they are just built up from simple formulas. Much the way that a complicated computer program is constructed from simple pieces of code. You don't need to have advanced Java knowledge to write a complicated computer program, and likewise you don't need to know any more about how Java does arithmetic (or whatever you meant by "how Java handles these things") for complicated formulas than you do for simple formulas.'
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason,

Take your mathematical equations and make necessary literals in your code and try to put mathematical relations ie +, -, % etc and form the mathematical equation you need to implement.
 
Jason Koonce
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thank you everyone. I appreciate it and will make a better effort at being more specific in the future as well. Take care and Happy New Year!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!