I'm having trouble implementing an Exception class. The explanation of the program is:
Vector strings must be initialized with "" instead of null. "
The capacity of the vector must be able to be changed by a method.
- The class must have a String [] and an int as attributes (object variables).
Create 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 String at position i. >
Each access must be checked and if there is a boundary error, the ArrayIndexOutOfBounds exception should be thrown.
Create a 'int find (String st)' method that returns the position of St in the vector, or -1 if it is not there.
If a vector is scaled down to a current size, you must first delete the empty Strings ("") and check if the new size contains the remaining Strings. If it does not, you should throw a VectorSizeException exception (create this class).
The task explained is there, so I just need to know how to implement this exception.
My code:
The Test class (can not 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've 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 at Exception class:
package Lab4;
public class VectorSizeException extends Exception {
private static final long serialVersionUID = 1L;
public VectorSizeException () {
System.out.println("erro: VectorSizeException");
}
}