-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBasicJava.java
More file actions
184 lines (133 loc) · 3.46 KB
/
BasicJava.java
File metadata and controls
184 lines (133 loc) · 3.46 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
import java.util.ArrayList;
public class BasicJava {
public void numbers() {
System.out.println("1.----- Todos los n�mero del 1 al 255 ------");
int i = 1;
while (i <= 255) {
System.out.print(i + ", ");
i++;
}
}
public void imparNumbers() {
System.out.println("\n\n2.----- N�meros impares del 1 al 255 ------");
for (int i = 1; i <= 255; i++) {
if (i % 2 != 0) {
System.out.print(i + ", ");
}
}
}
public void suma() {
System.out.println("\n\n3.----- Suma 1 al 255 ------");
int total = 0;
for (int i = 0; i <= 255; i++) {
total += i;
System.out.printf("Numero nuevo: %d Suma: %d \n", i, total);
}
}
public void array() {
System.out.println("\n\n4.----- Recorrer un arreglo ------");
int[] arr = { 1, 3, 5, 7, 9, 13 };
for (int i : arr) {
System.out.println(i);
}
}
public void maxNum() {
System.out.println("\n\n5.----- Encontrar el maximo numero ------");
int[] arr = { 1, 3, 42, 9, 48, 2, -4, -99 };
int maxNum = arr[0];
for (int i : arr) {
System.out.print(i + ", ");
if (maxNum < i) {
maxNum = i;
}
}
System.out.println("\nNumero Maximo: " + maxNum);
}
public void promedio() {
System.out.println("\n\n6.----- Obtener el promedio ------");
int[] arr = { 10, 10, 4 };
double total = 0;
double promedio = 0;
for (int i : arr) {
total += i;
}
promedio = Math.floor(total / arr.length * 100) / 100;
System.out.println("Promedio: " + promedio);
}
public void imparArray() {
System.out.println("\n\n7.----- Arreglo de numeros impares ------");
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 1; i <= 255; i++) {
if (i % 2 != 0) {
arr.add(i);
}
}
System.out.println(arr);
}
public void maxY() {
System.out.println("\n\n8.----- Mayor que Y ------");
int[] arr = { 1, 3, 5, 7 };
int y = 3;
int count = 0;
for (int i : arr) {
if (i >= y) {
count++;
}
}
System.out.println("Cantidad de numeros > 3(y): " + count);
}
public void numCuadrado() {
System.out.println("\n\n9.----- Valores al cuadrado ------");
ArrayList<Integer> arrPow = new ArrayList<Integer>();
int[] arr = { 1, 5, 10, -2 };
for (int i : arr) {
arrPow.add((int) Math.pow(i, 2));
}
System.out.println(arrPow);
}
public void eliminarNumNegativo() {
System.out.println("\n\n10.----- Eliminar numeros negativos ------");
int[] arr = { 1, 5, 10, -2 };
ArrayList<Integer> arrPositivo = new ArrayList<Integer>();
for (int i : arr) {
if (i > 0) {
arrPositivo.add(i);
} else if (i < 0) {
arrPositivo.add(0);
}
}
System.out.println(arrPositivo);
}
public void minMaxPromedio() {
System.out.println("\n\n11.----- Minimo, maximo y promedio ------");
int[] arr = { 1, 5, 10, -2 };
ArrayList<Object> arrResult = new ArrayList<Object>();
int max = 0;
int min = 0;
double suma = 0;
double promedio = 0;
for (int i : arr) {
if (i > max) {
max = i;
} else if (i < min) {
min = i;
}
suma += i;
}
promedio = Math.floor(suma / arr.length * 100) / 100;
arrResult.add(max);
arrResult.add(min);
arrResult.add(promedio);
System.out.println(arrResult);
}
public void combinarArray() {
System.out.println("\n\n12.----- Combinando los valores del Arreglo ------");
int[] arr = { 1, 5, 10, 7, -2 };
ArrayList<Object> arrResult = new ArrayList<Object>();
for (int i = 1; i < arr.length; i++) {
arrResult.add(arr[i]);
}
arrResult.add(0);
System.out.println(arrResult);
}
}