Question

In: Computer Science

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 addStudent(String student) {
        // Automatically increases the array size
        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){
        //find the index of student
        String [] b = new String[a.length-1];
        students = a;
        // int position = findStudent(student);
        for (int i = 0,k=0; i < a.length;i++){
            if (i == index){
                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;
    }
}


import java.util.Scanner;

public class d {

    public static void main(String[] args) {
        COurseCom666 com666 = new COurseCom666("com666");
        com666.addStudent("jake");
        com666.addStudent("becky");
        com666.addStudent("mai");
        com666.addStudent("sam");
        com666.addStudent("paul");

        int sum = 0;
       
        String students1[] = com210.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. 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();
                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("----------------------------------");
            }
        }
        while (choice!= 4);
    }
}


Solutions

Expert Solution

Program :-

import java.util.Scanner;

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) {
// Automatically increases the array size
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];
}
}
students = a;
numberOfStudents++;
return a;
}
public 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){
//find the index of student
String [] b = new String[a.length-1];
students = a;
// int position = findStudent(student);
for (int i = 0,k=0; i < a.length;i++){
if (i == index){
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 Demo {

public static void main(String[] args) {
COurseCom666 com666 = new COurseCom666("com666");
com666.addStudent("jake");
com666.addStudent("becky");
com666.addStudent("mai");
com666.addStudent("sam");
com666.addStudent("paul");

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. 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, index);
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("----------------------------------");
}
  
  
}
while (choice!= 4);
  
scan.close();
}
}


Related Solutions

---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...
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;    ...
----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...
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
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...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
/** 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT