Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.checkevenclass;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- class NumberClass{
- int value;
- public boolean isTriangular() //check for triangular number
- {
- if (value < 0)
- return false;
- int sum = 0;
- for (int n = 1; sum <= value; n++)
- {
- sum = sum + n;
- if (sum == value)
- return true;
- }
- return false;
- }
- public boolean isPerfectSquare() //check for perfect square
- {
- double sq = Math.sqrt(value);
- return ((sq - Math.floor(sq)) == 0);
- }
- }
- public void checkNumber(View view){
- EditText editText = (EditText) findViewById(R.id.editText);
- if (editText.getText().toString().trim().length() <=0 ){ //if editText is empty
- Toast.makeText(this, "Enter a number first", Toast.LENGTH_SHORT).show();
- }
- else {
- obj.value = Integer.parseInt(editText.getText().toString().trim());
- Toast.makeText(this, "Is Perfect square: " + obj.isPerfectSquare() + "\nIs triangular number: " + obj.isTriangular()
- , Toast.LENGTH_SHORT).show();
- }
- }
- NumberClass obj = new NumberClass();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- }
Add Comment
Please, Sign In to add comment