I have to write two files for an assignment, a client file that will receive two sets of data from the user. The first set is a list of applicant ID’s (int) and the second set is either y or n (char). The sets of data need to be entered into two arrays.
I then have to create a class file that will receive these two arrays into a constructor which will then run through some specific methods to validate if the answers are correct, how many answered correctly and finally which candidates received the most correct answers.
I just have two questions:
1st – the first line of data that will be entered into the second array will be the answer key. Is it possible to add a 0 to the beginning of the applicant ID’s array so that the two arrays will match (ID#{1} to answer{1}, etc)?
2nd – with the calling of my class file from my client file do I declare the array’s in both?
i.e.:
client file:
int[] id = new int[];
char[] answer = new char[];
ValidateMark importClerk = new ValidateMark(id, answer);
class constructor:
public ValidateMark(int number[], char marks[])
Thanks in advance
I am going to assume the data structures are required and specified as part of the assignment and not give any input on alternate ones to use unless asked otherwise.
First Question:
Yes you can, but not in the manner you are thinking. Arrays, once declared, occupy a static amount of memory with a static number of elements. Therefore, you can’t “insert” a item to the beginning of a array that has already been declared and allocated. That is a dynamic collection type of behavior such as a List.
If you don’t have control over the declaration/allocation of the array then the only choice you have is to copy the contents of the array to a new one that includes the empty 0 index.
On the other hand, if you are the one making this initial array then just simply make the size +1 bigger and add a zero at the beginning, problem solved.
Second Question:
Whether or not the arrays should declared in the “Client file” (whatever that is), is really up to you.