Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. As strings do vetor devem estar inicializadas com "", e não com null.
  2. A capacidade do vetor deve poder ser alterada através de um método.
  3. A classe deve ter como atributos (variáveis do objeto) um String[] e um int.
  4. Crie os métodos 'String at( int i )' e 'void put( int i, String st )', que respectivamente retornam a String na posição i e mudam o valor da String na posição i.
  5. Cada acesso deve ser verificado e se houver um erro de limites, a exceção ArrayIndexOutOfBounds deve ser lançada.
  6. Crie um método 'int find( String st )' que retorna a posição de St no vetor, ou -1 se ele não está lá.
  7. Se um vetor for redimensionado para um tamanho inferior ao atual, deve-se primeiro eliminar as Strings vazias ("") e verificar se o novo tamanho comporta as Strings restantes. Se não comportar, deve-se lançar uma exceção VectorSizeException (crie essa classe).
  8.  
  9. package Lab4;
  10.  
  11. public class Teste {
  12.  
  13. public static void main( String argc[] ) {
  14. StringVector v = new StringVector( 10 );
  15.  
  16. v.put( 1, "Janeiro ");
  17. v.put( 2, "fevereiro" );
  18.  
  19. System.out.println( v.at( 3 ) );
  20.  
  21. System.out.println( v.at( 13 ) ); // Exceção
  22.  
  23. System.out.println( v.at(1));
  24.  
  25. v.newSize( 2 ); // OK
  26.  
  27. v.newSize( 1 ); // Exceção
  28. }
  29. }
  30.  
  31. package Lab4;
  32.  
  33. public class StringVector {
  34.  
  35.  
  36. String[] vetor;
  37. int inteiro;
  38.  
  39. public StringVector (int vectorCapacity) {
  40. vetor = new String[vectorCapacity];
  41. for(int i = 0 ; i < vetor.length ; i++) {
  42. vetor[i] = "";
  43. }
  44. }
  45.  
  46.  
  47. public void newSize (int newSize) {
  48.  
  49.  
  50. String[] novo = new String[ newSize ];
  51.  
  52. if (newSize < vetor.length) {
  53.  
  54. while(find("") != -1) {
  55. vetor[find("")] = null;
  56. }
  57.  
  58. /*print pra testar o find e a substituição de valores
  59. int i = 0;
  60. while (i < vetor.length) {
  61. System.out.println(vetor[i]);
  62. i++;
  63. }
  64. */
  65.  
  66. completeVector(novo);
  67.  
  68. /* try {
  69. completeVector(novo);
  70. } catch (ArrayIndexOutOfBoundsException error) {
  71. System.out.println("erro: "+ error);
  72. }
  73. */
  74. }
  75.  
  76. else {
  77. completeVector(novo);
  78.  
  79. }
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86. public String at(int inteiro) {
  87. try {
  88. return vetor[inteiro];
  89. } catch (ArrayIndexOutOfBoundsException error) {
  90. return "erro: " + error;
  91. }
  92.  
  93. }
  94.  
  95.  
  96. public void put(int inteiro, String st) {
  97. try {
  98. vetor[inteiro] = st;
  99. } catch (ArrayIndexOutOfBoundsException error) {
  100. System.out.println("erro: " + error);
  101. }
  102. }
  103.  
  104.  
  105. public int find(String st) {
  106. for(int i = 0 ; i < vetor.length ; i++) {
  107. if(st.equals(vetor[i])) return i;
  108. }
  109. return -1;
  110. }
  111.  
  112.  
  113. public void completeVector (String[] novo) {
  114. int position = 0;
  115. for(int i = 0; i < vetor.length ; i++) {
  116. if(vetor[i] != null ) {
  117. novo[position] = vetor[i];
  118. position++;
  119. /*if(position > newSize) {
  120. throw new VectorSizeException();
  121. }*/
  122. }
  123. }
  124. }
  125.  
  126. }
  127.  
  128. package Lab4;
  129.  
  130. public class VectorSizeException extends Exception {
  131.  
  132. private static final long serialVersionUID = 1L;
  133.  
  134. public VectorSizeException () {
  135. System.out.println("erro: VectorSizeException");
  136. }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement