How To Build A Work and Energy Science Calculator App on Android - Hive Programmers
Greetings to my favorite science community online, StemSocial.
It's @skyehi and in yesterday's episode on my Android App Tutorial series, I shared how to build an Ohm's Law calculator for calculating the Voltage, Current and Resistance of a conductor using the Ohm's Law formula.
Original Image Source by insspirito from Pixabay
Now today's blog is quite similar but this time instead of the Ohm's law, we'll be building an Android Calculator App which calculates Energy and Work.
Work and energy are two very fundamental concepts in the world of Physics.
In an event where you want to calculate the amount of energy required for an object or a machine to perform a task or work, you could use the formula to do the math.
However, for convenience sake, we can build an App that we would rely on to do the calculations for us as long as we have the variables needed.
I'll be writing the codes on the account that you're already familiar with the formula for work and energy calculations.
I would highly recommend that you go and check out the formula if you're interested in completing your calculator App guys.
If you went through with yesterday's blog, you should find this tutorial as easy as a walk in the park.
- let's get started with today's work shall we
Prerequisites
For the benefit of newcomers to Android App development or my series, I'll share with you the required softwares you need to be able to go through with this tutorial.
Android Studio IDE - the platform on which we'll develop this and all other Android Apps.
Java Development Kit, JDK - You need to install JDK correctly on your PC for your PC to be able to execute Java commands or codes.
Physical Android Device or Emulator - After building the App, we'll need to test it either on an emulator or a physical Android device.
If you have all these checked out, you're absolutely ready to go through with this tutorial
Creating a New Android Project
To Start working on your Work and Energy calculator App, click on Android Studio IDE and create a new Android App project.
You can create a project by clicking on the "Create a New Project" button. You'll be presented with a number of project template options to choose from.
As we normally do, select "Empty Activity" as the template of your project and click the "Next" button to continue with your project configurations.
Set the App name and package name of your App. Also don't forget to ensure that Java programming language is the selected project language and not Kotlin.
However if you're already familiar with Kotlin, you can translate the Java codes in Kotlin guys.
When you're satisfied with your new Android App project configuration, click on the "Finish" button and wait while Android Studio prepares your new project space.
Designing the User Interface
Ok guys, we're ready to start working on the User interface of our Work and Energy calculator App. Like I said earlier on, this would be quite similar to yesterday's episode on building the Ohm's Law Calculator App.
We will create a few EditText
elements that would receive user inputs. To calculate energy or work, we probably need to have inputs for Force
, Distance
and perhaps Angle
We also need to create a Button
that would initiate the calculation using the algorithms we'll put inside the backend code.
Finally we would need to have a TextView
element that would display the final results.
Our design layout code will be written inside the res/layout/activity_main.xml
file.
Here's how your user interface layout code should look like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextForce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Force (N)" />
<EditText
android:id="@+id/editTextDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextForce"
android:layout_marginTop="16dp"
android:hint="Distance (m)" />
<EditText
android:id="@+id/editTextAngle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/editTextDistance"
android:layout_marginTop="16dp"
android:hint="Angle (degrees)" />
<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextAngle"
android:layout_marginTop="16dp"
android:text="Calculate" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnCalculate"
android:layout_marginTop="16dp"
android:text="Result: "
android:textStyle="bold" />
</RelativeLayout>
Working On Our Calculator App Logic
It's time to work on the backend code of our App. All of the work will be done inside our MainActivity.java
file.
This Java file will link the UI components and handle user interactions.
To begin with, we first have to initialize all the UI elements we introduced in our activity_main.xml
. This would include the editTexts, the button and the textView.
After we're done we also need to introduce a setOnClickListener
function or method for the "Calculate" button which would use the logic or formula of energy and work calculations to find the results.
Afterwards, we would set a String
variable that would hold the final results and display it inside our results textView.
It's as easy as that guys.
Here's how the logic code should look like
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText editTextForce, editTextDistance, editTextAngle;
Button btnCalculate;
TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextForce = findViewById(R.id.editTextForce);
editTextDistance = findViewById(R.id.editTextDistance);
editTextAngle = findViewById(R.id.editTextAngle);
btnCalculate = findViewById(R.id.btnCalculate);
textViewResult = findViewById(R.id.textViewResult);
btnCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calculateWorkAndEnergy();
}
});
}
private void calculateWorkAndEnergy() {
double force = parseEditText(editTextForce);
double distance = parseEditText(editTextDistance);
double angle = parseEditText(editTextAngle);
if (countProvidedValues(force, distance) < 2) {
textViewResult.setText("Provide at least force and distance for calculation.");
return;
}
double work = calculateWork(force, distance, angle);
double kineticEnergy = calculateKineticEnergy(force, distance);
String resultText = String.format("Work: %.2f J, Kinetic Energy: %.2f J", work, kineticEnergy);
textViewResult.setText(resultText);
}
private double parseEditText(EditText editText) {
String text = editText.getText().toString().trim();
return text.isEmpty() ? Double.NaN : Double.parseDouble(text);
}
private int countProvidedValues(double... values) {
int count = 0;
for (double value : values) {
if (!Double.isNaN(value)) {
count++;
}
}
return count;
}
private double calculateWork(double force, double distance, double angle) {
// Implement work calculation logic
}
private double calculateKineticEnergy(double force, double distance) {
// Implement kinetic energy calculation logic
}
}
I would assume that you're already familiar with the formula for calculating Energy. I would be happy if I could get a few comments from my readers to guess what the formula would look like.
Congratulations
Congratulations guys! You've successfully built a basic Work and Energy Calculator app for Android devices.
You can run the App either on an emulator or a physical Android device.
When the App launches, input the variables required, press the calculate button and the App should give you the final results.
I really hope you enjoyed this particular episode on my series.
We have built two science calculators this week and we'll be moving on from calculators to other concepts tomorrow.
Also to make this series a little more interesting, I would love to get some feedback from my readers on other Apps you would love to see being developed in this series.
I have a whole lot of App ideas prepared but I want to make sure that all my readers are enjoying and benefiting one way or the other from each episode.
Thank you so much for the support guys. Thanks for taking the time to read.
Have A Great Day And Catch You Next Time On StemSocial. Goodbye 👨💻
You Can Follow Me @skyehi For More Like This And Others
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
Thanks for including @stemsocial as a beneficiary, which gives you stronger support.