-
-
Save alvareztech/f17faf564e06abf2560c2d3ad78fbee3 to your computer and use it in GitHub Desktop.
Java: Creación y despliegue de una Pila (Stack) de objetos carta. Para mostrar la pila se utiliza un método estático.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Carta { | |
private String numero; | |
private String color; | |
private String simbolo; | |
public Carta(String numero, String color, String simbolo) { | |
this.numero = numero; | |
this.color = color; | |
this.simbolo = simbolo; | |
} | |
public Carta() { | |
this.numero ="" ; | |
this.color = ""; | |
this.simbolo = ""; | |
} | |
public String getNumero() { | |
return numero; | |
} | |
public void setNumero(String numero) { | |
this.numero = numero; | |
} | |
public String getColor() { | |
return color; | |
} | |
public void setColor(String color) { | |
this.color = color; | |
} | |
public String getSimbolo() { | |
return simbolo; | |
} | |
public void setSimbolo(String simbolo) { | |
this.simbolo = simbolo; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Stack; | |
public class Main { | |
public static void main(String[] args) { | |
Stack<Carta> misCartas = new Stack<Carta>(); | |
Carta c1 = new Carta("5", "rojo", "Trevol"); | |
Carta c2 = new Carta("2", "negro", "corazones"); | |
Carta c3 = new Carta("3", "rojo", "diamante"); | |
Carta c4 = new Carta("6", "negro", "trevol"); | |
Carta c5 = new Carta("7", "negro", "diamante"); | |
Carta c6 = new Carta("9", "rojo", "trevol"); | |
Carta c7 = new Carta("8", "negro", "trevol"); | |
misCartas.push(c1); | |
misCartas.push(c2); | |
misCartas.push(c3); | |
misCartas.push(c4); | |
misCartas.push(c5); | |
misCartas.push(c6); | |
misCartas.push(c7); | |
mostrar(misCartas); | |
} | |
public static void mostrar(Stack<Carta> a) { | |
Stack<Carta> aux = new Stack<Carta>(); | |
while (!a.isEmpty()) { | |
Carta x = a.pop(); | |
System.out.println(x.getNumero() + " " + x.getColor() + " " + x.getSimbolo()); | |
aux.push(x); | |
} | |
while (!aux.isEmpty()) { | |
Carta b = aux.pop(); | |
a.push(b); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment