Answer to Question #251250 in Java | JSP | JSF for I AM BATMAN

Question #251250
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-14T04:49:21-0400


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");
		}
	}
}

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