View the step-by-step solution to:

Here's what I'm supposed to do: Steps. (1) Create: Student class containing an ID, a last name, and a list of course names such as...

Here's what I'm supposed to do: 


Steps.

(1) Create: Student class containing an ID, a last name, and a list of course names such as "COP2210" or "COP2250". The ID should be an integer which is printed with leading zeros.

(2) Create: class named StudentTest that contains a List of students. This should not be a string of multiple courses. Do not use the list provided here. In the class constructor, create 15 students and add them to the list. Each student's ID must be different. Each student you add to the list must contain at least 3 different course names. At least one should have two course names and at least one should have four course names. Make sure the students do not all have the same courses, although there should be some overlap (see the sample below). The student names must be inserted into the list in random order (not sorted).

(3) Sort the list in ascending order by student last name and display the list. For each student, display the ID with leading zeros, last name, and list of courses on a single line. 



Here's my code: 



**Student.java:**


import java.util.*;

public class Student

{

   String ID, lastName;

   //Arraylist to store courses

  ArrayList<String> courses = new ArrayList<String>();

   public Student()

   {

       //Default constructor

       ID = "";

       lastName = "";

   }

   public Student(String I, String l)

   {

      //Parameterized Constructor to initialize

       ID = I;

       lastName = l;

       int i, n;

       String temp;

       Scanner sc = new Scanner(System.in);

       while(true){

      System.out.print("nHow many courses you want to add: ");

       n = Integer.parseInt(sc.nextLine());

       if(n < 3){ //Cannot choose less than 3 courses

          System.out.println("nNumber of courses must be at least 3.");

           continue;

       }

       for(i = 1; i <= n; i++)

       {

          System.out.print("nEnter course name: ");

           temp = sc.nextLine();

          if(courses.contains(temp))//If course already present

           {

              System.out.println("nCourse already present. Try another.");

               i--;

           }

           else

           {

              courses.add(temp); //Adding course

           }

       }

   }

   }

   //Accessors

   public String getID()

   {

       return ID;

   }

   public String getName()

   {

       return lastName;

   }

   public ArrayList<String> getCourses()

   {

       return courses;

   }

   //Mutators

   public void setID(String i)

   {

       ID = i;

   }

   public void setName(String n)

   {

       lastName = n;

   }

   public void setCourses(ArrayList<String> c)

   {

      courses.clear();

      courses.addAll(c);

   }

}



**StudentTest.java:**


import java.util.*;

public class StudentTest

{

   //To store 10 students

   Student[] list = new Student[10];

   public StudentTest(){

       int i, j, flag;

       Scanner sc = new Scanner(System.in);

       for(i = 0; i < 10; i++)

       {

           String temp, l;

          System.out.print("nEnter student ID: ");

           temp = sc.nextLine();

           flag = 1;

           for(j = 0; j < i; j++)

           {

              if(list[j].getID().equals(temp))//If ID already exists

               {

                   System.out.println("nID already exists. Try another.");

                  flag = 0;

                  i--;

                  break;

               }

           }

           if(flag == 1)

               {

                  System.out.print("nEnter student Last name: ");

                  l = sc.nextLine();

                  list[i] = new Student(temp, l); //Initializing student

               }

       }

   }

   public void sort()

   {

       //To sort and display

       int i, j;

       String temp;

      ArrayList<String> t = new ArrayList<String>();

       for(i = 0; i < 9; i++)

       {

           for(j = 0; j < 9 - i; j++)

           {

              if(list[j].getName().compareTo(list[j + 1].getName()) > 0)//If list[j + 1].lastName needs to come before list[j].lastName

               {

                  //Swapping IDs

                  temp = list[j].getID();

                  list[j].setID(list[j + 1].getID());

                  list[j + 1].setID(temp);

                  //Swapping last names

                  temp = list[j].getName();

                  list[j].setName(list[j + 1].getName());

                  list[j + 1].setName(temp);

                  //Swapping courses

                  t.clear();

                  t.addAll(list[j].getCourses());

                  list[j].setCourses(list[j + 1].getCourses());

                  list[j + 1].setCourses(t);

               }

           }

       }

       //Display

      System.out.println();

       for(i = 0; i < 10; i++)

       {

          System.out.print(list[i].getID() + ", " + list[i].getName());

           //Using fencepost loop to print with comma before

           System.out.print(" " + list[i].getCourses().get(0));

           for(j = 1; j < list[i].getCourses().size(); j++)

              System.out.print(", " + list[i].getCourses().get(j));

          System.out.println();

       }

   }

   public static void main(String args[])

   {

       StudentTest S = new StudentTest();

       S.sort();

   }

}



This is my output and continuous error:

Screen Shot 2018-05-10 at 9.39.27 AM.png



This is the output that I should have/should be similar:

Screen Shot 2018-05-10 at 9.41.28 AM.png


Can anyone spot code errors that can help with the loop that keeps asking me to enter a course name after I'm done adding courses? It's supposed to have 3 courses per student at 15 students, with the output to look like the list. Please and thank you!


P.S. I only receive an output in Linux. I tried in netbeans, no errors, and I only get "building complete" as my result. I would love to use netbeans so I don't have to use Linux through my VirtualBox, but any solutions will help big time. Thank you.

Screen Shot 2018-05-10 at 9.39.27 AM.png

Screen Shot 2018-05-10 at 9.41.28 AM.png

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question