GravityCalculator.java.xhtml 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 'GravityCalculator.java',
  21. 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-04-12',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Source code:</h2>
  25. <blockquote>
  26. <pre><code>/*
  27. * Copyright (C) 2017 Alex Yst
  28. *
  29. * This program is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  41. */
  42. package gravitycalculator;
  43. import java.io.*;
  44. /**
  45. *
  46. * @author Alex Yst
  47. */
  48. public class GravityCalculator {
  49. public static void main(String[] arguments) {
  50. double gravity =-9.81; // Earth&apos;s gravity in m/s^2
  51. double initialVelocity = 0.0; // starting velocity of the object
  52. double fallingTime = 10.0; // time in seconds that the object falls
  53. double initialPosition = 1000.0; // Starting position in meters, the calculation will determine the ending position in meters
  54. /*
  55. In this equasion, we multiply by fallingTime twice. Why? Because we need to
  56. square it. Java lacks an operator for exponentiation, and calling a function to
  57. do it seems like overkill.
  58. ...
  59. For the record though, if we wanted to use the function to do this, Math.pow()
  60. would be a good candidate.
  61. */
  62. double finalPosition = 0.5*gravity*fallingTime*fallingTime+initialVelocity*fallingTime+initialPosition;
  63. System.out.println(&quot;The object&apos;s position after &quot; + fallingTime +&quot; seconds is &quot;+finalPosition + &quot; m.&quot;);
  64. }
  65. }</code></pre>
  66. </blockquote>
  67. <h2>Output:</h2>
  68. <blockquote>
  69. <pre>run:
  70. The object&apos;s position after 10.0 seconds is 509.49999999999994 m.
  71. BUILD SUCCESSFUL (total time: 1 second)</pre>
  72. </blockquote>
  73. END
  74. );