-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
206 lines (158 loc) · 6.66 KB
/
Main.java
File metadata and controls
206 lines (158 loc) · 6.66 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
200
201
202
203
204
205
206
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import javax.swing.*;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
int[] arrA = {1, 2, 3, 4, 5};
int num = arrA.length;
int sumA = 0;
int maxA = arrA[0];
int[] arrB = new int[num];
int[] arrC = new int[num];
// 1. copying array A in array B and inverse order copying array A in array C
for (int i = 0; i < arrA.length; i++) {
arrB[i] = arrA[i];
arrC[arrA.length - 1 - i] = arrA[i];
}
System.out.println("Homework(B) - #1");
System.out.println(Arrays.toString(arrA));
System.out.println("\nHomework(B) - #1.1");
System.out.println(Arrays.toString(arrB));
System.out.println("\nHomework(B) - #1.2");
System.out.println(Arrays.toString(arrC));
System.out.println("\n------------------------------------------");
// 2 & 3. Sum of the array A elements using for-each and the max element of array A
for (int a : arrA) {
if (a >= maxA) {
maxA = a;
}
sumA += a;
}
System.out.println("Homework(B) - #2");
System.out.println(sumA);
System.out.println("\n------------------------------------------");
System.out.println("Homework(B) - #3");
System.out.println(maxA);
System.out.println("\n------------------------------------------");
// 4. Printing odd and even elements separately.
System.out.println("\nHomework(B) - #4");
System.out.println("Even numbers: ");
for (int b : arrA) {
if (b % 2 == 0)
System.out.println(b);
}
System.out.println("Odd numbers: ");
for (int b : arrA) {
if (b % 2 != 0)
System.out.println(b);
}
System.out.println("The sum of array A is " + sumA + " and the max element is " + maxA);
System.out.println("\n------------------------------------------");
System.out.println("\nHomework(B) - #5");
// 5. Showing only even index in one array. I did not understand completely the question.
char[] arrD = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
for (int i = 0; i < arrD.length; i++) {
if (i % 2 == 0)
System.out.print(arrD[i] + ", ");
}
System.out.println("\n------------------------------------------");
// 6. Sorting. Q1: Arrays.sort() method sort the array from small to great?
System.out.println("\nHomework(B) - #6");
// Q2: Why this line does not work? int[] arrG = Arrays.sort(arrF); The error says that it returns void.
// Q3: What if I want to create the descending array in a new array. Array.clone()?
int[] arrF = {1, 2, 3, 2, 5, 1, 4, 2, 5};
// Arrays.sort(arrF);
// int[] arrG = arrF;
for (int i = 0; i < arrF.length - 1; i++) {
for (int j = 0; j < arrF.length - 1 - i; j++) { //1
if (arrF[j] < arrF[j + 1]) {
int temp = arrF[j];
arrF[j] = arrF[j + 1];
arrF[j + 1] = temp;
}
}
}
System.out.print(Arrays.toString(arrF));
System.out.println("\n------------------------------------------");
//7. Deleting repetitive elements in an array
System.out.println("\nHomework(B) - #7");
int[] arrH = {1, 2, 3, 2, 5, 1, 4, 2, 5}; // [1, 1, 2, 2, 2, 3, 4, 5, 5]
Arrays.sort(arrH);
int N = 1; // I fix it to one because the code below counts the number of transitions though miss one group of numbers
for (int i = 1; i < arrH.length; i++) {
//for (int j = 0; j < i; j++){
if (arrH[i] != arrH[i - 1])
N++; //5
}
//}
int[] arrHRefined = new int[N];
arrHRefined[0] = arrH[0]; // I need to fix the first element if not as I start i from 0 I will miss the first element. If I start
int index = 1;
// i from 0 my loop goes to one element more than array length
for (int i = 1; i < arrH.length; i++) {
if (arrH[i] != arrH[i - 1]) {
arrHRefined[index++] = arrH[i];
}
}
System.out.println(Arrays.toString(arrH));
System.out.println(Arrays.toString(arrHRefined));
System.out.print(N);
System.out.println("\n------------------------------------------");
//8. Not resolved
System.out.println("\nHomework(B) - #8");
int[] defArr = {1, 2, 3, 2, 5, 1, 4, 2, 5};
// defArr = {1, 2, 3,-1, 5,-1, 4,-1,-1};
// fr = {2, 3, 1, 0, 2, 0, 1, 0, 0};
// newArr= {0, 0, 0}
int count;
int n = 0;
int[] fr = new int[defArr.length]; // {0, 0, 0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < defArr.length - 1; i++) {
if (defArr[i] == -1)
continue;
count = 1;
for (int j = i + 1; j < defArr.length; j++) {
if (defArr[i] == defArr[j]) {
defArr[j] = -1; // refine repetitive item
count++; // count how many times each item repeated
}
}
fr[i] = count;
if (count > 1)
n++;
}
int[] newArr = new int[n]; // newArr= {0, 0, 0}
int indx = 0;
for (int k = 0; k < defArr.length; k++) {
if (fr[k] > 1) {
newArr[indx] = defArr[k];
System.out.println("frequency of " + newArr[indx++] + " is: " + fr[k]);
}
}
System.out.println(Arrays.toString(newArr));
System.out.println("\n------------------------------------------");
System.out.println("\nHomework(A) - #5");
Scanner scanner = new Scanner(System.in);
double a, b, c, max;
System.out.println("1st digit");
a = scanner.nextDouble();
System.out.println("2nd digit");
b = scanner.nextDouble();
System.out.println("3rd digit");
c = scanner.nextDouble();
// max = a;
// if (b> max)
// max = b;
// if (c > max)
// max = c;
// max = a;
// max = b> max ? b : max;
max = a > b ? a : b;
max = c > max ? c : max;
System.out.println("max is : " + max);
}
}