-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumbers
More file actions
199 lines (184 loc) · 6.19 KB
/
Copy pathNumbers
File metadata and controls
199 lines (184 loc) · 6.19 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// This main method tests a set of student-written methods.
public class Hw4
{
public static void main(String[] args) throws Exception
{
// Declare main method vars and arrays here.
int [] first = new int[50];
int [] second = new int[50];
System.out.print("Primes per Fifty:\n");
for (int low=0, hi=49; hi < 400; low=hi+1, hi=hi+50)
{
System.out.print(low+"-"+hi+": ");
printPrimesOnRange(low, hi);
}
System.out.println();
System.out.print("Leap Years per Fifty (Years 1000-2049):\n");
for (int low=1000, hi=1049; hi < 2050; low=hi+1, hi=hi+50)
System.out.print(numberOfLeapYears(low, hi) + " ");
System.out.println();
readIntsTextFile(first, 35, "june.txt");
System.out.println("\nJune Array: ");
printArray(first, 20);
printArray(first, 32);
readIntsTextFile(second, 30, "april.txt");
System.out.println("April Array: ");
printArray(second, 15);
printArray(second, 25); //
System.out.print("\nLargest: ");
for (int i=3; i<32; i=i+3)
System.out.print(largestInArray(first, i)+" ");
System.out.print(" ----- ");
for (int i=3; i<30; i=i+3)
System.out.print(largestInArray(second, i)+" ");
System.out.print("\nLargest Gaps: ");
for (int i=3; i<32; i=i+3)
System.out.print(largestGapInArray(first, i)+" ");
System.out.print(" ----- ");
for (int i=3; i<30; i=i+3)
System.out.print(largestGapInArray(second, i)+" ");
System.out.println("\n\nTesting complete.");
}
// Students should write their 9 methods below
//method 1
public static boolean isDivisibleBy (int num, int denom){
/*
I need to make a statement that returns false if the demoninator is
zero because you cant devide by zero
*/
if (denom == 0){
return false;
}
return num % denom == 0;
}
//method 2
public static boolean isPrime(int x) {
if (x <= 1) { // a prime number has to be greater than one
return false;
}
/*
the method loops through all possible denominators of x that are less
than or equal to the square root of x
*/
for (int denominator = 2; denominator <= Math.sqrt(x); denominator++)
{
if (isDivisibleBy (x,denominator)) {
return false;
}
}
return true;
}
//method 3
public static void printPrimesOnRange(int start, int finish){
int count = 0;
/*
for this I need a for loop so that every time I encounter a
prime number i can increase the count by one
*/
for(int num = start; num<= finish; num++) {
if(isPrime(num)) {
System.out.print(num + " ");
count++;
}
}
System.out.print(count + "\n");
}
// method 4
public static boolean isLeapYear(int year){
/*
for this I decied to set the parameters for if the year wasn't a leap
year, and if it was a leap year the year would pass through all the if
else statements and return true
*/
if (!isDivisibleBy (year,4)) {
return false;
} else if (!isDivisibleBy (year,100)) {
return false;
} else if (!isDivisibleBy (year,400)) {
return false;
} else {
return true;
}
}
// method 5
public static int numberOfLeapYears(int startYear, int endYear) {
int count = 0;
for (int year = startYear; year <= endYear; year++) {
if (isALeapYear(year)) {
count++;
}
}
return count;
}
public static boolean isALeapYear(int year) {
boolean isLeapYear = false;
/*
this method is the rules for determining leap years, which state that a
year is a leap year if it is divisible by 4 but not divisible by 100, or
if it is divisible by 400.
*/
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = true;
}
return isLeapYear;
}
// method 6
public static void readIntsTextFile(int[] a, int n, String fileName) throws FileNotFoundException {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
for (int i = 0; i < n; i++) {
if (scanner.hasNextInt()) {
a[i] = scanner.nextInt();
/*
for this program i represents the line being scanned in the file
which is why I set it equal to zero so it can start at the
first position. Then it reads the line then I use i++ to get the
program to move to the next line in the file and scan that line.
*/
}
}
}
//method 7
public static void printArray(int[] a, int n) {
for (int current = 0; current < n; current++) {
/*
for loop that will iterate over each element in the array
*/
System.out.print(a[current] + " ");
/*
this will print out the current libe in the array and add a space after
for the next thing printed
*/
}
System.out.println();
}
// method 8
public static int largestInArray(int[] a, int n) {
int largest = a[0]; // Initialize largest to first element of array
for (int current = 1; current < n; current++) {
if (a[current] > largest) {
largest = a[current]; // Update largest if a[current] is greater
//than the current largest
}
}
return largest;
}
// method 9
public static int largestGapInArray(int[] a, int n) {
int largestGap = 0; // Initialize largest gap to zero
for (int current = 0; current < n - 1; current++) {
int gap = Math.abs(a[current+1] - a[current]); /*
I'm doing using Math.abs() because I wan to calcutate the absoulte
difference between the current and the next element being read
*/
if (gap > largestGap) {
largestGap = gap; // Update largest gap if gap is greater
// than current largest gap
}
}
return largestGap;
}
}