In: Computer Science
create code for deletestudentbyID Number for choice ==
4
package courseecom616;
import java.util.Scanner;
import java.util.ArrayList;
public class CourseeCOM616 {
private String courseName;
private String[] studentsName = new
String[1];
private String
studentId;
private int numberOfStudents;
public CourseeCOM616(String courseName) {
this.courseName =
courseName;
}
public String[] getStudentsName() {
return
studentsName;
}
public int getNumberOfStudents() {
return
numberOfStudents;
}
public String getStudentId() {
return studentId;
}
public void setStudentID(int studentID)
{
this.studentId =
studentId;
}
public String getCourseName(){
return courseName;
}
public void addStudent(String student) {
if (numberOfStudents ==
studentsName.length) {
String[] a = new String[studentsName.length + 1];
for (int i = 0; i < numberOfStudents; i++) {
a[i] = studentsName[i];
}
studentsName = a;
}
studentsName[numberOfStudents] = student;
numberOfStudents++;
}
public String[] addStudentByIndex(String
student, int index) {
String[] a = new
String[studentsName.length + 1];
if (numberOfStudents ==
studentsName.length) {
for (int i = 0; i < a.length; i++) {
if (i < index) {
a[i] = studentsName[i];
} else if (i == index) {
a[i] = student;
} else {
a[i] = studentsName[i - 1];
}
}
}
numberOfStudents++;
return a;
}
public static void display(String[] students)
{
System.out.println("========================");
System.out.print("Now
the New students Array is : \n");
for (int i = 0; i <
students.length; i++) {
System.out.println("Index: " + (i) + "--> " + students[i] + "
");
}
}
public void dropStudent(String student) {
int index = findStudent(student);
if (index >= 0) {
dropStudent(index);
}
else {
System.out.println(student + " is not in
the course: " + courseName);
}
}
private void dropStudent(int index)
{
String[] s = new
String[studentsName.length - 1];
for (int i = 0, j = 0; i < s.length;
i++, j++) {
if (i == index) {
j++;
}
s[i] = studentsName[j];
}
this.studentsName = s;
numberOfStudents--;
}
public String[] removeStudentByIndex(String[] a,
int index) {
String[] b = new
String[a.length - 1];
studentsName = a;
for (int i = 0, k = 0; i
< a.length; i++) {
if (i == index) {
continue;
} else {
b[k++] = a[i];
}
}
numberOfStudents--;
return b;
}
public String[] deleteStudentByID(String[] a,
int id) {
public String [] deleteStudentByName (String
students1 [], String name) {
String[] b = new
String[students1.length - 1];
for (int i = 0, k = 0; i
< students1.length; i++) {
if (students1[i].equals(name)) {
continue;
} else {
b[k++] = students1[i];
}
}
numberOfStudents--;
return b;
}
{
this.studentId =
studentId;
this.studentsName =
studentsName;
}
private int findStudent(String student) {
for (int i = 0; i <
numberOfStudents; i++) {
if (studentsName[i].equals(student)) {
return i;
}
}
return -1;
}
}
package courseecom616;
import java.util.Scanner;
import java.util.ArrayList;
public class d {
ArrayList <Students> students = new
ArrayList<> ();
public static void
main(String[] args) {
CourseeCOM616 com616 =
new CourseeCOM616("com616");
Com616.addStudent("Danny");
com616.addStudent("Harvey");
com616.addStudent("Joseph");
com616.addStudent("Ben");
com616.addStudent("Frank");
int sum = 0;
String students1[] =
com616.getStudentsName();
sum +=
com616.getNumberOfStudents();
students1 =
com616.getStudentsName();
Scanner scan = new
Scanner(System.in);
System.out.println("Welcome to College");
int choice;
do {
System.out.println("1) View Student");
System.out.println("2) Insert Student");
System.out.println("3) Remove a student");
System.out.println("4) Delete a student by ID");
System.out.println("5) Delete a student by name");
System.out.println("6) Exit");
choice = scan.nextInt();
if (choice == 1) {
for (int i = 0; i < students1.length; i++) {
System.out.println("Index number is: " + (i) + "---> " +
students1[i]);
students1 = com210.getStudentsName();
}
System.out.println("Number of students attending the Course is: " +
sum);
} else if (choice == 2) {
System.out.println("Enter the name of student and index: ");
scan.nextLine();
String student = scan.nextLine();
int index = scan.nextInt();
students1 = com616.addStudentByIndex(student, 3);
com616.display(students1);
sum = com616.getNumberOfStudents();
System.out.println("\nThe students in the course " +
com616.getCourseName() + ":" + sum);
System.out.println("------------------------------------------");
System.out.println();
} else if (choice == 3) {
System.out.println("Remove a student by index");
int index = scan.nextInt();
students1 = com616.removeStudentByIndex(students1, index);
sum = com616.getNumberOfStudents();
System.out.println("After student drop number is: " +
sum);
System.out.println("------------------------------------");
} else if (choice == 4) {
System.out.println("Delete a students name by their ID");
System.out.println("Enter students ID: ");
String name = scan.nextLine();
String Id = scan.nextLine();
} else if (choice == 5) {
System.out.println("Delete student by name");
System.out.println("Enter student name");
String name = scan.next();
students1 = com616.deleteStudentByName(students1, name);
com616.display(students1);
sum = com616.getNumberOfStudents();
System.out.println("After the student is dropped the number will
be: " + sum);
}
}
while (choice != 6);
System.out.println("This Program has ended");
}
}
Algorithm for deleteStudentByID() function:
1. Start.
2. first get the id of that student, the id is started from 1,2,3...n.
3. Call the function deleteStudentByID() and pass the arraylist and id to the function.
4. As per the logic of deleteStudentByName() we only change the if condition which check the index is equal to
id-1,because id is greater than index with 1.
5. If the condition is true then skip that id and assign next id to current index.
6. return array.
7. End.
Code:
Note: The extra added code is in bold font.
import java.util.Scanner;
import java.util.ArrayList;
class CourseeCOM616 {
private String courseName;
private String[] studentsName = new String[1];
private String studentId;
private int numberOfStudents;
public CourseeCOM616(String courseName) {
this.courseName = courseName;
}
public String[] getStudentsName() {
return studentsName;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getStudentId() {
return studentId;
}
public void setStudentID(int studentID)
{
this.studentId = studentId;
}
public String getCourseName(){
return courseName;
}
public void addStudent(String student) {
if (numberOfStudents == studentsName.length) {
String[] a = new String[studentsName.length + 1];
for (int i = 0; i < numberOfStudents; i++) {
a[i] = studentsName[i];
}
studentsName = a;
}
studentsName[numberOfStudents] = student;
numberOfStudents++;
}
public String[] addStudentByIndex(String student, int index) {
String[] a = new String[studentsName.length + 1];
if (numberOfStudents == studentsName.length) {
for (int i = 0; i < a.length; i++) {
if (i < index) {
a[i] = studentsName[i];
} else if (i == index) {
a[i] = student;
} else {
a[i] = studentsName[i - 1];
}
}
}
numberOfStudents++;
return a;
}
public static void display(String[] students) {
System.out.println("========================");
System.out.print("Now the New students Array is : \n");
for (int i = 0; i < students.length; i++) {
System.out.println("Index: " + (i) + "--> " + students[i] + "
");
}
}
public void dropStudent(String student) {
int index = findStudent(student);
if (index >= 0) {
dropStudent(index);
}
else {
System.out.println(student + " is not in the course: " +
courseName);
}
}
private void dropStudent(int index) {
String[] s = new String[studentsName.length - 1];
for (int i = 0, j = 0; i < s.length; i++, j++) {
if (i == index) {
j++;
}
s[i] = studentsName[j];
}
this.studentsName = s;
numberOfStudents--;
}
public String[] removeStudentByIndex(String[] a, int index) {
String[] b = new String[a.length - 1];
studentsName = a;
for (int i = 0, k = 0; i < a.length; i++) {
if (i == index) {
continue;
} else {
b[k++] = a[i];
}
}
numberOfStudents--;
return b;
}
public String[] deleteStudentByID(String[] a, int id)
{
String[] b = new String[a.length - 1];
studentsName = a;
for (int i = 0, k = 0; i < a.length; i++) {
if (i == id - 1) {
continue;
} else {
b[k++] = a[i];
}
}
numberOfStudents--;
return b;
}
public String [] deleteStudentByName (String students1 [],
String name) {
String[] b = new String[students1.length - 1];
for (int i = 0, k = 0; i < students1.length; i++) {
if (students1[i].equals(name)) {
continue;
} else {
b[k++] = students1[i];
}
}
numberOfStudents--;
return b;
}
{
this.studentId = studentId;
this.studentsName = studentsName;
}
private int findStudent(String student) {
for (int i = 0; i < numberOfStudents; i++) {
if (studentsName[i].equals(student)) {
return i;
}
}
return -1;
}
}
public class d {
ArrayList students = new ArrayList<> ();
public static void main(String[] args) {
CourseeCOM616 com616 = new CourseeCOM616("com616");
com616.addStudent("Danny");
com616.addStudent("Harvey");
com616.addStudent("Joseph");
com616.addStudent("Ben");
com616.addStudent("Frank");
int sum = 0;
String students1[] = com616.getStudentsName();
sum += com616.getNumberOfStudents();
students1 = com616.getStudentsName();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to College");
int choice;
do {
System.out.println("1) View Student");
System.out.println("2) Insert Student");
System.out.println("3) Remove a student");
System.out.println("4) Delete a student by ID");
System.out.println("5) Delete a student by name");
System.out.println("6) Exit");
choice = scan.nextInt();
if (choice == 1) {
for (int i = 0; i < students1.length; i++) {
System.out.println("Index number is: " + (i) + "---> " +
students1[i]);
students1 = com616.getStudentsName();
}
System.out.println("Number of students attending the Course is: " +
sum);
} else if (choice == 2) {
System.out.println("Enter the name of student and index:
");
scan.nextLine();
String student = scan.nextLine();
int index = scan.nextInt();
students1 = com616.addStudentByIndex(student, 3);
com616.display(students1);
sum = com616.getNumberOfStudents();
System.out.println("\nThe students in the course " +
com616.getCourseName() + ":" + sum);
System.out.println("------------------------------------------");
System.out.println();
} else if (choice == 3) {
System.out.println("Remove a student by index");
int index = scan.nextInt();
students1 = com616.removeStudentByIndex(students1, index);
sum = com616.getNumberOfStudents();
System.out.println("After student drop number is: " + sum);
System.out.println("------------------------------------");
} else if (choice == 4) {
System.out.println("Delete a students name by their
ID");
System.out.println("Enter students ID: ");
int id = scan.nextInt();
students1 = com616.deleteStudentByID(students1, id);
com616.display(students1);
sum = com616.getNumberOfStudents();
System.out.println("After student is dropped the number will be: "
+ sum);
System.out.println("------------------------------------");
} else if (choice == 5) {
System.out.println("Delete student by name");
System.out.println("Enter student name");
String name = scan.next();
students1 = com616.deleteStudentByName(students1, name);
com616.display(students1);
sum = com616.getNumberOfStudents();
System.out.println("After the student is dropped the number will
be: " + sum);
}
}
while (choice != 6);
System.out.println("This Program has ended");
}
}
Output: