hsuay
11/3/2018 - 6:48 PM

StudentArray.java


import java.io.*;

class Student {

	private String LName, FName;
	private int age;

	public Student(String arg1, String arg2, int arg3) {

		LName = arg1;
		FName = arg2;
		age = arg3;

	}

	public void display() {

		System.out.println("\nFirst name: " + FName);
		System.out.println("Last name: " + LName);
		System.out.println("Age: " + age);

	}

	public String getName() {

		return (FName + " " + LName);

	}

	public int getAge() {

		return age;

	}
	
}

public class StudentArray {

	Student[] students;
	int studentCount;

	public StudentArray(int max) {

		students = new Student[max];
		studentCount = 0;

	}

	public void insert(String arg1, String arg2, int arg3) {

		if(studentCount > students.length - 1) {

			System.out.println("\nArray is full!");
			System.exit(0);

		}

		if(studentCount == 0) {

			students[studentCount++] = new Student(arg1, arg2, arg3);

		}

		for(int counter = 1; counter < studentCount; counter++) 
			if(arg1.compareTo(students[counter].getName().substring(0, students[counter].getName().indexOf(' '))) < 0) {

				// insert at counter
				// move everything to the right
				for(int i = studentCount + 1; i > counter; i--) {

					students[i] = students[i - 1];

				}

				students[studentCount++] = new Student(arg1, arg2, arg3);


			}

	}

	public void binarySearch(String lastName) throws IOException {

		if(studentCount == 0) {

			System.out.println("\nArray is empty!");
			System.exit(0);

		}

		int start = 0, end = studentCount, midIndex = (int) (start + end) / 2;
		String lName;

		while(start != end) {

			lName = students[midIndex].getName().substring(0, students[midIndex].getName().indexOf(' '));

			if(lastName.equals(lName)) {

				System.out.println("\nFound at index " + midIndex);
				System.out.println("Name: " + students[midIndex].getName());
				System.out.println("Age: " + students[midIndex].getAge());
				System.out.println("\nPress enter to continue...");
				new BufferedReader(new InputStreamReader(System.in)).readLine();
				System.exit(0);
				
			} 
			
			else if(lastName.compareTo(lName) > 0)
				start = midIndex + 1;
			
			else if(lastName.compareTo(lName) < 0)
				end = midIndex - 1;

		}

		System.out.println("\nNot found!");

	}

	public static void main(String[] args) throws IOException {
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("\n\nEnter the number of students: ");
		int numberOfStudents = Integer.parseInt(reader.readLine()), age;
		String last, first;
		StudentArray studentArray = new StudentArray(numberOfStudents);


		System.out.println("\nINSERTION");

		for(int i = 0; i < numberOfStudents; i++) {

			System.out.print("\n#" + (i + 1) + "\nEnter first name: ");
			first = reader.readLine();
			System.out.print("\nEnter last name: ");
			last = reader.readLine();
			System.out.print("\nEnter age: ");
			age = Integer.parseInt(reader.readLine());

			studentArray.insert(last, first, age);

		}

		System.out.println("\nBINARY SEARCH");
		System.out.print("\nEnter last name to search for a matching student: ");
		studentArray.binarySearch(reader.readLine());

	}

}