Skip to content

Commit 6f4ea2a

Browse files
authored
Merge pull request mouredev#6934 from Josegs95/main
#7, #8 y #9 - Java
2 parents f95c63e + 2e174c3 commit 6f4ea2a

File tree

3 files changed

+450
-0
lines changed

3 files changed

+450
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import java.util.*;
2+
3+
public class Josegs95 {
4+
public static void main(String[] args) {
5+
//Pilas. En java existe una clase que representa una pila, la clase Stack.
6+
Stack<String> bag = new Stack<>();
7+
bag.push("Bola roja"); //Sirve para añadir un elemento al final de la pila
8+
bag.push("Bola negra");
9+
bag.push("Bola azul");
10+
System.out.println("La pila: " + bag); //Out: 'La pila: [Bola roja, Bola negra, Bola azul]'
11+
12+
bag.pop(); //Sirve para sacar el último elemento de la pila
13+
System.out.println("La pila: " + bag); //Out: 'La pila: [Bola roja, Bola negra]'
14+
15+
//Implementación de pila a través de un arrayList
16+
List<String> stack = new ArrayList<>();
17+
stack.add("Elemento 1");
18+
stack.add("Elemento 2");
19+
stack.add("Elemento 3");
20+
System.out.println("Mi stack: " + stack); //Out: 'Mi stack: [Elemento 1, Elemento 2, Elemento 3]'
21+
22+
stack.remove(stack.size() - 1); //Borra y devuelve el último elemento, como si fuera una pila
23+
System.out.println("Mi stack: " + stack); //Out: 'Mi stack: [Elemento 1, Elemento 2]'
24+
25+
//Colas. En java existe una clase que representa una pila, la clase Queue.
26+
Queue<String> processes = new LinkedList<>();
27+
processes.offer("Process 1"); //Añade un elemento
28+
processes.offer("Process 2");
29+
processes.offer("Process 3");
30+
System.out.println("La cola: " + processes); //Out: 'La cola: [Process 1, Process 2, Process 3]'
31+
32+
processes.poll(); //Borra el primer elemento
33+
System.out.println("La cola: " + processes); //Out: 'La cola: [Process 2, Process 3]'
34+
35+
//Implementación de cola a través de un arrayList
36+
List<String> queue = new ArrayList<>();
37+
queue.add("Elemento 1");
38+
queue.add("Elemento 2");
39+
queue.add("Elemento 3");
40+
System.out.println("Mi queue: " + queue); //Out: 'Mi queue: [Elemento 1, Elemento 2, Elemento 3]'
41+
42+
queue.remove(0); //Borra y devuelve el primer elemento, como si fuera una cola
43+
System.out.println("Mi queue: " + queue); //Out: 'Mi queue: [Elemento 1, Elemento 2]'
44+
45+
//Reto
46+
System.out.println("\n");
47+
retoFinalPila();
48+
retoFinalCola();
49+
}
50+
51+
static Scanner sc;
52+
53+
public static void retoFinalPila(){
54+
sc = new Scanner(System.in);
55+
int sitesSize;
56+
57+
List<String> sites = new ArrayList<>();
58+
List<String> cachedSites = new ArrayList<>();
59+
sites.add("Inicio");
60+
61+
app: while(true){
62+
sitesSize = sites.size();
63+
System.out.println("Página actual: " + sites.get(sites.size() - 1));
64+
System.out.print("Escribe el nombre de la web, Adelante/Atrás para moverte o Salir: ");
65+
String reply = sc.nextLine();
66+
switch (reply.toLowerCase()){
67+
case "adelante":
68+
if (cachedSites.size() > 0)
69+
sites.add(cachedSites.remove(cachedSites.size() - 1));
70+
break;
71+
case "atrás", "atras":
72+
if (sitesSize > 1)
73+
cachedSites.add(sites.remove(sites.size() - 1));
74+
break;
75+
case "salir":
76+
break app;
77+
default:
78+
sites.add(reply);
79+
cachedSites.removeAll(cachedSites);
80+
}
81+
}
82+
}
83+
84+
public static void retoFinalCola(){
85+
sc = new Scanner(System.in);
86+
87+
List<String> printer = new ArrayList<>();
88+
89+
app: while(true){
90+
System.out.println("Documentos para imprimir: " + printer.size());
91+
System.out.print("Escribe el nombre de un documento, Imprimir o Salir: ");
92+
String reply = sc.nextLine();
93+
switch (reply.toLowerCase()){
94+
case "imprimir":
95+
if (printer.size() > 0)
96+
System.out.println("Imprimiendo: " + printer.remove(0));
97+
break;
98+
case "salir":
99+
break app;
100+
default:
101+
printer.add(reply);
102+
}
103+
}
104+
}
105+
}
+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import java.util.ArrayList;
2+
import java.util.Collection;
3+
import java.util.Collections;
4+
import java.util.List;
5+
6+
public class Josegs95 {
7+
public static void main(String[] args) {
8+
//Clases
9+
Persona p1 = new Persona("Jose", 29); //Crea un objeto 'p1' de la clase persona
10+
Persona p2 = new Persona("María", 27);
11+
Persona p3 = new Persona("Alberto", 41);
12+
13+
System.out.println("p1: " + p1); //Out: 'p1: Nombre: Jose, edad: 29 años.'
14+
System.out.println("p2: " + p2); //Out: 'p2: Nombre: María, edad: 27 años.'
15+
System.out.println("p3: " + p3); //Out: 'p3: Nombre: Alberto, edad: 41 años.'
16+
17+
p3.setName("Juan Alberto");
18+
19+
System.out.println("p3: " + p3); //Out: 'p3: Nombre: Juan Alberto, edad: 41 años.'
20+
21+
//Reto
22+
System.out.println("\n");
23+
retoFinal();
24+
}
25+
26+
public static void retoFinal(){
27+
//Pila
28+
My_Stack<String> stack = new My_Stack<>();
29+
System.out.println(stack); //Out: 'Stack: []'
30+
31+
stack.push("Element 1");
32+
stack.push("Element 2");
33+
stack.push("Element 3");
34+
stack.print(); //Out: '[Element 3] [Element 2] [Element 1]'
35+
System.out.println(stack); //Out: 'Stack: [Element 1, Element 2, Element 3]'
36+
37+
System.out.println(stack.pop()); //Out: 'Element 3'
38+
stack.print(); //Out: '[Element 2] [Element 1]'
39+
System.out.println(stack); //Out: 'Stack: [Element 1, Element 2]'
40+
41+
System.out.println("\n\n");
42+
//Cola
43+
My_Queue<String> queue = new My_Queue<>();
44+
System.out.println(queue); //Out: 'Queue: []'
45+
46+
queue.add("Element 1");
47+
queue.add("Element 2");
48+
queue.add("Element 3");
49+
queue.print(); //Out: '[Element 1] [Element 2] [Element 3]'
50+
System.out.println(queue); //Out: 'Queue: [Element 1, Element 2, Element 3]'
51+
52+
System.out.println(queue.poll()); //Out: 'Element 1'
53+
queue.print(); //Out: '[Element 2] [Element 3]'
54+
System.out.println(queue); //Out: 'Queue: [Element 2, Element 3]'
55+
}
56+
57+
public static class Persona{
58+
private String name;
59+
private int age;
60+
61+
Persona(String name, int age){
62+
this.name = name;
63+
this.age = age;
64+
}
65+
66+
public String getName() {
67+
return name;
68+
}
69+
70+
public void setName(String name) {
71+
this.name = name;
72+
}
73+
74+
public int getAge() {
75+
return age;
76+
}
77+
78+
public void setAge(int age) {
79+
this.age = age;
80+
}
81+
82+
@Override
83+
public String toString(){
84+
return "Nombre: " + name + ", edad: " + age + " años.";
85+
}
86+
}
87+
88+
//Reto
89+
public static class My_Stack<E>{
90+
List<E> elements;
91+
92+
public My_Stack(Collection<E> collection){
93+
if (collection == null)
94+
elements = new ArrayList<>();
95+
else
96+
elements = new ArrayList<>(collection);
97+
}
98+
public My_Stack(){
99+
this(null);
100+
}
101+
102+
public E push(E element){
103+
elements.add(element);
104+
return element;
105+
}
106+
107+
public E pop(){
108+
if (size() == 0)
109+
return null;
110+
111+
return elements.remove(size() - 1);
112+
}
113+
114+
public int size(){
115+
return elements.size();
116+
}
117+
118+
public void print(){
119+
List<E> cloneList = new ArrayList<>(elements);
120+
Collections.reverse(cloneList);
121+
for (E element : cloneList)
122+
System.out.print("[" + element + "] ");
123+
System.out.println();
124+
}
125+
126+
@Override
127+
public String toString() {
128+
return "Stack: " + elements.toString();
129+
}
130+
}
131+
132+
public static class My_Queue<E>{
133+
List<E> elements;
134+
135+
public My_Queue(Collection<E> collection){
136+
if (collection == null)
137+
elements = new ArrayList<>();
138+
else
139+
elements = new ArrayList<>(collection);
140+
}
141+
public My_Queue(){
142+
this(null);
143+
}
144+
145+
public E add(E element){
146+
elements.add(element);
147+
return element;
148+
}
149+
150+
public E poll(){
151+
if (size() == 0)
152+
return null;
153+
154+
return elements.remove(0);
155+
}
156+
157+
public int size(){
158+
return elements.size();
159+
}
160+
161+
public void print(){
162+
for (E element : elements)
163+
System.out.print("[" + element + "] ");
164+
System.out.println();
165+
}
166+
167+
@Override
168+
public String toString() {
169+
return "Queue: " + elements.toString();
170+
}
171+
}
172+
}

0 commit comments

Comments
 (0)