Question

In: Computer Science

create code for deletestudentbyID Number for choice == 4 package courseecom616; import java.util.Scanner; import java.util.ArrayList; public...

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");
        }
}

Solutions

Expert Solution

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:


Related Solutions

In this create a code that will drop a student by ID number //////////////////////////////////////////////////////////////////////// import java.util.Scanner;...
In this create a code that will drop a student by ID number //////////////////////////////////////////////////////////////////////// import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String [] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public void...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666...
----fix code to search and delete a student by Identification number import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public int DeleteStudentsByID() {         return...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public...
/** Create a method as instructed below and then call it appropriately. */ import java.util.Scanner; public class BankCharges { public static void main(String[] args) { //Variable declarations int numChecks; double perCheckFee; double totalFee; double totalFeesAllAccounts = 0; //accumulator int numAccounts;    //Constant declarations for base fee and per check fees final double BASE_FEE = 10.0; final double LESS_THAN_20_FEE = 0.10; final double TWENTY_TO_THIRTYNINE_FEE = 0.08; final double FORTY_TO_FIFTYNINE_FEE = 0.06; final double SIXTY_OR_MORE_FEE = 0.04;    // Create a Scanner...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write a description of class SpellChecker here. * * @author (your name) * @version (a version number or a date) */ public class SpellChecker { private ArrayList words; private DictReader reader; /** * Constructor for objects of class SpellChecker */ public SpellChecker() { reader = new DictReader("words.txt"); words = reader.getDictionary(); } /** * This method returns the number of words in the dictionary. * Change...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT