-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayOperations.java
More file actions
91 lines (84 loc) · 3.27 KB
/
ArrayOperations.java
File metadata and controls
91 lines (84 loc) · 3.27 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
package module1.introduction.arrayoperations;
/**
* Mae Morella
* Lab 1A - Array Operations
* Demonstrate basic functions for manipulating arrays
*/
public class ArrayOperations {
/** Convert an array's contents as a JSON-style string
* @param arr An integer array to display.
* @return A string formatted like "[3, -2, 7]"
*/
public static String arrayToString(int[] arr) {
String children = "";
for (int i = 0; i < arr.length; i++) {
// Append number
children += arr[i];
if (i < arr.length - 1) {
children += ", ";
}
}
return "[" + children + "]";
}
/** Search an array and return the largest value
* @param arr The array of integers, with at least one element
* @return The largest value found in the array.
*/
public static int findLargest(int[] arr) {
int largest = arr[0];
for (int el : arr) {
largest = Math.max(el, largest);
}
return largest;
}
/** Generate an array filled with random integer values
* @param len The desired length of the random array
* @param min The lower bound of values to use
* @param max The upper bound of values to use (exclusive)
* @return An int[] array of random integers [min, max)
*/
public static int[] randomArray(int len, int min, int max) {
int[] arr = new int[len];
for (int i = 0; i < arr.length; i++) {
// Random number between min and max
double rand = (Math.random()) * (max - min) + min;
arr[i] = (int) rand;
}
return arr;
}
/**
* Determine the length of the longest continuous series of positive integers.
* For example [-1, 8, 7, 5, -6] yields a length of 3
* @param arr An array of at least one integer
* @return The length of this streak
*/
public static int positiveStreakLength(int[] arr) {
int currentStreak = 0;
int longestStreak = 0;
for (int el : arr) {
if (el > 0) {
currentStreak++;
longestStreak = Math.max(currentStreak, longestStreak);
} else {
currentStreak = 0;
}
}
return longestStreak;
}
public static void main(String[] args) {
int[] nums = { 1, 4, 13, 43, -25, 17, 22, -37, 29 };
System.out.println("int[] nums = " + arrayToString(nums));
/* Print the largest value in int[] nums */
System.out.println("findLargest(nums) => " + findLargest(nums));
System.out.println();
/* Create an array called data and fill it with random numbers [-100, 100) */
int[] data = randomArray(20, -100, 100);
System.out.println("int[] data = " + arrayToString(data));
/* Print the largest value in int[] data */
System.out.println("findLargest(data) => " + findLargest(data));
/* Print the sum of the largest values from nums and data */
System.out.println("findLargest(nums) + findLargest(data) => " + (findLargest(nums) + findLargest(data)));
/* Find the length of the longest continuous series of positive numbers */
System.out.println("positiveStreakLength(data) == " + positiveStreakLength(data));
}
}