Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1.  
  2. package ed_ficha4;
  3.  
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7.  
  8. public class DezReais implements Iterable<Double> {
  9.  
  10. private final int TAMANHO = 10;
  11. private Double []tab = new Double[TAMANHO];
  12. private int last = 0;
  13.  
  14. public boolean add(Double d){
  15.  
  16. if(size() >= TAMANHO)throw new RuntimeException();
  17. tab[last++] = d;
  18. return true;
  19. }
  20.  
  21.  
  22. public int size(){
  23. return last;
  24. }
  25.  
  26. public Double get (int pos){
  27. return tab[pos];
  28. }
  29.  
  30. @Override
  31. public Iterator<Double> iterator() {
  32. return new ItDezReais(this) {};
  33. }
  34.  
  35. }
  36.  
  37. class ItDezReais implements Iterator<Double>{
  38.  
  39. DezReais lista;
  40. int contador = -1;
  41.  
  42. ItDezReais (DezReais d){
  43. lista = d;
  44. }
  45.  
  46. @Override
  47. public boolean hasNext() {
  48. return contador+1 < lista.size();
  49. }
  50.  
  51. @Override
  52. public Double next() {
  53. if(contador < 10)
  54. return lista.get(++contador);
  55. else
  56. throw new NoSuchElementException();
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement