MIT-Curricular/OOP/Java/Lab/Week5/Number.java

62 lines
1.6 KiB
Java

import java.util.Scanner;
class Number {
private double value;
public Number(double value) {
this.value = value;
}
public boolean isZero() {
return value == 0;
}
public boolean isPositive() {
return value > 0;
}
public boolean isNegative() {
return value < 0;
}
public boolean isOdd() {
return (int) value % 2 != 0;
}
public boolean isEven() {
return (int) value % 2 == 0;
}
public boolean isPrime() {
if (value <= 1 || value != (int) value) return false;
for (int i = 2; i <= Math.sqrt(value); i++) if (
(int) value % i == 0
) return false;
return true;
}
public boolean isArmstrong() {
if (value != (int) value) return false;
int num = (int) value, sum = 0, digits = String.valueOf(num).length();
for (int n = num; n > 0; n /= 10) sum += Math.pow(n % 10, digits);
return sum == num;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
Number number = new Number(sc.nextDouble());
System.out.println("Is Zero: " + number.isZero());
System.out.println("Is Positive: " + number.isPositive());
System.out.println("Is Negative: " + number.isNegative());
System.out.println("Is Odd: " + number.isOdd());
System.out.println("Is Even: " + number.isEven());
System.out.println("Is Prime: " + number.isPrime());
System.out.println("Is Armstrong: " + number.isArmstrong());
sc.close();
}
}