Skip to content

Commit 5c5e682

Browse files
committed
#8 - Java
1 parent 928ca1d commit 5c5e682

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
+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)