0% found this document useful (0 votes)
15 views3 pages

Android Factorial Calculator App

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Android Factorial Calculator App

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q. WAP to create two screens. First screen will take one number input from user.

After click on
Factorial button, second screen will open and it should display factorial of the same number.
Also specify which type of intent you will use in this case.

XML File:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>

<EditText
android:id="@+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="75dp"
android:layout_y="248dp"
android:ems="10"
android:hint="Enter Number:"
android:inputType="text" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="139dp"
android:layout_y="354dp"
android:onClick="Fact"
android:text="Factorial" />
</AbsoluteLayout>

JAVA File:
package [Link].practical183;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


EditText t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
public void Fact(View v){
t1=(EditText) findViewById([Link].t);
String num=[Link]().toString();
Intent i=new Intent(getApplicationContext(), [Link]);
[Link]("Num",num);
startActivity(i);
}
}

XML File 2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=""
android:textSize="25dp"
android:textStyle="bold"
tools:layout_editor_absoluteX="149dp"
tools:layout_editor_absoluteY="72dp" />
</LinearLayout>

JAVA File 2:
package [Link].practical183;
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity2 extends AppCompatActivity {


TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main2);
t=(TextView) findViewById([Link]);
Intent i=getIntent();
int n=[Link]([Link]("Num"));
int factorial=1;
for(int x=2;x<=n;x++) {
factorial *= x;
}
[Link]("Factorial="+factorial);

}
}
OUTPUT:

Common questions

Powered by AI

The TextView in the second activity's layout displays the calculated factorial to the user. It is updated with the results computed from the user input received through the Intent.

The application uses an AbsoluteLayout for capturing user input, which includes an EditText for number entry and a Button to trigger the factorial calculation.

The Android application transitions from the first screen to the second by using an Intent in the Fact method. The Intent carries the user input from MainActivity to MainActivity2, where it is processed.

The user-inputted number is initially stored as a String, which is appropriate as EditText returns text. It is then converted to an integer in MainActivity2 to perform arithmetic calculations for the factorial.

Potential improvements include implementing input validation checks to ensure only numeric values are entered, utilizing a try-catch block around Integer parsing to prevent runtime errors, and providing user feedback on invalid input through UI alerts.

In the second activity, the factorial calculation process involves retrieving the passed number, parsing it into an integer, and using a for loop to multiply it by descending integers starting from 2 to the number itself, storing the result in a variable named factorial.

The method implemented to handle button click events is the Fact method. It retrieves the input number using the EditText widget, creates an Intent to launch the second activity, and passes the input number as an extra in the Intent.

AbsoluteLayout hardcodes positions, reducing UI flexibility and making maintenance challenging, as it doesn't adjust well to different screen sizes/densities, unlike other layouts like ConstraintLayout or LinearLayout which are more responsive.

The application ensures accurate display by setting the calculated factorial value as the text of a TextView component in MainActivity2 after the loop computation is complete.

Using an Intent is necessary to switch between activities because it provides a mechanism to carry data and specify the target activity, ensuring the input number is passed from MainActivity to MainActivity2 where the result will be displayed.

You might also like