Question

In: Computer Science

----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 deleteStudentByID();
    }

    public void addStudent(String student) {

        if (numberOfStudents == students.length) {
            String[] a = new String[students.length + 1];
            for (int i = 0; i < numberOfStudents; i++) {
                a[i] = students[i];
            }
            students = a;
        }
        students[numberOfStudents] = student;
        numberOfStudents++;
    }

    public String[] addStudentByIndex(String student, int index) {
        String[] a = new String[students.length + 1];
        if (numberOfStudents == students.length) {
            for (int i = 0; i < a.length; i++) {
                if (i < index) {
                    a[i] = students[i];
                } else if (i == index) {
                    a[i] = student;
                } else {
                    a[i] = students[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 String[] removeStudentByIndex(String[] a, int index) {

        String[] b = new String[a.length - 1];
        students = a;

        for (int i = 0, k = 0; i < a.length; i++) {
            if (i == index) {
                continue;
            } else {
                b[k++] = a[i];
            }
        }
        numberOfStudents--;
        return b;

    }

    public int[] deleteStudentByID(int num[]) {

    }


    private int findStudent(String student) {
        for (int i = 0; i < numberOfStudents; i++) {
            if (students[i].equals(student)) {
                return i;
            }
        }
        return -1;
    }
}

import java.util.Scanner;

public class d {

    public static void main(String[] args)
    {
        COurseCom666 com666 = new COurseCom666("com666");
        com666.addStudent("Bishal");
        com666.addStudent("Ana");
        com666.addStudent("Ying");
        com666.addStudent("Samuel");
        com666.addStudent("Surai");
        int sum = 0;

        String students1[] = com666.getStudents();
        sum += com666.getNumberOfStudents();
        Scanner scan = new Scanner(System.in);

        int choice;

        do {
            System.out.println("Welcome to St.Joseph's College");
            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. Exit");
            choice = scan.nextInt();
            if (choice == 1)
            {
                for (int i = 0; i < students1.length; i++)
                {
                    System.out.println("Index is: " + (i) + "---> " + students1[i]);
                }
                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 = com210.addStudentByIndex(student, 3);
                com666.display(students1);
                sum = com666.getNumberOfStudents();
                System.out.println("After student was added number is: " + sum);
            }
            else if (choice == 3) {
                System.out.println("Remove a student by index");
                int index = scan.nextInt();
                students1 = com666.removeStudentByIndex(students1, index);
                com666.display(students1);
                sum = com666.getNumberOfStudents();
                System.out.println("After student drop number is: " + sum);
                System.out.println("Number of students attending the Course is: " + sum);
                System.out.println("----------------------------------");
            }
            else if (choice ==4) {
                System.out.println("Delete a students ID");

                System.out.println("Bishal", StudentID);
                String student1_name = "Bishal";
                int StudentID = 111111;

                System.out.println("Ana", StudentID);
                String student2_name = "Ana";
                int StudentID = 222222;


                String student3_name = "Ying";
                int StudentID = 333333;

                String student4_name = "Samuel";
                int student4_id = 444444;

                String student5_name = "Surai";
                int student5_id = 555555;



            }


        }
        while (choice!= 5);
    }
}

Solutions

Expert Solution

This is the code to fix search and delete student by identification number and I also mention the comment in the code to easily understand the changes, just copy the code and paste it on any java ide or complier.

import java.util.*;

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 addStudent(String student) {

if (numberOfStudents == students.length) {
String[] a = new String[students.length + 1];
for (int i = 0; i < numberOfStudents; i++) {
a[i] = students[i];
}
students = a;
}
students[numberOfStudents] = student;
numberOfStudents++;
}

public String[] addStudentByIndex(String student, int index) {
String[] a = new String[students.length + 1];
for (int i = 0; i < a.length; i++) {
if (i < index) {
a[i] = students[i];
} else if (i == index) {
a[i] = student;
} else {
a[i] = students[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 String[] removeStudentByIndex(String[] a, int index) {

String[] b = new String[a.length - 1];
students = a;

for (int i = 0, k = 0; i < a.length; i++) {
if (i == index) {
continue;
} else {
b[k++] = a[i];
}
}
numberOfStudents--;
return b;

}
// fix the code
public String[] deleteStudentByID(String a[],int id) {
String sname[]={"Bishal", "Ana","Ying","Samuel","Surai"};
int StudentID[]={111111,222222,333333,444444,555555};
int index=0;
String name="";
// find name of student we want to delete by id
for(int i=0;i<StudentID.length;i++){
if(StudentID[i]==id){
index=StudentID[i];
name=sname[i];
break;
}
}
String[] b = new String[a.length - 1];
students = a;

for (int i = 0, k = 0; i < a.length; i++) {
if (a[i].equals(name)) {
continue;
} else {
b[k++] = a[i];
}
}
numberOfStudents--;
return b;
}

private int findStudent(String student) {
for (int i = 0; i < numberOfStudents; i++) {
if (students[i].equals(student)) {
return i;
}
}
return -1;
}
}

public class d{

public static void main(String[] args)
{
COurseCom666 com666 = new COurseCom666("com666");
com666.addStudent("Bishal");
com666.addStudent("Ana");
com666.addStudent("Ying");
com666.addStudent("Samuel");
com666.addStudent("Surai");
int sum = 0;

String students1[] = com666.getStudents();
sum += com666.getNumberOfStudents();
Scanner scan = new Scanner(System.in);

int choice;

do {
System.out.println("Welcome to St.Joseph's College");
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. Exit");
choice = scan.nextInt();
if (choice == 1)
{
for (int i = 0; i < students1.length; i++)
{
System.out.println("Index is: " + (i) + "---> " + students1[i]);
}
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 = com666.addStudentByIndex(student, 3);
com666.display(students1);
sum = com666.getNumberOfStudents();
System.out.println("After student was added number is: " + sum);
}
else if (choice == 3) {
System.out.println("Remove a student by index");
int index = scan.nextInt();
students1 = com666.removeStudentByIndex(students1, index);
com666.display(students1);
sum = com666.getNumberOfStudents();
System.out.println("After student drop number is: " + sum);
System.out.println("Number of students attending the Course is: " + sum);
System.out.println("----------------------------------");
}
else if (choice ==4) {
System.out.println("Delete a students ID");
String name[]={"Bishal", "Ana","Ying","Samuel","Surai"};
int StudentID[]={111111,222222,333333,444444,555555};
  
for(int i=0;i<name.length;i++)
System.out.println(name[i]+" "+StudentID[i]);
  
// take id from user that we want to delete
int id=scan.nextInt();

students1 = com666.deleteStudentByID(students1,id);
com666.display(students1);
sum = com666.getNumberOfStudents();
System.out.println("After student drop number is: " + sum);
}
}
while (choice!= 5);
}
}


Related Solutions

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 =...
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...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main...
TASK: Based upon the following code: import java.util.Scanner; // Import the Scanner class public class Main {   public static void main( String[] args ) {     Scanner myInput = new Scanner(System.in); // Create a Scanner object     System.out.println("Enter (3) digits: ");     int W = myInput.nextInt();     int X = myInput.nextInt();     int Y = myInput.nextInt();      } } Use the tools described thus far to create additional code that will sort the integers in either monotonic ascending or descending order. Copy your code and...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class...
What is wrong with this code and how can it be fixed? import java.util.Scanner; public class admissionRequirement { public static void main(String[] args) { // TODO Auto-generated method stub Scanner myObj = new Scanner(System.in); System.out.println("What is your name?"); String name = myObj.nextLine(); System.out.println("What is your Reading Score?"); int reading = myObj.nextInt(); System.out.println("What is your Math Score?"); int math = myObj.nextInt(); System.out.println("What is your Writing Score?"); int writing = myObj.nextInt(); System.out.println("What is your Class Standing?"); int standing = myObj.nextInt(); System.out.println("What is...
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import...
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String [] students = new String[1];     private int numberOfStudents;     public COurseCom66(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public...
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...
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;    ...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT