소수 판별 TEST
public class PrimeNumberTest {
@Test
public void 구현테스트1() {
System.out.println(checkPrimeNumber(new int[]{29}));
Assert.assertTrue(checkPrimeNumber(new int[]{29}));
}
@Test
public void 구현테스트2() {
System.out.println(checkPrimeNumber(new int[]{79}));
Assert.assertTrue(checkPrimeNumber(new int[]{79}));
}
@Test
public void 구현테스트3() {
System.out.println(checkPrimeNumber(new int[]{89}));
Assert.assertTrue(checkPrimeNumber(new int[]{89}));
}
@Test
public void 테스트_하나라도_소수가_있는_경우() {
System.out.println(checkPrimeNumber(new int[]{12, 12, 12, 1}));
Assert.assertTrue(checkPrimeNumber(new int[]{12, 12, 12, 1}));
}
@Test
public void 하나도_소수가_없는_경우() {
System.out.println(checkPrimeNumber(new int[]{4, 6, 8, 10}));
Assert.assertFalse(checkPrimeNumber(new int[]{4, 6, 8, 10}));
}
@Test
public void 모두_소수인_경우() {
System.out.println(checkPrimeNumber(new int[]{1, 11, 13, 7}));
Assert.assertTrue(checkPrimeNumber(new int[]{1, 11, 13, 7}));
}
private boolean checkPrimeNumber(int[] nums) {
for (int num : nums) {
int index = (int) Math.sqrt(num);
boolean isPrime = true;
for (int i = 2; i <= index; i++) {
if (num % i == 0) {
isPrime = false;
}
}
if (isPrime) {
return true;
}
}
return false;
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int N = 5;
private static int loop = 0;
private static int x1 = Integer.MIN_VALUE;
private static int y1 = Integer.MIN_VALUE;
private static int x2 = Integer.MIN_VALUE;
private static int y2 = Integer.MIN_VALUE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.touch_view).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_UP: {
loop++;
if(loop >= N) {
Toast.makeText(MainActivity.this, "Touch count limits", Toast.LENGTH_SHORT).show();
return true;
}
x2 = (int) motionEvent.getX();
y2 = (int) motionEvent.getY();
Log.d(TAG, "is there prime number?" + ' ' + checkPrimeNumber(new int[]{x1, x2, y1, y2}));
int xRange = Math.abs(x2 - x1);
int yRange = Math.abs(y2 - y1);
String content = String.format(Locale.getDefault(), "X value - %d, Y value - %d", xRange, yRange);
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
}
break;
case MotionEvent.ACTION_DOWN: {
x1 = (int) motionEvent.getX();
y1 = (int) motionEvent.getY();
}
break;
default: {
}
}
return true;
}
});
}
private boolean checkPrimeNumber(int[] nums) {
for (int num : nums) {
int index = (int) Math.sqrt(num);
boolean isPrime = true;
for (int i = 2; i <= index; i++) {
if (num % i == 0) {
isPrime = false;
}
}
if (isPrime) {
return true;
}
}
return false;
}
}