• Post Reply Bookmark Topic Watch Topic
  • New Topic

Physics Calculator  RSS feed

 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, hopefully I will have some luck on this forum and someone can help me get past this issue: There are a lot of variables in physics and it is easy to manipulate equations to find the unknown variable. Well, in my program I have the problem of determining which equation to solve for an unknown. You can see I have a timeF, timeI, time, distanceI, distanceF, distance...etc...variables. The way that I have written the code, it will scan for an empty() JTF and assign it as the unknown variable (not sure I can do it this way anymore). At that point, the value is passed into the correct calculation method to process the equation based on a series of case statements in each method. Can someone please help me figure this part out. I think once I get past this issue...the rest will fall into place.

I know the button arrays I have set up will process correctly in the checkJTF method...and I will need to account for that...but right now my focus is on getting the program to properly calculate the kinematics equations and display the result in the JPanel.

NOTE: If you see any other problems, please let me know so that I don't venture too far off the beaten path.

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Derek, welcome to the Ranch!

The main problem I see there, I'm sorry to say, is that you have posted 500 lines of code. And you haven't told us what part of the code you're asking about. It would be much more practical if you posted a much smaller piece of code to demonstrate the problem. For example I don't think you need a whole lot of buttons to do that.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! Yeah, I know you guys are busy and its probably a pain to read through so much code and I just wanted you to be able to see it in its entirety. I will post the snippets of code that are the problem areas.

In code 1: This will calculate the input from the user that was entered into the JTextField. I have a checkJTF method that will scan the array for an empty JTF and assign it the variable unknownVar. As you can see in the calculate ActionListener you can see it will pass the unknownVar through some if/else arguments to determine which calculate method to call. The problem I have is what to do when more than one JTF in the array is left empty -- meaning more than one unknownVar. The physics equations have different variables depending on which equation, in addition to them being manipulated any number of different ways, and I'm really not sure how to account for this in code.
Hopefully this is a little easier to see specifically what is the problem.





 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:The problem I have is what to do when more than one JTF in the array is left empty -- meaning more than one unknownVar.


Then the question is, what do you want to happen in that case? Your design seems to assume that only one of them will be empty, since you only use a single variable to say which one is empty, but perhaps that's a design error? Think up some examples and see if you can fix your design for a start.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:Yeah, I know you guys are busy and its probably a pain to read through so much code and I just wanted you to be able to see it in its entirety. I will post the snippets of code that are the problem areas.

Still quite a bit, but I'll try to give you a kick-start:

Your "calculator" seems to be very tightly coupled to your GUI code and that's almost always a bad way to go. At the very least I'd suggest a Calculator or EquationSolver class that has absolutely no GUI code in it at all. It should work only with numbers or formulae and spit back a result. And if you design it well, you may well be able to subtype it to solve different equations (eg, quadratics or integrals), because that's how Object-Orientation works.

However, in order to do that, you need to first understand exactly WHAT your "calculator" needs to do. Right now, you're worrying about frames and buttons and panels and user interaction, and they are simply not important to the task of writing an equation solver.

Get back to basics: What is this "calculator" of yours supposed to do? And forget about screens when you're trying to describe it. What information does it need, and what is it supposed to produce?

HIH

Winston

PS: Your code lines are far too long, and it makes your thread very difficult to read. I'd break them up myself, but you have tons of them, so I suggest you do it yourself. Or, at the very least, read the link before you post a new thread. Thanks.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further to what Winston said about the calculator, maybe you can consider using the Factory design factory to instantiate the proper calculator instance given what variables are known/unknown.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey. Thank you for replying to my post. I will try to make a better quality post next time.

When I originally started designing my code, I had setup classes for each topic (kinematics, dynamics..etc.). I created the JFrame and its components (JButton, JLabels, ActionListeners..etc) within this class. I knew it would help keep everything separated and easy to read and access. Somewhere along the lines when I tried to actually use the equations from each class, I ran into issues passing the variables and creating the object ("calculate"). Instead I resorted to keeping it all in the same class, since I was having plenty of other problems at the time it seemed like the best choice. This is one of two things I am very disappointed with in the code.

As far as calculating for different equations (the 5 kinematics equations, 4 or 5 dynamics..etc), I stuck with just one equation for each topic. Trying to account for more than one empty JTF array (or variable) in the checkJTF method was a problem I just couldn't figure out. Do you guys know how I could have fixed that issue? I completed the code last night after choosing to just solve for one variable using one equation. I plan to do more work on this code and would like to improve upon it, so I will likely go back to try and put each (topic)Calculator method into its own class to start with. But I still need to figure out how to use more equations than one and solve for more than one unknown.

Thanks a lot for the replies.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question, when I create a class for each calculation topic, where should I create the object for that class? Should I create it in the main method or within the GUI class...? I think this may have been why I couldn't get it to work properly last time.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:As far as calculating for different equations (the 5 kinematics equations, 4 or 5 dynamics..etc), I stuck with just one equation for each topic. Trying to account for more than one empty JTF array (or variable) in the checkJTF method was a problem I just couldn't figure out. Do you guys know how I could have fixed that issue? I completed the code last night after choosing to just solve for one variable using one equation. I plan to do more work on this code and would like to improve upon it, so I will likely go back to try and put each (topic)Calculator method into its own class to start with. But I still need to figure out how to use more equations than one and solve for more than one unknown.


Imagine you can calculate multiple unknowns in your "calculateXXX" methods. Passing in the unknown variables would just be passing in an array of ints or varargs of ints. The real problem is inside the calculateXXX() method. I see your current approach is using an if statement then case inside. If the passed in parameter is array, the if statement can easily turn into a for loop.

Anyway, I'm no physics or math guy much but I'm sure there are formulae for solving such equations more easier than whole bunch of case statements.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
Imagine you can calculate multiple unknowns in your "calculateXXX" methods. Passing in the unknown variables would just be passing in an array of ints or varargs of ints. The real problem is inside the calculateXXX() method. I see your current approach is using an if statement then case inside. If the passed in parameter is array, the if statement can easily turn into a for loop.

Anyway, I'm no physics or math guy much but I'm sure there are formulae for solving such equations more easier than whole bunch of case statements.


Yes, I see what you mean by passing the array of variable integers into the calculate method, but once inside this method, the problem of choosing which variable can be solved for by a variety of equations. Each equation depends on the known data and each equation can be manipulated to solve for different variables. I mean there are at least 3 different equations that can be used to solve for distance, but if a velocity variable isn't given, then I need to first find the right equation to used to solve for velocity -- then go back and solve for distance. I'm just not seeing how I can do that in code.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using your distance example, not knowing velocity at first. Then your calculateDistance() method would look like:

Is velocity provided?
if Y solve distance directly
if N solve velocity then solve distance

Now I'm not suggesting you use bunch of if to find "if velocity is given". Now thinking more, maybe an array may not be the best input parameter, a map can be better. But to illustrate the logic in code using a map because with array, you would not know which element is what, assuming these calculate methods are NOT GUI dependent:



Now you may noticed there will be repeated code for "solving directly" part. In OO terms, reuse is key. Maybe you can have many methods calculating distance given the known variables instead. This will then become:




Now this surely looks lengthy depends on how many variables you can have for solving distance. But hope this helps with your design approach.
 
Derek Smiths
Ranch Hand
Posts: 119
Eclipse IDE Mac VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I definitely see what you are saying here (thanks for including code, helped to clarify your point) and this could very well be the answer to solving my dilemma. I've never used mapping before and we never covered it this semester. I will definitely look into using mapping though. My only concern with doing it this way and it could be that I don't fully understand mapping, but the distance equations don't rely on a single variable (velocity).

I know that the program will need to verify enough data field (JTF) include data in order for any equations to work, but in addition to that, if any two of the three (distance, velocity, time) variables are not entered there is not enough data.

Kinematic Equations that include distance:

(finalDistance - initialDistance) = ((initialVelocity + finalVelocity)/2 ) * (finalTime - initialTime)
(finalDistance - initialDistance) = initialVelocity * (finalTime - initialTime) + 0.5 * acceleration * (finalTime - initialTime)(finalTime - initialTime)
(finalDistance - initialDistance) = (finalVelocity*finalVelocity - initialVelocity*initialVelocity) / (2 * acceleration)

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:I know that the program will need to verify enough data field (JTF) include data in order for any equations to work

No, it needs to verify that it has only one unknown out of the three; and it shouldn't care less where those values come from. Indeed, if you want to set up a test rig for this, you may well want to supply values yourself (particularly for corner cases), rather than relying on someone keying them in every time.

In fact, I'm not even sure that it does need to verify that you have only one unknown because you could actually force this via a dialogue. For example, if your user chooses to enter velocity for their first two values, then they can ONLY choose either time OR distance for their second two; after which you invoke your Calculator class for the two sets of values that they did supply.

This is why I say that you've got to separate the GUI from your "calculation" logic completely, and stop thinking in terms of JTF's and JTables and JFrames - in fact, remove ALL types that start with 'J' from your thought process and think of this in purely algibraic terms.

A further suggestion: Take ONE equation (eg, the first one on your list) and tackle that; and don't even think about writing any others until you KNOW that it works. Every time, and with any set of values - including invalid ones.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derek Nickerson wrote:...I need to first find the right equation to used to solve for velocity -- then go back and solve for distance. I'm just not seeing how I can do that in code.

The first thing you need to ask yourself is "How do I do this on paper?" Before you write a single line of code, you have to have a solid understanding of the problem in your brain. Work through examples on paper. write out step by step how YOU would solve this - or even better, how a high school algebra student should solve these steps. Your directions need to be unambiguous, clear, and precise.

My advice would be to throw all your code away, and not write any new code until you have the above written out on paper.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!