Skip to content

Instantly share code, notes, and snippets.

@guinetik
Last active August 29, 2015 13:56
Show Gist options
  • Save guinetik/9086926 to your computer and use it in GitHub Desktop.
Save guinetik/9086926 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
public class HelloWorld{
private int max;
private ArrayList<String> lista;
public static int MAX = 10;
public HelloWorld(int max) throws Exception {
if(max > 0 && max < MAX) {
this.max = max;
lista = new ArrayList();
} else {
throw new Exception("argumento invalido");
}
}
public int adicionaNaLista(String item) throws Exception {
if(lista.size() < max) {
lista.add(item);
return lista.size();
} else throw new Exception("alcancou o maximo");
}
public static void main(String []args){
System.out.println("Hello World");
try {
HelloWorld teste = new HelloWorld(3);
System.out.println("adicionando. a lista agora tem " + teste.adicionaNaLista("um") + " itens");
System.out.println("adicionando. a lista agora tem " + teste.adicionaNaLista("dois") + " itens");
System.out.println("adicionando. a lista agora tem " + teste.adicionaNaLista("tres") + " itens");
System.out.println("adicionando. a lista agora tem " + teste.adicionaNaLista("quatro") + " itens");
System.out.println("vai travar...");
} catch(Exception e) {
System.out.println("travou");
System.out.println("erro: " + e);
}
try {
HelloWorld teste = new HelloWorld(-1);
} catch(Exception e) {
System.out.println("travou");
System.out.println("erro: " + e);
}
try {
HelloWorld teste = new HelloWorld(100);
} catch(Exception e) {
System.out.println("travou");
System.out.println("erro: " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment