Hi..I've been working on this program and now exhausted. I'm asked to input students informationin data structure. first enter number of students then their information(name, id# and gpa). I've created sorted array class(with insert, fetch, delete and update method...),Listing class which creats object of each student and holds their information, & Main sorted array class which calls other two classes.
I'm getting few errors in fetch method. doent sort properly. I'm using binary search method. I also dont understand how to input student info from main sorted array class and insert it. I was getting array out of bound error.
I'm posting my both class here. plz help me...thanks
public class SortedArray{
private int size;
private int next;
private Listing[] data;
private int mid = 0;
public SortedArray(){
next = 0;
size = 100;
data = new Listing[size];
}
public SortedArray(int n){
next = 0;
size = n;
data = new Listing[size];
}
//insert operation
public int insert(Listing newNode) {
int low = 0;
String nodeName = newNode.getName();
if(next == 0) {
data[next] = newNode.deepCopy();
next = next + 1;
return next;
}
if(next == 1) {
data[next] = newNode.deepCopy();
next = next + 1;
return next;
}
else{
low = 0;
int high = next - 1;
// System.out.println("line 1");
while (high >= low) {
mid = (low + high) / 2;
// System.out.println("mid is " + mid);
if(data[mid].compareTo(nodeName) > 0)
{high = mid - 1;
//System.out.println("high is" + high);
}
else if (data[mid].compareTo(nodeName) == 0)
break;
//return mid;
else{
low = mid + 1;
//System.out.println("low is " + low);
}
}
for(int j = next; j >= mid; j--){
data[j] = data[j-1];
// System.out.println("line 5");
}
next = next + 1;
//System.out.println("next is " + next);
data[mid] = newNode.deepCopy();
//System.out.println("mid is" + mid);
return mid;
}
// return -low-1;
}
//fatch operation
public Listing fatch(String targetKey){
System.out.println("targetkey is " + targetKey);
int low = 0;
int high = next - 1;
System.out.println("next is " + next);
while (high >= low) {
mid = (low + high) / 2;
System.out.println("fatch mid is " + mid);
if (data[mid].compareTo(targetKey) > 0) {
System.out.println("comparison output is " + data[mid].compareTo(targetKey));
System.out.println("data[mid] is "+data[mid] + "\n");
high = mid - 1;
System.out.println("fatch high is " + high);}
else if (data[mid].compareTo(targetKey) == 0){
break;}
else{
low = mid + 1;
System.out.println("fatch low is " + low);}
}
return data[mid].deepCopy();
}
import java.util.Scanner;
public class MainSortedArray{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter number of students: ");
int numStu = input.nextInt();
SortedArray studentInfo = new SortedArray();
Listing temp;
for(int i = 0; i < numStu; i++){
System.out.println("Enter name of the student: ");
String stuName = input.next();
System.out.println("Enter student ID number: ");
String idNumber = input.next();
System.out.println("Enter students GPA: ");
String stuGpa = input.next();
Listing newStudent = new Listing(stuName,idNumber,stuGpa);
System.out.println(studentInfo.insert(newStudent)) ;
}
}
}