NumberUtilities.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. public class NumberUtilities {
  2. /**
  3. * Takes in one parameter "stop" to create a string of numbers, starting
  4. * from 0 to stop, not including stop.
  5. */
  6. public static String getRange(int stop) {
  7. String numberRange = "";
  8. for (int i = 0; i < stop; i++) {
  9. numberRange += i;
  10. }
  11. return numberRange;
  12. }
  13. /**
  14. * Takes 2 parameters to dictate the range of the string.
  15. */
  16. public static String getRange(int start, int stop) {
  17. String numberRange = "";
  18. for (int i = start; i < stop; i++) {
  19. numberRange += i;
  20. }
  21. return numberRange;
  22. }
  23. /**
  24. * Uses the iterator to step and create the numbers in the range.
  25. */
  26. public static String getRange(int start, int stop, int step) {
  27. String numberRange = "";
  28. for (int i = start; i < stop; i += step) {
  29. numberRange += i;
  30. }
  31. return numberRange;
  32. }
  33. /**
  34. * Uses the modulus operator to check for even/odd numbers to add to the
  35. * numberRange string.
  36. */
  37. public static String getEvenNumbers(int start, int stop) {
  38. String numberRange = "";
  39. for (int i = start; i < stop; i += 1) {
  40. if (i % 2 == 0) {
  41. numberRange += i;
  42. }
  43. }
  44. return numberRange;
  45. }
  46. /**
  47. * See "getEvenNumbers"
  48. */
  49. public static String getOddNumbers(int start, int stop) {
  50. String numberRange = "";
  51. for (int i = start; i < stop; i += 1) {
  52. if (i % 2 == 1) {
  53. numberRange += i;
  54. }
  55. }
  56. return numberRange;
  57. }
  58. /**
  59. * Uses Math.pow to raise i to a specific power, then casts it to an int
  60. * so it can be passed into the string to be returned.
  61. */
  62. public static String getExponentiations(int start, int stop, int exponent) {
  63. String numberRange = "";
  64. for (int i = start; i <= stop; i += 1) {
  65. int intNum = (int) Math.pow(i, exponent);
  66. numberRange += intNum;
  67. }
  68. return numberRange;
  69. }
  70. }