3
I am having problems implementing an Exception class. The explanation of the program is:
Vector strings must be initialized with "", not null.
The vector capacity shall be capable of being changed by a method.
The class must have as attributes (object variables) a String[] and an int.
Create the methods 'String at( int i )' and 'void put( int i, String st )', which respectively return the String at position i and change the value of the String at position i.
Each access must be checked and if there is a limit error, the Arrayindexoutofbounds exception must be launched.
Create a method 'int find( String st )' that returns the position of St in the array, or -1 if it is not there.
If an array is resized to a smaller size than the current one, one must first eliminate the empty Strings ("") and verify that the new size holds the remaining Strings. If it does not behave, you must launch a Vectorsizeexception exception (create this class).
The task explained is there, so I just need to know how to implement this exception.
My code:
To test class (cannot be changed):
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
}
}
The class Stringvector (I implemented, can be changed):
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();
}*/
}
}
}
}
And my attempt to exception class:
package Lab4;
public class VectorSizeException extends Exception {
private static final long serialVersionUID = 1L;
public VectorSizeException () {
System.out.println("erro: VectorSizeException");
}
}
Can’t do what, specifically?
– Jéf Bueno
How so? Where should the exception stand?
– Daniel La Rubia
What don’t you know how to do? Cast the exception?
– Jéf Bueno
Ah, yes. I need to implement the Exception class with that name there. ).
– Daniel La Rubia
Exactly, I don’t know if my exception class is created properly, and I don’t know how to cast
– Daniel La Rubia
I’m making an explanation for you =D
– Jéf Bueno
thanks, thanks mt :D
– Daniel La Rubia