-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
69 lines (55 loc) · 1.91 KB
/
MainActivity.java
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
package com.example.numbershapes;
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 Numbershape {
int shape;
String message;
public boolean isSquare() {
double squareRoot = Math.sqrt(shape);
if (squareRoot == Math.floor(squareRoot)) {
return true;
} else {
return false;
}
}
public boolean isTriangular() {
int x = 1;
int triangularNumber = 1;
while (triangularNumber < shape) {
x++;
triangularNumber += x;
}
if (triangularNumber == shape) {
return true;
} else {
return false;
}
}
}
public void guessShape(View view) {
Numbershape numbershape = new Numbershape();
EditText editText = (EditText) findViewById(R.id.editTextNumber);
numbershape.shape = Integer.parseInt(editText.getText().toString());
System.out.println(numbershape.shape);
String message = editText.getText().toString() + " is ";
if(numbershape.isSquare() && numbershape.isTriangular()){
message += " both Square and Triangular number.";
} else if (numbershape.isSquare()) {
message += " a Square number.";
} else if (numbershape.isTriangular()) {
message += " a triangular number.";
} else {
message = "Please enter a proper shape number!!";
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}