In: Computer Science
///create a main menu to enter choices such as
"Welcome to any college" then please enter your choice of information when you press a
key 1. view students. 2. insert student 3. remove student 4.
exit...and student ID#, age, birthday, location, expected gradation
date
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("hank");
com666.addStudent("paul");
com666.addStudent("david");
com666.addStudent("ben");
com666.addStudent("jacob");
int sum = 0;
////////create a new array String students1[] to hold all
students/////////////////////
String students1[] = com666.getStudents();
sum += com666.getNumberOfStudents();
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);
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of student and index:");
String student = scan.nextLine();
students1 = com666.addStudentByIndex(student,3);
com666.display(students1);
sum = com666.getNumberOfStudents();
System.out.println("After student was added the Student number is:"
+ sum);
System.out.println("remove 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("----------------------------");
//use do while loops
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];
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;
}
}
public class d {
public static void main(String [] args){
CourseCOM666 com666 = new CourseCOM666("com666");
com666.addStudent("hank");
com666.addStudent("paul");
com666.addStudent("david");
com666.addStudent("ben");
com666.addStudent("jacob");
int sum = 0;
////////create a new array String students1[] to hold all students/////////////////////
String students1[] = com666.getStudents();
sum += com666.getNumberOfStudents();
Scanner scan = new Scanner(System.in);
//use do while loops
int choice;
do {
System.out.println("Welcome to any college");
System.out.println("1. View Student");
System.out.println("2. insert student");
System.out.println("3. remove 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 the Student number is:" + sum);
}
else if(choice == 3) {
System.out.println("remove 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);
}
}