1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3.  
  4. /**
  5.  * Some exercises to practice the concepts of looping and arraylists
  6.  *
  7.  * Looping through multiples of 5 between 10 and 95
  8.  * Adding up all numbers between 2 integers
  9.  * Testing if a number is a prime
  10.  *
  11.  * @author Justin Chan
  12.  * @version v.1
  13.  */
  14. public class Looptester
  15. {
  16. private ArrayList<Integer> modValues;
  17. private int[] modValues2;
  18.  
  19. /**
  20.   * Constructor for objects of class tester
  21.   */
  22. public Looptester()
  23. {
  24. modValues = new ArrayList<Integer>();
  25. }
  26.  
  27. /**
  28.   * Tests is a number is a prime using an Array
  29.   *
  30.   * @return True if it is a prime, false if it is not
  31.   * @param n The number whose status as a prime number you wish to find out
  32.   */
  33. public boolean arrayPrime(int n)
  34. {
  35. modValues2 = new int[n - 2];
  36. int nextIndexNumber = 0;
  37. boolean hasZero = true;
  38.  
  39. //x is the divisor, goes from 2 to n-1 inclusive
  40. for(int x = n - 1; x > 1 ; x--)
  41. {
  42. //x value loops through all numbers between 1 and n-1
  43. modValues2[nextIndexNumber] = (n % x);
  44. nextIndexNumber++;
  45. }
  46.  
  47. for(int modValue : modValues2)
  48. {
  49. if(modValue != 0)
  50. {
  51. //there are no 0s in the Array
  52. //it is a prime
  53. hasZero = true;
  54. }
  55.  
  56. else
  57. {
  58. //there is a 0 in the Array
  59. //it is not a prime
  60. hasZero = false;
  61. }
  62. }
  63. //empty Array for subsequent method calls
  64. modValues2 = new int[n];
  65. return hasZero;
  66. }
  67.  
  68. /**
  69.   * Tests is a number is a prime using a for-loop
  70.   *
  71.   * @return True if it is a prime, false if it is not
  72.   * @param n The number whose status as a prime number you wish to find out
  73.   */
  74. public boolean isPrime(int n)
  75. {
  76. boolean isPrime = true;
  77.  
  78. for(int i = 2; i < n; i++)
  79. {
  80. if(n % i == 0)
  81. {
  82. isPrime = false;
  83. //break;
  84. }
  85. }
  86. return isPrime;
  87. }
  88.  
  89. /**
  90.   * Tests is a number is a prime using an ArrayList
  91.   *
  92.   * @return True if it is a prime, false if it is not
  93.   * @param n The number whose status as a prime number you wish to find out
  94.   */
  95. public boolean arrayListPrime(int n)
  96. {
  97. int x = n; //x is the divisor, goes from 2 to n-1 inclusive
  98.  
  99. while(x > 2)
  100. {
  101. x--; //x value loops through all numbers between 1 and n-1
  102. modValues.add(n % x);
  103. }
  104.  
  105. if(modValues.contains(0) == false) //can't find a 0 in modValues, is prime
  106. {
  107. //empties array
  108. modValues = new ArrayList<Integer>();
  109.  
  110. return true;
  111. }
  112.  
  113. else //there is a 0 in modValues, is prime
  114. {
  115. //empties array
  116. modValues = new ArrayList<Integer>();
  117.  
  118. return false;
  119. }
  120. }
  121. }