Update OOP/Java/Lab/Week5/Number.java
This commit is contained in:
parent
b178260bf8
commit
63d5f2ae23
@ -29,40 +29,24 @@ class Number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isPrime() {
|
public boolean isPrime() {
|
||||||
if (value <= 1 || value != (int) value) {
|
if (value <= 1 || value != (int) value) return false;
|
||||||
return false;
|
for (int i = 2; i <= Math.sqrt(value); i++) if (
|
||||||
}
|
(int) value % i == 0
|
||||||
for (int i = 2; i <= Math.sqrt(value); i++) {
|
) return false;
|
||||||
if ((int) value % i == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isArmstrong() {
|
public boolean isArmstrong() {
|
||||||
if (value != (int) value) {
|
if (value != (int) value) return false;
|
||||||
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);
|
||||||
int num = (int) value;
|
return sum == num;
|
||||||
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) {
|
public static void main(String[] args) {
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner sc = new Scanner(System.in);
|
||||||
System.out.print("Enter a number: ");
|
System.out.print("Enter a number: ");
|
||||||
double input = sc.nextDouble();
|
Number number = new Number(sc.nextDouble());
|
||||||
Number number = new Number(input);
|
|
||||||
|
|
||||||
System.out.println("Is Zero: " + number.isZero());
|
System.out.println("Is Zero: " + number.isZero());
|
||||||
System.out.println("Is Positive: " + number.isPositive());
|
System.out.println("Is Positive: " + number.isPositive());
|
||||||
|
Loading…
Reference in New Issue
Block a user