Question

please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...

please fix code to delete by studentname

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 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 String[] deleteStudentByID(String[] a, int id) {

        String fname [] = {"Bishal", "Ana", "Ying", "Samuel", "Surai"};
        int StudentID[] = {1111, 2222, 3333, 4444, 5555};
        int index = 0;
        String name ="";

        for (int i = 0; i<StudentID.length; i++ ) {
            if (StudentID[i] == id){
                index = StudentID[i];
                name = fname[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;
    }

    public void deleteStudent () {

        Scanner scan = new Scanner(System.in);
        String searchName;
        boolean found = false;
        int loopCounter = 0;

        System.out.println("Please Enter the student you want to delete: ");
        searchName = scan.nextLine();

        while ((!found)&&(loopCounter < getNumberOfStudents()))
        {
            found = students[loopCounter].
            }
        }

    }

    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 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 = com210.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 by their ID");
                String fname [] = {"Bishal", "Ana", "Ying", "Samuel", "Surai"};
                int StudentID [] = {1111, 2222, 3333, 4444, 5555};
                for (int i = 0; i < fname.length; i++) ;
                System.out.println(fname[i]+ "" + StudentID[i]) ;
                int id = scan.nextInt();
                students1 = com666.deleteStudentByID(students1, id);
                com666.display(students1);
                sum = com666.getNumberOfStudents();
                System.out.println("After the student is dropped the number will be: " + sum);
            }
            else if (choice == 5) {
                System.out.println("Delete student by their name");
                  for (int i = 0; i < com666.getNumberOfStudents(); i++ )
                  {
                     
                      }
                  }
            }
        }
        while (choice!= 6);
    }
}

Homework Answers

Answer #1

package --Mention the package name-----

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 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 String[] deleteStudentByID(String[] a, int id) {

String fname [] = {"Bishal", "Ana", "Ying", "Samuel", "Surai"};
int StudentID[] = {1111, 2222, 3333, 4444, 5555};
int index = 0;
String name ="";

for (int i = 0; i<StudentID.length; i++ ) {
if (StudentID[i] == id){
index = StudentID[i];
name = fname[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;
}

public void deleteStudent () {

Scanner scan = new Scanner(System.in);
String searchName;
boolean found = false;
int loopCounter = 0;

System.out.println("Please Enter the student you want to delete: ");
searchName = scan.nextLine();

while ((!found)&&(loopCounter < getNumberOfStudents()))
{
students[loopCounter].equals(searchName);
found = true;
}
}

  

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

package --Mention the package name-----

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 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. 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 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 by their ID");
String fname [] = {"Bishal", "Ana", "Ying", "Samuel", "Surai"};
int StudentID [] = {1111, 2222, 3333, 4444, 5555};
for (int i = 0; i < fname.length; i++) {
System.out.println(fname[i] +" "+ StudentID[i]) ;
}
int id = scan.nextInt();
students1 = com666.deleteStudentByID(students1, id);
com666.display(students1);
sum = com666.getNumberOfStudents();
System.out.println("After the student is dropped the number will be: " + sum);
}
else if (choice == 5) {
System.out.println("Delete student by their name");
   com666.deleteStudent();
}

}
while (choice!= 6);
}
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
do a code trace on how a key gets deleted package interview; import java.util.*; public class...
do a code trace on how a key gets deleted package interview; import java.util.*; public class binarySearchTree { Node root; void insert(int key) { root = insertRec(root,key); } void delete(int key) { root = deleteRec(root,key); } Node insertRec(Node root, int key) { if(root == null) { root = new Node(key); return root; } if(key < root.key) { root.leftChild = insertRec(root.leftChild,key); } else if(key > root.key){ root.rightChild = insertRec(root.rightChild,key); } return root; } Node deleteRec(Node root, int key) { if(root ==...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent...
Java: ModifyStudentList.javato use anArrayListinstead of an array In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and should add the given Student to the StudentList. The removeStudent method should have one parameter of type String. The String is the email of the student to be removed from the StudentList. In Assign5Test.java do the following: Create a new StudentList containing 3 students. Print the info of all the Students in the StudentList using...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...