In: Computer Science
Write a class file called lastname_digits.java. In it, write a method to compute and return the total instances where the sum of digits of the array equals to the target.
Write a demo file called lastname_digits_demo.java. Create an array of random 10 integers between 301 and 999. Also create an integer variable called target that is equal to a value between 5 and 10. Print the answer within this file.
Example:
If five of the 20 integers are equal to 512, 607, 620, 767, 791 and the target is 8. Sum of the four integers is equal to 5+1+2 = 8, 13, 8, 20, 17. Then the total instances where the sum of digits of the array equals to the target is equal to 2.
//lastname_digits.java
class lastname_digits
{
   public static int getTotalInstaces(int[] array,int
target)
   {
       int count=0;
       for(int
i=0;i<array.length;i++)
       {
           int
temp=array[i];
           int sum=0;
          
while(temp!=0)
           {
          
    sum=sum+temp%10;
          
    temp=temp/10;
           }
          
if(sum==target)
           count++;
       }
       return count;
   }
}
//lastname_digits_demo.java
import java.util.Random;
class lastname_digits_demo
{
   public static void main(String[] args)
   {
       Random r = new Random();
       int low = 301;
       int high = 999;
       int[] array = new int[10];
       for(int i=0;i<10;i++)
       {
          
array[i]=r.nextInt(high-low) + low;
       }
       low=5;
       high=10;
       int target=r.nextInt(high-low) +
low;
       System.out.print("Array elements:
");
       for(int i=0;i<10;i++)
       {
          
System.out.print(array[i]+"\t");
       }  
   
       System.out.println("\nTarget:
"+target);
       System.out.println("Count =
"+lastname_digits.getTotalInstaces(array,target));
   }
}
