Question: Create a method that will receive an integer parameter and then return an ArrayList that contains all of the numbers factors, excluding 1 and itself.
Create a method that will receive an integer parameter and then return an ArrayList that contains all of the numbers factors, excluding 1 and itself. Create a 2nd method that will remove all numbers from its ArrayList parameter that are not composite numbers. Composite numbers are divisible by 1, itself, and must have at least 1 other positive factor. You will need to use % to determine if a number is a factor.
starter code
public class ArrayListFactors { /** * Return all the factors of num, excluding 1 and num. * * For example: * If num is 9, then an ArrayList containing [3] is returned. * If num is 17, then an ArrayList of size zero is returned. * If num is 50, then an ArrayList containing [2, 5, 10, 25] * is returned. * * @param num All the factors of num are found * @return An ArrayList containing the factors of num. */ public static ArrayList
/** * Given a list of numbers, remove all prime numbers numbers from the * list, so there are only composite numbers remaining. * * For example: * If on entry nums is [2, 3, 4, 5, 6], then on return nums * will be [4,6]. * * @param nums An ArrayList of integers. All prime numbers are * removed from the list, so only Composite Numbers remain * on return. */ public static void keepOnlyCompositeNumbers( List
runner
public class ArrayListFactorsRunner { public static void main( String args[] ) { // Test getListOfFactors System.out.println(ArrayListFactors.getListOfFactors(9)); System.out.println(ArrayListFactors.getListOfFactors(23)); System.out.println(ArrayListFactors.getListOfFactors(50)); System.out.println(ArrayListFactors.getListOfFactors(100)); System.out.println(ArrayListFactors.getListOfFactors(762)); System.out.println(); // Test keepOnlyCompositeNumbers Integer[] nums = {2,6,8,9,10,12,13,15,17,24,55,66,78,77,79}; List
System.out.println("Original List"); System.out.println( list ); System.out.println("Composite List"); ArrayListFactors.keepOnlyCompositeNumbers( list ); System.out.println( list ); } }
Sample Data :
9
23
50
100
762
2 6 8 9 10 12 13 15 17 24 55 66 78 77 79
Sample Output :
[3]
[]
[2, 5, 10, 25]
[2, 4, 5, 10, 20, 25, 50]
[2, 3, 6, 127, 254, 381]
Original List
[2, 6, 8, 9, 10, 12, 13, 15, 17, 24, 55, 66, 78, 77, 79]
Composite List
[6, 8, 9, 10, 12, 15, 24, 55, 66, 78, 77]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
