Console Based - Angular Motion Modeling
This week in my physics class I was learning about angular motion and the various equations associated with it. So, I thought it might make a fun project to make some code that integrates everything I've learned, so far. I chose to do this because I wanted to use object oriented programming in a project as well as wanting to learn how to do more complex calculations in C++ (such as square root ect) and I also wanted to get a feel for how to arrange equations in code.
Angular motion is very similar to regular motion in the manner that the equations of motion can be used if there is a constant acceleration. So I wrote down all the equations that I wanted to include in the program.
These equations can also be rearranged to find any of the values included but I haven't bothered writing the rearrangements down.
The code
In all my other console-based projects everything I've written has all been in one .cpp file but since I have created a class I wanted to spread declarations and definitions over different files. We will look at that after what is in the "main()" function.
The first part of the code is nothing special, nine different "float" variables are created and the user is given the opportunity to assign each of them a value. I chose to use "float" variables rather than integers because otherwise numbers wouldn't be allowed to be decimal at all and that isn't much use with circle equations, or many equations for that matter! If the user doesn't know the value of one of the variables they are told to assign it the value "-1" I thought this would be a good choice for a unknown variable because these numbers will not become negative (I know some are vector quantities but I am not associating direction with this particular program).
Next, a for loop is created. The reason for this will become clear after we have looked at the class functions. Inside the for loop an instance of the class "angular_motion" called k is created and all the values from the initialization step are passed to the constructor, known or not. Now the program tests which variables are "known" and which are set to -1. If a variable is set to -1 then the corresponding function is called from the "angular_motion" class. The return value of the function is then assigned to the variable. If that variable no longer equals -1, after the function has been called then is is printed to the screen. This process is repeated on all the variables 3 times though for reasons I will explain shortly. (note in the photo not all the if statements are included but this does happen to all the variables).
angular_motion class
All the declarations for the angular_motion class are kept in a separate header file (angular_motion.h) the code looks like so.
The class has 9 different functions, of type float, and a constructor. It has 10 different float variables, the extra one being pi all stored in the private section. The constructor, in the definition, takes all of the variables from the "main()" function and copies them to the corresponding variable in the class. Since there is no scope between class variables and functions the "OBT_" (OBT just means obtain) functions do not need to take in variables. Lets take a closer look at one of the functions, as defined in the "angular_motion.cpp" file.
If the program doesn't know enough variables to do any of the equations then it simply returns the value -1 and the program will work out the others. Since there is a for loop it will come back to the function after hopefully knowing other variables and should be able to complete an equation.
Here's another function definition. If the program "knows" the value of "radius" then the equation area = pi*radius^2 is carried out but if not the program will continue to learn the other values (hopefully one of them is radius) and will come back to the area function and be able to "complete" it.
Downloads
I liked this project because it was more challenging than the others I have attempted and required me to use OOP, which is a habit I would like to get into for future programs. I used different files in one program for the first time, I used the header file <math.h> for the first time, I used classes outside of practice for the first time too.
The source files are available for download here. Thanks and until next time.