Answer to Question #251480 in Java | JSP | JSF for I AM GRROT

Question #251480
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-14T16:04:18-0400
package factors;


import java.util.Scanner;




public class Factors {


    private int data;
    private boolean [] factors;
    private int [] arr;
    private int x;
    Factors(int d){
        data = d;
        int m = 0;
        factors = new boolean[data];
        
        int n = 0;
        for(int i=1; i<=data; i++){
            
            if(data % i==0){
                factors[n] = true;
                n++;
                m++;
            }
            else{
                 factors[n] = false;
                n++;
            }
            
        }
        for(int i=0; i<factors.length; i++){
            System.out.printf("%b  ", factors[i]);
        }
        x=0;
        arr = new int[m];
    }
    
    void populate(){
       int n = 1;
       int f = 0;
       
       while(n<=data){
           if(data %n ==0){
               arr[f] = n;
               factors[f]  = true;
               f++;
               
           }
           
           n++;
       } 
        
       
        
       
        
    }
    
    void display(){
       
        System.out.printf("\nFactors of %d are: ", data);
        int sum = 0;
        for(int i=0; i<arr.length; i++){
            
           
                System.out.printf("%d, ",arr[i]);
               
            
        }
        for(int i=0; i<arr.length-1; i++){
             sum +=arr[i];
        }
        
        
        if(sum==data){
            System.out.printf("\n%d, is a perfect number\n", data);
        }
        else{
            System.out.printf("\n%d, is not a perfect number\n", data);
        }
    }
    
    void common_unique_factors(Factors f){
        
        int first = f.arr.length;
        int x = f.arr[0];
         
        int second = arr.length;
        System.out.printf("\nFirst array length: %d", first);
        System.out.printf("\nSecond array length: %d\n", second);
        int max = first;
        int min = second;
        if(second>first){
            max = second;
            min = first;
        }
        
        
        for(int i=0; i<max; i++){
            
            
           
            if(i==min || i==max){
                break;
            }
             int t1= arr[i];
             int t2 = f.arr[i];
            
            
            if(t1==t2){
                
            }
        }
        
    }
    
    public static void main(String[] args) {
        
        Factors f1 = new Factors(28);
        f1.populate();
        f1.display();
        Factors f2 = new Factors(42);
        f2.populate();
        f2.display();
        f1.common_unique_factors(f2);
        
        
       
    }
    
}

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