First of all apologize for a title that does not specify much and that is that I could not think of a better one. I am making a program for programming classes in java.
The first problem I find is in the constructors.
b) a constructor that will allow us to indicate the initial capacity of our dynamic array. In case the indicated capacity is a negative value, an IllegalArgumentException will be thrown with the message “Invalid size: ” and the requested size
c) a constructor that will allow us to initialize our object from an array of character strings
d) a constructor that will allow us to initialize our object from another D ynStrArray object
In cases c) and d), if the passed argument is a null reference, an IrrivalArgumentException will be thrown with the corresponding text
The code I have for the constructors is the following
public DynStrArray() {
this.arCadena = new String[10];
}
public DynStrArray(int n) {
try {
if (n >= 0) {
this.arCadena = new String[n];
}
} catch (IllegalArgumentException | NullPointerException e) {
System.out.println("Tamaño no valido: " + n);
System.out.println(e.getMessage());
}
}
public DynStrArray(String[] arCadena) {
this.arCadena = arCadena;
try {
if (arCadena != null) {
this.arCadena = arCadena;
}
} catch (IllegalArgumentException e) {
System.out.println("Valor nulo: " + e.getMessage());
}
}
public DynStrArray(DynStrArray d){
try {
if (d != null) {
this.arCadena = d.getArCadena();
}
} catch (IllegalArgumentException e) {
System.out.println("Valor nulo: " + e.getMessage());
}
}
The problems I have with these constructors:
When I use the first constructor to create an instance of the class passing an index as a parameter, when I put it negative, it does not get the error message and continues, when I do a print to show the instance, it gives me a NullPointerException error. When I do a print of the array I get null. At no time do I get the catch message
In the other two, I pass null values, but even so, it creates the instance for me without the catch coming out.
The second of the problems I have is with a foreach. I try from an index to move the elements one position so that said index is free and thus insert a new value in it
add(i ndex, string ) : inserts the string at the indicated position. In case the indicated position is incorrect, it will throw an IndexOutOfBoundsException exception with the message “Invalid position: ” and the requested position. In case there is no space available, the size of the array will be increased by 50%.
The code I have for this method is the following
public void add(int index, String s){
try {
while (index > this.arCadena.length - 1) {
increment();
}
if (this.arCadena[index] != null || "".equals(this.arCadena[index])) {
String[] arS = this.arCadena;
for (int i = 0; i < this.arCadena.length - 1; i++) {
this.arCadena[i + 1] = arS[i];
}
}
this.arCadena[index] = s;
} catch (IndexOutOfBoundsException e) {
System.out.println("Posición no válida: " + index);
System.out.println(e.getMessage());
}
}
What makes me leave the index that I want to have free to null and copy its value to the following ones, thus obtaining that repeated value and it does not store the value in the index.
I think you have a confusion when you are asked to throw an exception, what you are doing is catching it. To throw exceptions you must use the statement
throw
and the exception type. For example for the constructor b it would be like this:throw new IllegalArgumentException("Tamaño no válido: " + n);
I put a possible solution to your sections b, c and d:
Now all you have to do is catch the exception when you create the instance, like so:
In the case of adding a string to the array, I would do it like this:
And now you need to catch the exception when you call that function: