Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- As strings do vetor devem estar inicializadas com "", e não com null.
- A capacidade do vetor deve poder ser alterada através de um método.
- A classe deve ter como atributos (variáveis do objeto) um String[] e um int.
- 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.
- Cada acesso deve ser verificado e se houver um erro de limites, a exceção ArrayIndexOutOfBounds deve ser lançada.
- 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á.
- 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).
- package Lab4;
- public class Teste {
- public static void main( String argc[] ) {
- StringVector v = new StringVector( 10 );
- v.put( 1, "Janeiro ");
- v.put( 2, "fevereiro" );
- System.out.println( v.at( 3 ) );
- System.out.println( v.at( 13 ) ); // Exceção
- System.out.println( v.at(1));
- v.newSize( 2 ); // OK
- v.newSize( 1 ); // Exceção
- }
- }
- package Lab4;
- public class StringVector {
- String[] vetor;
- int inteiro;
- public StringVector (int vectorCapacity) {
- vetor = new String[vectorCapacity];
- for(int i = 0 ; i < vetor.length ; i++) {
- vetor[i] = "";
- }
- }
- public void newSize (int newSize) {
- String[] novo = new String[ newSize ];
- if (newSize < vetor.length) {
- while(find("") != -1) {
- vetor[find("")] = null;
- }
- /*print pra testar o find e a substituição de valores
- int i = 0;
- while (i < vetor.length) {
- System.out.println(vetor[i]);
- i++;
- }
- */
- completeVector(novo);
- /* try {
- completeVector(novo);
- } catch (ArrayIndexOutOfBoundsException error) {
- System.out.println("erro: "+ error);
- }
- */
- }
- else {
- completeVector(novo);
- }
- }
- public String at(int inteiro) {
- try {
- return vetor[inteiro];
- } catch (ArrayIndexOutOfBoundsException error) {
- return "erro: " + error;
- }
- }
- public void put(int inteiro, String st) {
- try {
- vetor[inteiro] = st;
- } catch (ArrayIndexOutOfBoundsException error) {
- System.out.println("erro: " + error);
- }
- }
- public int find(String st) {
- for(int i = 0 ; i < vetor.length ; i++) {
- if(st.equals(vetor[i])) return i;
- }
- return -1;
- }
- public void completeVector (String[] novo) {
- int position = 0;
- for(int i = 0; i < vetor.length ; i++) {
- if(vetor[i] != null ) {
- novo[position] = vetor[i];
- position++;
- /*if(position > newSize) {
- throw new VectorSizeException();
- }*/
- }
- }
- }
- }
- package Lab4;
- public class VectorSizeException extends Exception {
- private static final long serialVersionUID = 1L;
- public VectorSizeException () {
- System.out.println("erro: VectorSizeException");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement