Results 1 to 10 of 10
  1. #1
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default Adding elements to an array

    I am having problems with an assignment. In the assignment we are given most of the program, with a few bits missing. The part I am having trouble with is logging in as a student and adding a course. The code that I need help with is under public void addClass.

    Java Code:
    package assignment03;
    public class Student extends Person
    {
        private String major;
        private int    collegeYear; // 1 freshman 2 sophomore 3 junior 4 senior
        private Course[] enrollment;
        private int  numberOfCourses;
        public Student()
        {//calls base class constructor Person() automatically
        }
        public Student(String ID, String firstName, String lastName, String major, String password, int collegeYear, int userType)
        {
            super(ID, firstName, lastName, password, userType);
            this.major       = major;
            this.collegeYear = collegeYear;
            enrollment = new Course[3];
            numberOfCourses =0;
        }
        public int getNumberOfCourses()
        {
            return numberOfCourses;
        }
        public void addClass(Course course)
        {
            // THIS FUNCTION WILL ADD course OBJECT TO THE ARRAY enrollment
            // PLEASE NOTICE THAT
            // A STUDENT CANNOT TAKE MORE THAN 3 COURSES
            // IF A CAPACITY IS FULL, THE STUDENT CANNOT ENROLL.
            // STUDENT CANNOT RE-REGISTER.
             /*if(numberOfCourses<=3)
                       {
                            
                        }
                        else
                        {
                            System.out.println(" You have too many registered Courses!");
                        }
            numberOfCourses++;
                */
            // INCOMPLETE FUNCTION
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
    
        }
        public String toString()
        {
            return super.toString()+ " " +  Application.addSpaces(major,20) + " " + collegeYear;
        }
        public void dropClass(String courseNumber)
        {
            // REMOVE THE COURSE WITH THE courseNumber FROM TEH ARRAY enrollment.
            // UPDATE THE THE ARRAY enrollment.
            // STUDENT CANNOT DROP AN UNREGISTERED CLASS.
    
               numberOfCourses--;
            // INCOMPLETE FUNCTION
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
        }
        public void showMyClasses()
        {
            // PRINT THE CLASSES IN THE enrollment.
            System.out.println(numberOfCourses + " courses");
            System.out.println(enrollment);
            // INCOMPLETE FUNCTION
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
        }
    
        public boolean takeTheClass(String courseID)
        {
             boolean result = false;
            // THIS FUNCTION SHOULD CHECK WHETHER A STUDENT IS ENROLLED TO THE CLASS courseID, OR NOT.
            // RETURNS FALSE IF THE STUDENT IS NOT REGISTERED.
            // RETURN TRUE IF THE STUDENT IS  ENROLLED.
    
            // YOU CAN CHANGE EVERYTHING IN THIS FUNCTION INCLUDING THE STAMENTS:
            //  boolean result = false;
            //  return result;
               if (courseID.equals(courseID))
               {
                   result= true;
               }
    
            // INCOMPLETE FUNCTION
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
            // oooooooo  ooooooooo
            // ooooooooooooooooooo
            return result;
        }
    }
    The class Course has (just a piece of it)
    Java Code:
    public Course(String number, String name, String instructorID, int capacity)
        {
            this.number       = number;
            this.name         = name;
            this.instructorID = instructorID;
            this.capacity     = capacity;
            numberOfStudents  = 0;
        }
    Which I have been trying to extract name out of it, and its not letting me.
    Also, the class Application has (again, just a piece of the code)
    Java Code:
    / Create Courses
            courseDatabase[numberOfCourses++] = new Course("1320", "Intro to Computer Science", "52037101", 4);
            courseDatabase[numberOfCourses++] = new Course("1410", "Data Structures", "10132103", 4);
            courseDatabase[numberOfCourses++] = new Course("2430", "Artificial Intelligence", "32546104", 3);
            // Enrollment
            ((Student)userDatabase[0]).addClass(courseDatabase[0]);
            ((Student)userDatabase[1]).addClass(courseDatabase[0]);
            ((Student)userDatabase[2]).addClass(courseDatabase[0]);
            ((Student)userDatabase[3]).addClass(courseDatabase[0]); // 1320, full
    
            ((Student)userDatabase[4]).addClass(courseDatabase[1]);
            ((Student)userDatabase[5]).addClass(courseDatabase[1]); // 1410, 2 available
    
            ((Student)userDatabase[6]).addClass(courseDatabase[2]);
            ((Student)userDatabase[7]).addClass(courseDatabase[2]); // 2430, 1 available
        }
    So basically I am trying to let the user add a course as a student. It has to be entered into the array so I can recall it later (if you look at the class student, it asks me to eventually drop the class and show the classes). I think if i can get help with this one part I can find out the rest. If I need to, I can post the complete codes of the classes.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    All I see is a buck load of code and some requirements. I don't see a question and don't ask "How do I do it?". What part do you not understand? Ask a specific question, get a specific answer.

  3. #3
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    All I see is a buck load of code and some requirements. I don't see a question and don't ask "How do I do it?". What part do you not understand? Ask a specific question, get a specific answer.
    I need to fill in the blank at:

    // THIS FUNCTION WILL ADD course OBJECT TO THE ARRAY enrollment
    // PLEASE NOTICE THAT
    // A STUDENT CANNOT TAKE MORE THAN 3 COURSES
    // IF A CAPACITY IS FULL, THE STUDENT CANNOT ENROLL.
    // STUDENT CANNOT RE-REGISTER.

    I have tried some pathetic ideas and honestly, I have no idea where to go. I am assuming I need to access courseDatabase Array, but when I try I get "Cannot find symbol".

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by slitka View Post
    I need to fill in the blank at:
    That is a statement and not a question. "I need a 10 million dollars". What are you going to do about that? <- see that was a question.
    I am assuming I need to access courseDatabase Array
    You assume wrong.
    but when I try I get "Cannot find symbol".
    That is because it is in a different class. You are in the Student class. What array does it have? Perhaps you should try accessing that array instead.

  5. #5
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    That is a statement and not a question. "I need a 10 million dollars". What are you going to do about that? <- see that was a question.

    You assume wrong.
    Woke up on the wrong side of the bed this morning?
    That is because it is in a different class. You are in the Student class. What array does it have? Perhaps you should try accessing that array instead.
    The only array I see in the student class is course[]. Would this be a correct way to input the information?

    if(numberOfCourses<=3)
    {
    enrollment[0] = course;
    enrollment[1] = course;
    enrollment[2] = course;
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Getting closer. Think about what your code does. If a Student tries to enrol in a class your code will enrol them 3 times. Hmmm, maybe you only want to add the course once.

    Another thing before you can add the course to the Student's array of enrolled courses you have to check if there is enough room left ie you need to check the capacity.

    Lastly I would move the numberOfClassess++ to be inside the if statement so it is only updated if the course is actually added.

  7. #7
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    Thank you for the help. Now, I am having trouble accessing Course for the capacity.
    Java Code:
    if(numberOfCourses<3)
            {
                        if(Course.isFull())
                       {
                           System.out.println("The class is full.");
                       }
                        else
                       {
                            enrollment[0] = course;
                            numberOfCourses++;
                            Course.numberOfStudents++;
                        }
            }
             else
                        {
                            System.out.println(" You have too many registered Courses!");
                        }
    How would I go about and accessing capacity and numberOfStudents even though they are private?
          }
    at my if(course.isFull()) it is saying "non-static method isFull() cannot be referenced from a static context." and at my Course.numberOfStudents it is saying that "numberOfStudents has private access in assignment03.Course".

    I know why the Course.numberOfStudents is not working. numberOfStudents is private in Course. However, in the assignment instructions we can't change the code in the program, except for certain areas so I can't change it to public.

    I have no idea what the isFull error is saying. Is that even the right way to call it?

    This is the Course class:
    Java Code:
    public class Course
    {
        private String number;
        private String name;
        private String instructorID;
        private int    capacity;
        private int    numberOfStudents;
        public Course(String number, String name, String instructorID, int capacity)
        {
            this.number       = number;
            this.name         = name;
            this.instructorID = instructorID;
            this.capacity     = capacity;
            numberOfStudents  = 0;
        }
        public boolean isFull()
        {
            return (capacity==numberOfStudents);
        }
        public String getNumber()
        {
            return number;
        }
        public String toString()
        {
            return number + " " +  Application.addSpaces(name,28) + " " + instructorID + " " + numberOfStudents +"/" +capacity;
        }
        public void increaseNumberOfStudents()
        {
            numberOfStudents++;
        }
        public void decreaseNumberOfStudents()
        {
            numberOfStudents--;
        }
        public String getName()
        {
            return name;
        }
        public String getInstructorID()
        {
            return instructorID;
        }
    }
    Again thanks for helping. I'm sure I am annoying as hell with the questions haha.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    if(Course.isFull())
    Java is case sensitive. Course != course
    Java Code:
    Course.numberOfStudents++;
    Ditto. Also, numberOfStudents is private and needs to accessed via a method.

  9. #9
    slitka is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default

    I seem to have it mostly working. Theres just one thing I can't get; I can't seem to delete an element from the array. How do I set enrollment back to the default (which was nothing)?
    Java Code:
    public void dropClass(String courseNumber)
        {
            // REMOVE THE COURSE WITH THE courseNumber FROM THE ARRAY enrollment.
            // UPDATE THE THE ARRAY enrollment.
            // STUDENT CANNOT DROP AN UNREGISTERED CLASS.
            for(int i=0; i<numberOfCourses;i++)
            {
                if( enrollment[i].getNumber().equals(courseNumber) )
                {
                    enrollment[0]=enrollment[i];
                    numberOfCourses--;
                    System.out.println ("You have Dropped the class");
                }
    
                else
                {
                    System.out.println("You don't have a class like that");
                }
    
                
    
            }
    After this, I think I can finish the rest myself.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Deleting "things" from an array isn't that simple. For Objects you can set the element to null but then you end up with holes. eg {"Biology", "Math", "English", "Chemistry"} if you want the drop Math the array becomes {"Biology", null, "English", "Chemistry"}. Then when you want to add another course you have to iterate over the array to find the hole.

    A better solution is to copy everything down one slot. Using the above example the array becomes {"Biology", "English", "Chemistry", "Chemistry"}. Note that the last element is duplicated. That doesn't matter if you are using numberOfCourses correctly. If you print all the courses then it stops after the first "Chemistry". If you add another course then it is inserted at the end and overwrites the second "Chemistry". {"Biology", "English", "Chemistry", "History"}

Similar Threads

  1. Adding elements before and after array
    By liQuorice in forum New To Java
    Replies: 4
    Last Post: 03-04-2011, 01:52 AM
  2. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 04:05 PM
  3. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-08-2010, 12:04 AM
  4. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 04:07 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 04:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •