package com.groovys
class GroovyMain{
static void main(args){
println "Curso Groovy"
tipos()
aserciones()
operadores()
secuenciales()
iterativas()
cadenas()
excepciones()
}
static void cadenas(){
println "**Cadenas**"
def cadena = "ABC"
println cadena.length()
println cadena[1]
println cadena[2]
println cadena.indexOf('B')
println cadena[0..1]
println cadena.substring(0,2);
String[] array = "1,2,3,4,".split(",")
for(a in array){
println "${a}"
}
cadena = cadena.replace("A","X").concat(".").toLowerCase()
println "${cadena}"
println "${cadena.toList()}"
println " Ribola "*3
boolean verdadero = "A".equalsIgnoreCase("a");
if(verdadero){
println "Son iguales"
}
}
static void excepciones(){
println "**try-finally**"
try{
println "En el try"
}finally{
println "En el finally"
}
println "**ArithmeticException**"
try{
int division = 3/0;
}catch(ArithmeticException ae){
println "Ha ocurrido una ArithmeticException!!"
println "Mensaje: "+ae.getMessage()
}catch(NullPointerException npe){
println "Ha ocurrido una NullPointerException"
}
String cad = null;
println "**NullPointerException**"
try {
println cad.trim()
}
catch(NullPointerException e) {
println "Ha ocurrido una NullPointerException:\n${e.toString()}"
println "Causa: ${e.getCause()}"
println "Mensaje: ${e.getMessage()}"
}
if(cad == null){
throw new MyExcepcion("Esto provocara un NullPointerException");
}
if(cad == null){
throw new Exception("La cadena es nula: "+cad);
}
}
static void iterativas(){
def cont = 0
final int MAX = 5
println "Ciclo while:"
while(cont++ < MAX) {
println "${cont}"
}
println "Uso de for in :"
for(n in 0..5){
println "${n}"
}
println "Uso de x.upto(y):"
1.upto(5){
println it
}
println "Uso de x.times:"
5.times { i ->
println "${i}"
}
println "Uso del clasico for:"
for(int i=0; i < MAX; i++){
println "${i}"
}
println "Uso de x.step(y):"
1.step(10, 2) { i ->
println "${i}"
}
}
static void secuenciales(){
println "Uso de if-else if-else:"
def edad = 12
if(edad == 18){
println "Puedes votar."
}
else if(edad >= 18){
println "Puedes votar y comprar un auto."
}
else{
println "No puedes votar ni comprar un auto."
}
println "Uso de switch:"
def opc = 11
switch(opc) {
case 9:
println "9"
break
case 10:
println "10"
break
case {9+2}:
println "11"
break
default:
println "default"
break
}
}
static void operadores(){
println "Operadores logicos:"
println "5 > 7 : "+(5 > 7)
println "11 == 11 : "+(11 == 11)
println "true || false : "+(true || false)
println "true && true : "+(true && true)
println "!true : "+(!true)
println "!false : "+(!false)
def salida = (true == true)? "Es verdadero" : "Es falso"
println "${salida}"
}
static void aserciones(){
println "Usando aserciones:"
assert 1 + 2 == 3
assert Boolean.TRUE == !(3>7)
assert 9/5 == 1.8
assert 10%6 == 4
assert true || false
assert true && true
assert 11 == 11
assert (!'') == true
assert (!false) == true
assert (!'foo') == false
}
static void tipos(){
//8 tipos
println "Tipos Byte:"
println Byte.MIN_VALUE
println Byte.MAX_VALUE
print "Tipos Short:"
println Short.MIN_VALUE
println Short.MAX_VALUE
println "Tipos Integer:"
println Integer.MIN_VALUE
println Integer.MAX_VALUE
println "Tipos Long:"
println Long.MIN_VALUE
println Long.MAX_VALUE
println "Tipos Float:"
println Float.MIN_VALUE
println Float.MAX_VALUE
println "Tipos Double:"
println Double.MIN_VALUE
println Double.MAX_VALUE
println "Tipos Boolean:"
println Boolean.TRUE
println Boolean.FALSE
println "Tipos Character:"
println Character.MIN_VALUE
println Character.MAX_VALUE
}
}
class MyExcepcion extends Exception{
public MyExcepcion(String e){
super(e);
}
}
Groovy en ejemplos
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.