Answer to Question #250576 in Java | JSP | JSF for Not the answer

Question #250576
Write a class Factors to model the factors of a integer1 à ƒ ƒ ¢ € ¢have a attribute storing the integer whose factors are to be calculated à ƒ ƒ ¢ € ¢have a boolean array attribute( factors) where each element record if that index is a factor of the integer ie factors[5] = True à ƒ ƒ ¢ € ¢ have a constructor assign the stored integer attribute to a user value and create the factors array, size of this factors array? à ƒ ƒ ¢ € ¢have a method to calculate factors of stored integer and store this in mentioned Boolean array This is done by dividing the integer by the integers less than itself check for a remainder à ƒ ƒ ¢ € ¢have a method that print out the factors of stored integer to screen à ƒ ƒ ¢ € ¢ have a method that takes another Factor object and prints out common factors between that object and current object. Demonstrate this class work by creating two factors objects one with a stored integer value of 28 and the other with a stored integer value of 42 Print out all and unique, common factors of both, print out if they are perfect number
1
Expert's answer
2021-10-13T00:29:03-0400
public class Factors {
    private boolean[] factors;
    private int value;

    public Factors(int value) {
        this.value = value;
        factors = new boolean[value + 1];
    }

    public void calculateFactors() {
        for (int i = 1; i < factors.length; i++) {
            factors[i] = value % i == 0;
        }
    }

    public void printFactors() {
        for (int i = 1; i < factors.length; i++) {
            System.out.print(factors[i] ? i + " " : "");
        }
        System.out.println();
    }

    public boolean isPerfect() {
        int count = 0;
        for (int i = 1; i < factors.length; i++) {
            count += factors[i] ? 1 : 0;
        }
        return count == 2;
    }

    public void printCommonFactors(Factors other) {
        for (int i = 0; i < Math.min(other.factors.length, factors.length); i++) {
            System.out.print(factors[i] && other.factors[i] ? i + " " : "");
        }
        System.out.println();
    }

    public void printUnique(Factors other) {
        for (int i = 0; i < Math.max(other.factors.length, factors.length); i++) {
            if (i < other.factors.length && i < factors.length) {
                System.out.print(factors[i] ^ other.factors[i] ? i + " " : "");
            } else if (i < other.factors.length) {
                System.out.print(other.factors[i] ? i + " " : "");
            } else {
                System.out.print(factors[i] ? i + " " : "");
            }
        }
        System.out.println();
    }
}


public class Main {
    public static void main(String[] args) {
        Factors a = new Factors(28);
        a.calculateFactors();
        Factors b = new Factors(42);
        b.calculateFactors();
        System.out.println("All:");
        System.out.println("a:");
        a.printFactors();
        System.out.println("b:");
        b.printFactors();
        System.out.println("Unique");
        a.printUnique(b);
        System.out.println("Common:");
        a.printCommonFactors(b);
        System.out.println("Is a perfect: " + a.isPerfect());
        System.out.println("Is b perfect: " + b.isPerfect());

    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS