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

78 lines
1.8 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;
int originalNum = num;
int sum = 0;
int digits = String.valueOf(num).length();
while (num > 0) {
int digit = num % 10;
sum += Math.pow(digit, digits);
num /= 10;
}
return sum == originalNum;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
double input = sc.nextDouble();
Number number = new Number(input);
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();
}
}