-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayPrac.java
More file actions
170 lines (142 loc) · 5.1 KB
/
Copy pathArrayPrac.java
File metadata and controls
170 lines (142 loc) · 5.1 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
package stringAndArrays;
public class ArrayPrac
{
public static void main(String[] args)
{
int [] oneToTenArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int [] randomArray = {5, 8, 5, 5, 1, 3, 0, 4, 5, 10, 11, 14, 22};
//test the swap feature a couple times
System.out.println("Testing swap...");
System.out.print("Our inital array: ");
printArray(oneToTenArray);
swap(oneToTenArray, 0, 9);
System.out.println("Swap first and last to get: ");
printArray(oneToTenArray);
swap(oneToTenArray, 1, 2);
System.out.println("Swap second and third to get: ");
printArray(oneToTenArray);
//swap them back to put the numbers in order
swap(oneToTenArray, 9, 0);
swap(oneToTenArray, 2, 1);
System.out.println("Swap them back to get: ");
printArray(oneToTenArray);
//check the countOf method a few times
System.out.println();
System.out.println("testing countOf...");
System.out.println("How many 9s are in our 1-10 array?");
System.out.println("Answer: " + countOf(oneToTenArray, 9));
System.out.println("How many 12s are in our 1-10 array?");
System.out.println("Answer: " + countOf(oneToTenArray, 12));
System.out.println("How many 5s are in our random array?");
printArray(randomArray);
System.out.println("Answer: " + countOf(randomArray, 5));
System.out.println();
//check the isInOrder method
System.out.println("checking isInOrder...");
System.out.println("Is the oneToTen array in order?");
System.out.println("Answer: " + isInOrder(oneToTenArray));
System.out.println("Is the random array in order?");
System.out.println("Answer: " + isInOrder(randomArray));
System.out.println();
//check the first reverse method
System.out.println("testing first reverse method...");
System.out.println("We can reverse the one to ten array:");
oneToTenArray = reverse(oneToTenArray);
printArray(oneToTenArray);
System.out.println("Reverse it back!");
oneToTenArray = reverse(oneToTenArray);
printArray(oneToTenArray);
System.out.println();
//check the second reverse method
System.out.println("Checking second reverse method");
System.out.println("I'm making a new array that is reverse of one to ten");
int [] newArray = getReverseArray(oneToTenArray);
printArray(newArray);
System.out.print("Here is the random array: ");
printArray(randomArray);
System.out.println("Here it is reversed: ");
int[] anotherArray = getReverseArray(randomArray);
printArray(anotherArray);
System.out.println();
System.out.println("Here are our original arrays: ");
printArray(oneToTenArray);
printArray(randomArray);
System.out.println(ArraysAssignment.arraySize(oneToTenArray));
}
private static void printArray(int[] anIntArray)
{
//this method simply prints the elements of an integer array (horizontally), with one space
//between each element
for (int k = 0; k < anIntArray.length; k++)
{
System.out.print(anIntArray[k] + " ");
}
System.out.println("\n");
}
private static void swap(int[] anIntArray, int i, int j)
{
int temp = anIntArray[i];
anIntArray [i] = anIntArray [j];
anIntArray [j] = temp;
}
private static int countOf(int[] anIntArray, int n) {
//this method returns the number of occurrences of the integer n in an array
int count = 0;
for (int l = 0; l < anIntArray.length; l++)
{
if (anIntArray[l] == n)
{
count ++;
}
else {
//do nothing
}
}
return count;
}
private static boolean isInOrder(int[] anIntArray)
{
//this method returns true if anIntArray is in order, false otherwise
boolean checkOrder = false;
int m = 0;
while (m < (anIntArray.length - 1))
{
if (anIntArray[m] < anIntArray[m + 1])
{
checkOrder = true;
m++;
}
else
{
checkOrder = false;
m = anIntArray.length;
}
}
return checkOrder;
}
private static int [] reverse(int[] anIntArray)
{
//this method reverses the order of an anIntArray
int [] tempArray = new int[anIntArray.length];
int count = 0;
for (int n = (tempArray.length - 1); n>= 0; n--)
{
tempArray [count] = anIntArray[n];
count++;
}
return tempArray;
}
private static int[] getReverseArray(int[] anIntArray) {
//this method returns an array that has the elements of anIntArray, but does
//not change anIntArray
//this method reverses the order of an anIntArray
int [] tempArray = new int[anIntArray.length];
int count = 0;
for (int n = (tempArray.length - 1); n>= 0; n--)
{
tempArray [count] = anIntArray[n];
count++;
}
return tempArray;
}
}