class Factors {
private boolean[] factors;
private int value;
public Factors(int value) {
this.value = value;
this.factors = new boolean[value + 1];
}
public void findFactors() {
for (int i = 1; i < this.factors.length; i++) {
if ((this.value % i == 0) == true) {
this.factors[i] = true;
}
}
}
public void displayFactors() {
for (int i = 1; i < factors.length; i++) {
if (factors[i]) {
System.out.print(i + " ");
}
}
System.out.println();
}
public boolean isPerfect() {
int count = 0;
for (int i = 1; i < factors.length; i++) {
if (factors[i]) {
count++;
}
}
return count == 2;
}
public void displayCommonFactors(Factors other) {
for (int i = 0; i < Math.min(other.factors.length, factors.length); i++) {
if (factors[i] && other.factors[i]) {
System.out.print(i + " ");
}
}
System.out.println();
}
public void displayUnique(Factors otherFactors) {
for (int i = 0; i < Math.max(otherFactors.factors.length, factors.length); i++) {
if (i < otherFactors.factors.length && i < factors.length) {
if ((factors[i] ^ otherFactors.factors[i])) {
System.out.print(i + " ");
}
} else if (i < otherFactors.factors.length) {
if (otherFactors.factors[i]) {
System.out.print(i + " ");
}
} else {
if (factors[i]) {
System.out.print(i + " ");
}
}
}
System.out.println();
}
}
public class App {
public static void main(String[] args) {
Factors factors1 = new Factors(28);
factors1.findFactors();
Factors factors2 = new Factors(42);
factors2.findFactors();
System.out.println("Factors of value 28:");
factors1.displayFactors();
System.out.println("Factors of value 42:");
factors2.displayFactors();
System.out.println("Unique values are");
factors1.displayUnique(factors2);
System.out.println("Common values are:");
factors1.displayCommonFactors(factors2);
if (factors1.isPerfect()) {
System.out.println("Factors1 is a perfect");
} else {
System.out.println("Factors1 is NOT a perfect");
}
if (factors2.isPerfect()) {
System.out.println("Factors2 is a perfect");
} else {
System.out.println("Factors2 is NOT a perfect");
}
}
}
Comments
Leave a comment