I am having issues with determining what the given algorithm is asking for.I'm not sure how to delete a location within an array or assign a null to the last spot in an array. Here is the algorithm:
found = searchToDelete( course )
IF (found == -1) THEN
Display message that course is not in student’s schedule
ELSE
IF (found == ( position of last course in array) THEN
course number = course number -1
ELSE
FOR (index = found, index < course number, index++)
schedule [index] = schedule [index + 1]
END FOR
course number = course number - 1
END IF
END IF
Here is my code:
public void dropCourse(String courseName){
found = searchToDelete(course);
int index = 0;
if (found == -1){
System.out.println("Course not in schedule");
if (found = index(6)){
courseNumber = courseNumber -1;
for ( index = found, index < courseNumber, index ++);
{
schedule[index] = schedule[index + 1];
}courseNumber = courseNumber -1;
}
}
}
If you don't have to implement the algorithm exactly like the pseudo-code, I would suggest you use a List class like the ArrayList. The ArrayList class has a lot of useful functions that would make your code easier.
If you have to use an array I suggest you take a look at the arraycopy method to replace your original array, as you cannot resize an array in Java.
You can, of course, set the entry schedule[courseNumber]
to null (after decrementing the courseNumber), but then you will possibly wind up with arrays that mostly consist of null, and, since you cannot resize the array, you would probably run into problems when adding something to it in case it is full.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments