In: Computer Science
Code an application program that keeps track of student
information at your college: names, identification numbers, and
grade
point averages in a fully e ncapsulated (homogeneous) Sorted
array-
based data structure. When launched, the user will be asked to
input the
maximum size of the data set, the initial number of students,
and the
initial data set. Once this is complete, the user will be presented
with
the following menu:
Enter: 1 to insert a new student's information,
2 to fetch and output a student's information,
3 to delete a student's information,
4 to update a student's information,
5 to output all the student information in sorted order, and
6 to exit the program.
The program should perform an unlimited number of operations
until
the user enters a 6 to exit the program.
1). ANSWER :
GIVENTHAT :
/****************************Student.java************************/
public class Student {
private String name;
private String id;
private double gpa;
public Student(String name, String id, double gpa)
{
super();
this.name = name;
this.id = id;
this.gpa = gpa;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getgpa() {
return gpa;
}
public void setgpa(double gpa) {
this.gpa = gpa;
}
@Override
public String toString() {
return "Name: " + name + "\tID: " +
id + "\tGPA: " + gpa;
}
}
/*********************************StudentInformation.java*************************/
import java.util.Arrays;
import java.util.Collections;
public class StudentInformation {
private Student[] students;
private int numberOfStudents;
public StudentInformation() {
this.numberOfStudents = 0;
students = new Student[100];
}
public StudentInformation(int numberOfStudent)
{
this.students = new
Student[numberOfStudent];
this.numberOfStudents = 0;
}
public Student[] getStudents() {
return students;
}
public void setStudents(Student[] students) {
this.students = students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void setNumberOfStudents(int
numberOfStudents) {
this.numberOfStudents =
numberOfStudents;
}
public void insertNewStudent(String name, String id, double gpa) {
students[numberOfStudents] = new
Student(name, id, gpa);
numberOfStudents++;
}
public Student fetchStudent(String id) {
Student st = null;
for (Student student : students) {
if (student != null && student.getId().equalsIgnoreCase(id)) {
st = student;
break;
}
}
return st;
}
public void deleteStudent(String id) {
int currentLength =
students.length;
for (int i = 0; i <
currentLength; i++) {
if (students[i]
!= null && students[i].getId().equalsIgnoreCase(id))
{
students[i] = students[currentLength - 1];
// employees[currentLength - 1] = null; ...
could help reclaim storage
currentLength--;
break;
}
if (i ==
currentLength - 1) {
System.out.println("No Student found with this
id!!");
}
}
}
public void updateStudent(String id, String nameUpdate, double gpa) {
Student st = fetchStudent(id);
if (st != null) {
st.setName(nameUpdate);
st.setgpa(gpa);
} else {
System.out.println("No Student found with this id!!");
}
}
private void sortStudents() {
Collections.sort(Arrays.asList(students), new
StudentComparator());
}
public void printAllStudent() {
try {
sortStudents();
} catch (Exception e) {
}
for (Student student : students) {
if (student != null) {
System.out.println(student.toString());
}
}
}
}
/*********************************StudentComparator.java***************************/
import java.util.Comparator;
public class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return
o1.getName().compareTo(o2.getName());
}
}
/******************************************StudentInfoTest.java***************************/
import java.util.Scanner;
public class StudentInfoTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter the
number of Student: ");
int numberOfStudents =
scan.nextInt();
StudentInformation stf = new
StudentInformation(numberOfStudents);
scan.nextLine();
int option = 0;
do {
System.out.println(
"Menu:\n\t1. Insert New
Student\n\t2. Fetch Student\n\t3. Delete Student\n\t4. Update
Student\n\t5. Print All Student\n\t6. Exit");
System.out.print("Enter your choice: ");
option =
scan.nextInt();
scan.nextLine();
switch (option)
{
case 1:
System.out.print("Enter Student id: ");
String id = scan.nextLine();
System.out.print("Enter Student name: ");
String name = scan.nextLine();
System.out.print("Enter Student gpa: ");
double gpa = scan.nextDouble();
scan.nextLine();
stf.insertNewStudent(name, id, gpa);
break;
case 2:
System.out.print("Enter student id: ");
id = scan.nextLine();
Student st = stf.fetchStudent(id);
if (st == null) {
System.out.println("No
Student Found!!");
} else {
System.out.println(st.toString());
}
break;
case 3:
System.out.print("Enter Student id: ");
id = scan.nextLine();
stf.deleteStudent(id);
break;
case 4:
System.out.print("Enter Student id: ");
id = scan.nextLine();
System.out.print("Enter Student Name: ");
name = scan.nextLine();
System.out.print("Enter Student gpa: ");
gpa = scan.nextDouble();
scan.nextLine();
stf.updateStudent(id, name, gpa);
break;
case 5:
stf.printAllStudent();
break;
case 6:
System.out.println("Thank you!!");
break;
default:
System.out.println("Invalid Choice!!");
break;
}
} while (option != 6);
}
}
The above program output :
Enter the number of Student: 100
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 1
Enter Student id: A100
Enter Student name: JKS
Enter Student gpa: 9.43
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 1
Enter Student id: B101
Enter Student name: Virat Kohli
Enter Student gpa: 8.45
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 1
Enter Student id: MS Dhoni
Enter Student name: DD
Enter Student gpa: 6.6
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 5
Name: DD ID: MS Dhoni GPA: 6.6
Name: JKS ID: A100 GPA: 9.43
Name: Virat Kohli ID: B101 GPA: 8.45
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 3
Enter Student id: MS Dhoni
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 5
Name: JKS ID: A100 GPA: 9.43
Name: Virat Kohli ID: B101 GPA: 8.45
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 2
Enter student id: B101
Name: Virat Kohli ID: B101 GPA: 8.45
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 4
Enter Student id: B101
Enter Student Name: Virat
Enter Student gpa: 9.99
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 5
Name: JKS ID: A100 GPA: 9.43
Name: Virat ID: B101 GPA: 9.99
Menu:
1. Insert New Student
2. Fetch Student
3. Delete Student
4. Update Student
5. Print All Student
6. Exit
Enter your choice: 6
Thank you!!