blob: e80cedff40f6733820eeca06ee643d8b012df071 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/**
* Assignment 1, COMP268 Class: Number.java
*
* @description Provides static methods for operating on numeric values.
* @author: mo khan Student ID: 3431709
* @date May 8, 2019
* @version 1.0
*/
package Q9;
import java.util.ArrayList;
public class Number {
/**
* Checks to see if a number is divisible by 5.
*
* @param n the number to check for divisibility
* @return true if the number is evenly divisible by 5
*/
public static boolean isDivisibleBy5(int n) {
return isDivisibleBy(n, 5);
}
/**
* Checks to see if a number is divisible by 7
*
* @param n the number to check for divisibility
* @return true if the number is evenly divisible by 7
*/
public static boolean isDivisibleBy7(int n) {
return isDivisibleBy(n, 7);
}
/**
* Checks if a number is odd
*
* @param n the number to check
* @return true if the number is an odd number.
*/
public static boolean isOdd(int n) {
return !isDivisibleBy(n, 2);
}
/**
* Checks if a number is prime. This is naive implementation of the prime number check that will
* blow the stack for any sufficiently large number.
*
* @param n the number to check
* @return true if the number is a prime number
*/
public static boolean isPrime(int n) {
if (n <= 1) return false;
for (int i = n - 1; i > 1; i--) if (isDivisibleBy(n, i)) return false;
return true;
}
/**
* Checks to see if a number is divisible by denominator
*
* @param n the number to check for divisibility
* @param denominator the number to see if n is evenly divisible by
* @return true if the number is evenly divisible by denominator
*/
public static boolean isDivisibleBy(int n, int denominator) {
return n % denominator == 0;
}
/** @return a list of strings for each number between 0 and 113 */
public static ArrayList<String> iterate() {
ArrayList<String> items = new ArrayList<String>();
ArrayList<String> row = new ArrayList<String>();
for (Integer i = 0; i < 113; i++) {
row.clear();
row.add(String.format("%d", i));
if (isOdd(i)) row.add(String.format("%d is odd", i));
if (isDivisibleBy5(i)) row.add("hi five");
if (isDivisibleBy7(i + (i + 1))) row.add("wow");
if (isPrime(i)) row.add("prime");
items.add(String.join(",", row));
}
return items;
}
public static void main(String[] args) {
for (String item : Number.iterate()) System.out.println(item);
}
}
|