All pastes #233316 Raw Edit

some stuff

public text v1 · immutable
·published 2006-11-01 21:47 UTC
rendered paste body
//import java.util.*;

public class StudentList implements StudentListInterface {
	public Student[] students;
	public int num_comparisons;
	public int elements = 0;
	public int arrayLength = 1;
	public StudentList(){
		students = new Student[arrayLength];
	}
	private boolean isFull() {
		return (students.length == elements);
	}
	private void ExpandList() {
		Student[] temp = new Student[elements*2];
	    for (int i =0; i < students.length; i++) {
	         temp[i] = students[i];
	    }
	    students = temp;
	    arrayLength = arrayLength*2;
	}
	public void add(Student s) {
		if (isFull()){
			ExpandList();
			students[elements] = s;
			elements++;
		}
		else {
			students[elements] = s;
			elements++;
		}
	}
	

	public void add(Student s, int n) {
		if(isFull()){
			ExpandList();
			for (int i = elements; i>=n; i--){
				students[i] = students[i-1];	//makes room for Student
												//to be added in array before specified position n.
			}									//Moves all elements up to and including n-1 to right in array.
		}
		else {
			for (int i = elements; i>=n; i--) {
				students[i] = students[i-1];
				
			}
		}
		students[n-1] = s;		//adds before nth item
		elements++;
	}

	public void clear() {
		elements = 0;
		arrayLength = 0;
		//remove?
	}

	public void display() {
		for (int i=0; i < elements; i++){
			students[i].display();
		}
	}

	public Student get(int n) throws ArrayIndexOutOfBoundsException {
		if (n < elements && n >= 0 && students[n] != null){
			return students[n];
		}
		else {
			throw new ArrayIndexOutOfBoundsException("Object at position " + n  + " does not exist.");
			}
	}
	public void closeGap(int n) {
		for(int i = n + 1; i < size(); i++){
			students[i-1] = students[i];
		}
	}
	public Student remove(int n) throws ArrayIndexOutOfBoundsException {
			if (n < elements && n >= 0 && students[n] != null) {
				Student temp = students[n];
				closeGap(n);
				elements--;
				return temp;
					
				}
		else {
			throw new ArrayIndexOutOfBoundsException("Object at position " + n + " does not exist.");
		}
	}

	public int size() {
		return elements;
	}
	public void bubbleSort(boolean showGui) {
		Student[] tempstudents = new Student[arrayLength];
//		num_comparisons = 0;
		for(int k=0; k < tempstudents.length; k++){
	          for(int j = 0; j < tempstudents.length; j++)
	                  if (tempstudents[k].compareTo(tempstudents[j]) != 0 && tempstudents[k].compareTo(tempstudents[j]) > 0){
	                      num_comparisons++;
	                	  swap(tempstudents, k, j);
	                  }
	   }
	}
	
	public static void swap(Student[] tstudents, int i, int j) {
		Student temp = tstudents[i];
		tstudents[i] = tstudents[j];
	    tstudents[j] = temp;
	}
	public void insertionSort(boolean showGui){
		
	}

	
	public void mergeSort(boolean showGui){
		
	}
	

}



//Output

Running C level...
Running constructor...
Testing add()...
Testing display()...
ID: 1, Month: DEC, Date: 3
ID: 2, Month: AUG, Date: 2
ID: 4, Month: MAY, Date: 21
Testing add(s, n)...
ID: 1, Month: DEC, Date: 3
ID: 3, Month: APR, Date: 9
ID: 2, Month: AUG, Date: 2
ID: 4, Month: MAY, Date: 21
Testing get()...
The 1st student in the list is ID: 1, Month: DEC, Date: 3
Testing get() error handling...
Testing remove()...
ID: 3, Month: APR, Date: 9
ID: 2, Month: AUG, Date: 2
ID: 4, Month: MAY, Date: 21java.lang.ArrayIndexOutOfBoundsException: Object at position 1000 does not exist.
	at StudentList.get(StudentList.java:70)
	at A3Test.ctest(A3Test.java:55)
	at A3Test.main(A3Test.java:20)

Testing remove() error handling...
Testing length()...
Length is: 3
read testData 1
java.lang.ArrayIndexOutOfBoundsException: Object at position -10 does not exist.
	at StudentList.remove(StudentList.java:87)
	at A3Test.ctest(A3Test.java:65)
	at A3Test.main(A3Test.java:20)
ID: 1, Month: JAN, Date: 2
ID: 2, Month: JAN, Date: 5
ID: 3, Month: JAN, Date: 12
ID: 4, Month: JAN, Date: 13
ID: 5, Month: JAN, Date: 15
ID: 6, Month: JAN, Date: 16
ID: 7, Month: JAN, Date: 19
ID: 8, Month: JAN, Date: 20
ID: 9, Month: JAN, Date: 23
ID: 10, Month: JAN, Date: 26
ID: 11, Month: FEB, Date: 2
ID: 12, Month: FEB, Date: 4
ID: 13, Month: FEB, Date: 5
ID: 14, Month: FEB, Date: 6
ID: 15, Month: FEB, Date: 8
ID: 16, Month: FEB, Date: 11
ID: 17, Month: FEB, Date: 15
ID: 18, Month: FEB, Date: 19
ID: 20, Month: FEB, Date: 21
ID: 21, Month: FEB, Date: 28
ID: 22, Month: MAR, Date: 2
ID: 23, Month: MAR, Date: 3
ID: 24, Month: MAR, Date: 5
ID: 25, Month: MAR, Date: 7
ID: 26, Month: MAR, Date: 10
ID: 27, Month: MAR, Date: 11
ID: 28, Month: MAR, Date: 13
ID: 29, Month: MAR, Date: 21
ID: 30, Month: MAR, Date: 26
ID: 31, Month: APR, Date: 2
ID: 32, Month: APR, Date: 3
ID: 33, Month: APR, Date: 5
ID: 34, Month: APR, Date: 9
ID: 35, Month: APR, Date: 12
ID: 36, Month: APR, Date: 18
ID: 37, Month: APR, Date: 23
ID: 38, Month: APR, Date: 26
ID: 39, Month: APR, Date: 28
ID: 40, Month: MAY, Date: 1
ID: 41, Month: MAY, Date: 3
ID: 42, Month: MAY, Date: 6
ID: 43, Month: MAY, Date: 14
ID: 44, Month: MAY, Date: 18
ID: 45, Month: MAY, Date: 19
ID: 46, Month: MAY, Date: 20
ID: 47, Month: MAY, Date: 24
ID: 48, Month: MAY, Date: 25
ID: 49, Month: MAY, Date: 26
ID: 50, Month: MAY, Date: 28
Number of comparisons for bubble sort on testData1.txt: 0
read testData 2
ID: 50, Month: MAY, Date: 28
ID: 49, Month: MAY, Date: 26
ID: 48, Month: MAY, Date: 25
ID: 47, Month: MAY, Date: 24
ID: 46, Month: MAY, Date: 20
ID: 45, Month: MAY, Date: 19
ID: 44, Month: MAY, Date: 18
ID: 43, Month: MAY, Date: 14
ID: 42, Month: MAY, Date: 6
ID: 41, Month: MAY, Date: 3
ID: 40, Month: MAY, Date: 1
ID: 39, Month: APR, Date: 28
ID: 38, Month: APR, Date: 26
ID: 37, Month: APR, Date: 23
ID: 36, Month: APR, Date: 18
ID: 35, Month: APR, Date: 12
ID: 34, Month: APR, Date: 9
ID: 33, Month: APR, Date: 5
ID: 32, Month: APR, Date: 3
ID: 31, Month: APR, Date: 2
ID: 30, Month: MAR, Date: 26
ID: 29, Month: MAR, Date: 21
ID: 28, Month: MAR, Date: 13
ID: 27, Month: MAR, Date: 11
ID: 26, Month: MAR, Date: 10
ID: 25, Month: MAR, Date: 7
ID: 24, Month: MAR, Date: 5
ID: 23, Month: MAR, Date: 3
ID: 22, Month: MAR, Date: 2
ID: 21, Month: FEB, Date: 28
ID: 20, Month: FEB, Date: 21
ID: 18, Month: FEB, Date: 19
ID: 17, Month: FEB, Date: 15
ID: 16, Month: FEB, Date: 11
ID: 15, Month: FEB, Date: 8
ID: 14, Month: FEB, Date: 6
ID: 13, Month: FEB, Date: 5
ID: 12, Month: FEB, Date: 4
ID: 11, Month: FEB, Date: 2
ID: 10, Month: JAN, Date: 26
ID: 9, Month: JAN, Date: 23
ID: 8, Month: JAN, Date: 20
ID: 7, Month: JAN, Date: 19
ID: 6, Month: JAN, Date: 16
ID: 5, Month: JAN, Date: 15
ID: 4, Month: JAN, Date: 13
ID: 3, Month: JAN, Date: 12
ID: 2, Month: JAN, Date: 5
ID: 1, Month: JAN, Date: 2
Number of comparisons for bubble sort on testData2.txt: 0
read testData 3
ID: 41, Month: MAY, Date: 3
ID: 42, Month: MAY, Date: 6
ID: 43, Month: MAY, Date: 14
ID: 44, Month: MAY, Date: 18
ID: 45, Month: MAY, Date: 19
ID: 46, Month: MAY, Date: 20
ID: 47, Month: MAY, Date: 24
ID: 48, Month: MAY, Date: 25
ID: 49, Month: MAY, Date: 26
ID: 50, Month: MAY, Date: 28
ID: 34, Month: APR, Date: 9
ID: 35, Month: APR, Date: 12
ID: 36, Month: APR, Date: 18
ID: 37, Month: APR, Date: 23
ID: 38, Month: APR, Date: 26
ID: 39, Month: APR, Date: 28
ID: 40, Month: MAY, Date: 1
ID: 26, Month: MAR, Date: 10
ID: 27, Month: MAR, Date: 11
ID: 28, Month: MAR, Date: 13
ID: 29, Month: MAR, Date: 21
ID: 30, Month: MAR, Date: 26
ID: 31, Month: APR, Date: 2
ID: 32, Month: APR, Date: 3
ID: 33, Month: APR, Date: 5
ID: 18, Month: FEB, Date: 19
ID: 20, Month: FEB, Date: 21
ID: 21, Month: FEB, Date: 28
ID: 22, Month: MAR, Date: 2
ID: 23, Month: MAR, Date: 3
ID: 24, Month: MAR, Date: 5
ID: 25, Month: MAR, Date: 7
ID: 10, Month: JAN, Date: 26
ID: 11, Month: FEB, Date: 2
ID: 12, Month: FEB, Date: 4
ID: 13, Month: FEB, Date: 5
ID: 14, Month: FEB, Date: 6
ID: 15, Month: FEB, Date: 8
ID: 16, Month: FEB, Date: 11
ID: 17, Month: FEB, Date: 15
ID: 1, Month: JAN, Date: 2
ID: 2, Month: JAN, Date: 5
ID: 3, Month: JAN, Date: 12
ID: 4, Month: JAN, Date: 13
ID: 5, Month: JAN, Date: 15
ID: 6, Month: JAN, Date: 16
ID: 7, Month: JAN, Date: 19
ID: 8, Month: JAN, Date: 20
ID: 9, Month: JAN, Date: 23
Number of comparisons for bubble sort on testData3.txt: 0