Another Headache! You are stuck in Android App where you can to check if number is triangular or check if number is square but you’ve not good luck yet.
Let’s start the Android Studio, create a new empty activity, open up the XML of the activity and paste the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/getNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="19dp" android:ems="10" android:hint="Enter a Number" android:inputType="textPersonName" /> <Button android:id="@+id/checkNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/getNumber" android:layout_centerHorizontal="true" android:layout_marginTop="27dp" android:onClick="checknumberST" android:text="Check Now!" /> </RelativeLayout> |
Now Open the java file for that activity and paste the following code.
Replace isSquareTriangular class name with your class name, also edit the package for the class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package com.example.dell.demoapp; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class isSquareTriangular extends AppCompatActivity { class shape { int number; public boolean isSquare() { double squarenumber = Math.sqrt(number); if(squarenumber == Math.floor(squarenumber)) { return true; } else { return false; } } public boolean isTriangular() { int x = 1; int trino = 1; while(trino < number) { x++; trino = trino + x; } if(trino == number) { return true; }else { return false; } } } public void checknumberST(View view){ EditText editText = (EditText) findViewById(R.id.getNumber); String getnumber = editText.getText().toString(); String message = ""; if(getnumber.isEmpty()){ message = "please input something!!!!!"; }else { shape call = new shape(); call.number = Integer.parseInt(editText.getText().toString()); System.out.println(call.isSquare()); if (call.isSquare()) { if (call.isTriangular()) { message = call.number + " : is both Square and Triangular!"; } else { message = call.number + " : is only Square"; } } else { if (call.isTriangular()) message = call.number + " : is only Triangular"; else message = "Niether Square, Nor Triangular!!!!"; } } Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_is_square_triangular); } } |
An output like this will be created, where you can easily put a number which the program will check if number is triangular, check if number is square or both.
[[Also check If you’re stuck at Activity must be exported or contain a Intent-Filter]]
In the below example, you can see that the Toast message shows that the number (1) is Both. If you will check other numbers it will tell you the other two parts of App if number is triangular or square.
Final Words on Check If Number is Triangular or Square
Sometimes the university or college assignment and mini projects will ruin your happiness but not anymore, Just follow the above steps, if there anything wrong or you have a question just ask us in the comment section.