Free AI web copilot to create summaries, insights and extended knowledge, download it at here
3178
Abstract
7d">Dart</h2><p id="2594">The console Dart only version I wrote when I first had the idea is:</p><div id="8ea5"><pre><span class="hljs-keyword">import</span> <span class="hljs-string">'dart:math'</span> <span class="hljs-keyword">as</span> <span class="hljs-built_in">Math</span>;</pre></div><div id="b136"><pre>double kTonTNTInJoules <span class="hljs-operator">=</span> <span class="hljs-number">4184000000000</span><span class="hljs-comment">;</span>
double c <span class="hljs-operator">=</span> <span class="hljs-number">299792458</span><span class="hljs-comment">;</span>
int fatMan <span class="hljs-operator">=</span> <span class="hljs-number">21</span><span class="hljs-comment">;</span></pre></div><div id="8461"><pre><span class="hljs-built_in">Function</span> E = <span class="hljs-function"><span class="hljs-params">(double m)</span> =></span> m * <span class="hljs-built_in">Math</span>.pow(c, <span class="hljs-number">2</span>);</pre></div><div id="1ea8"><pre><span class="hljs-built_in">num</span> joulesToKTonTNT(<span class="hljs-built_in">num</span> val) => val/(kTonTNTInJoules*fatMan);</pre></div><div id="5b83"><pre><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">main</span>()</span> {
<span class="hljs-built_in">double</span> yourCatsWeight = <span class="hljs-number">4.1</span>; <span class="hljs-comment">//in kg</span>
print(<span class="hljs-string">'Your cat has ${joulesToKTonTNT(E(yourWeight)).toStringAsFixed(2)} times more explosive energy than a bomb that destroyed an entire city…'</span>);
}</pre></div><p id="d188">Above we first import the Dart’s core math library then define some constants: the number of Joules in one kiloton of TNT (<b>T</b>ri<b>N</b>itro<b>T</b>oluene) — <b>kTonTNTInJoules</b>, a better approximation of the speed of light — <b>c</b>, and the number of kilotons of TNT that a well known atomic bomb had — <a href="https://en.wikipedia.org/wiki/Fat_Man"><b>fatMan</b></a>.</p><p id="bf90">We then define the function that will calculate <b>mc²</b></p><p id="0306">We also define a function that will transform the energy (measured in Joules) into kilotons of TNT.</p><p id="61db">In the <b>main</b> body I defined my cat’s weight and displayed the result:</p><div id="8000"><pre>Your <span class="hljs-keyword">cat</span> has 4193.86 times <span class="hljs-keyword">more</span> explosive energy than a bomb that destroyed <span class="hljs-keyword">an</span> entire city…</pre></div><p id="c40c">The DartPad version can be played with <a href="https://dartpad.dev/97476531a0d5723f143700003c01f1ba">here</a>:</p><div id="bc84" class="link-block">
<a href="https://dartpad.dev/97476531a0d5723f143700003c01f1ba">
<div>
<div>
<h2>Your Explosive Energy </h2>
<div><h3>Dart Edition</h3></div>
<div><p>dartpad.dev</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div>
</div>
</div>
</a>
</div><h2 id="9e26">Flutter</h2><p id="7ce
Options
9">Moving on to Flutter, the code becomes:</p>
<figure id="5220">
<div>
<div>
<iframe class="gist-iframe" src="/gist/Constans/bcae948ad353fcc6813ed23e3f06e4b0.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="e998">Regarding the logic behind it, the code goes about the same as the previous one.</p><p id="8a1a">I changed the example to use the most powerful atomic bomb to be ever detonated in the history of human kind: the <a href="https://en.wikipedia.org/wiki/Tsar_Bomba">Tsar Bomba</a>.</p><p id="25d4">I also provided with the option to switch weight units between <b>kilograms</b> (kg) and <b>pounds</b> (lbs). When the pounds unit is chosen the weight must be first converted in kilograms and that is what the <b>getMass</b> function does.</p><figure id="e96b"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*9x8j2L58QRLNaf0Uw3Safg.png"><figcaption></figcaption></figure><p id="3638">The result is displayed either when pressing on the <b>Calculate</b> button, or when <b>Enter</b> key is pressed and the weight input is in focus.</p><figure id="347c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*BBC_N5NWOQdWgAWgCCTMNA.png"><figcaption></figcaption></figure><p id="11f8">The DartPad version can be played with <a href="https://dartpad.dev/bcae948ad353fcc6813ed23e3f06e4b0">here</a> (using Google Chrome is recommended):</p><div id="b5b1" class="link-block">
<a href="https://dartpad.dev/bcae948ad353fcc6813ed23e3f06e4b0">
<div>
<div>
<h2>Your Explosive Energy</h2>
<div><h3>Flutter Edition</h3></div>
<div><p>dartpad.dev</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div>
</div>
</div>
</a>
</div><p id="edef">If you want to read more about functions in Dart, you can do that <a href="https://readmedium.com/fluttering-dart-b37110f4d1bf">here</a>:</p><div id="af3b" class="link-block">
<a href="https://readmedium.com/fluttering-dart-b37110f4d1bf">
<div>
<div>
<h2>Fluttering Dart: Functions</h2>
<div><h3>How to write, use and abuse functions in Dart</h3></div>
<div><p>medium.com</p></div>
</div>
<div>
<div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*88Gz2J-Hje09d7geYn752A.jpeg)"></div>
</div>
</div>
</a>
</div><p id="9bfa">I hope this example got you intrigued and you’ll calculate the explosive energy you’ve stored inside of you.</p><p id="a0c1">Next time you feel tired just think of that. :)</p><figure id="bf0f"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aeDRwVZtvT8quFeVrSZMhA.jpeg"><figcaption></figcaption></figure><p id="204a">Tha(nk|t’)s all!</p></article></body>