Results 1 to 9 of 9
  1. #1
    BlueMoon is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Help with overloading methods and nested classes?

    So... I have to modify a program called StudentBody, which contains student objects. I also had to design 'Course' class which represents a course taken at a school, and create methods to add students, calculate average test scores, and a method called roll which prints all students enrolled in the course. The add student method is supposed to allow up to 5 students to enroll. This method seems to not be working. The program keeps printing "course full" even though its not. I'm also stuck working on the main method, where I'm supposed to add a few students, print a roll, and print the average test score from a specific course. I'm completely lost as to how I should go about doing this. I'm suspecting I may have done something wrong in the add student method, where each student is added as s1, s2 (etc), but when I add students in the main method they are given actual names and not assigned a number.
    Also, I'm sick and not exactly well-rested, so feel free to point out any other mistakes, either in the program or even with terminology.
    If needed, I'll post the whole program.

    This is the Course class:
    Java Code:
     class Course
    {
        private String courseName;
        private int num_students = 0;
        private Student s1, s2, s3, s4, s5;  //s = student
        private String first, last;
        private Address home, school;
        
        public Course (String courseName) //constructor, accepts only course name
        {
        this.courseName = courseName;
        
        }
        public int addStudent (Student s)
        { 
            switch (num_students)
            {
                case '0':
                    s1 = new Student (first, last, home, school);
                    num_students++;
                case '1':
                    s2 = new Student (first, last, home, school);
                    num_students++;
                case '2':
                    s3 = new Student (first, last, home, school);
                    num_students++;
                case '3':
                    s4 = new Student (first, last, home, school);
                    num_students++;
                case '4':
                    s5 = new Student (first, last, home, school);
                    num_students++;
                default:
                    System.out.println ("Course Full!"); 
                    return num_students;
            }
        }
        public int average() {
            int average = (s1.average () + s2.average () + s3.average () + s4.average ()
                            + s5.average ())/(num_students);
            return average;
            }
        public String roll()
        {
            String result;
            result = s1 + "\n";
            result += s2 + "\n"; 
            result += s4 + "\n";
            result += s5 + "\n";
            
            return result;
    And the main method:
    Java Code:
    public class StudentBody
    {
        //Creates Address and Student Objects
        public static void main (String[] args)
        {
            Address school = new Address ("800 Lancaster Ave.", "Villanova",
                                          "PA", 19085);
            Address jHome = new Address ("21 Jump Street", "Lynchburg", 
                                          "VA", 24551);
            Student john = new Student ("John", "Smith", jHome, school);
            john.setTestScore (1,91); 
            john.setTestScore (2,86);
            john.setTestScore (3,94);
            john.average ();
            System.out.println (john);
            System.out.println ();
            
            Address mHome = new Address ("123 Main Street", "Euclid", "OH",
                                         44132);
            Student marsha = new Student ("Marsha", "Jones", mHome, school);
            marsha.setTestScore(1, 85);
            marsha.setTestScore(2, 90);
            marsha.setTestScore(3, 100);
            marsha.average ();
            System.out.println (marsha);
                
            Course CS = new Course ("Computer Science");
            
            CS.addStudent (john);
            CS.addStudent (marsha);
            CS.roll();
            
        }
    }
    Last edited by BlueMoon; 11-27-2013 at 06:26 AM.

  2. #2
    BlueMoon is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help with overloading methods and nested classes?

    Also, I'm suspecting the main class (StudentBody), is supposed to contain all of the other classes (Student, Course....) However, I tried to put all of the classes inside the brackets of the student body class, but I'm getting an error saying that non-static variable can not be referenced from a static context, and pointing to 'new'.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Help with overloading methods and nested classes?

    First, the reason you are getting class full is that you are switching on an int value but your case statements are char values. So remove the single quotes from your case targets.


    Don't put your classes inside the brackets (unless you want to create inner classes which you probably don't at this point). Put them after the StudentBody class but don't make them public. Only one non-inner class may be public per file. So if your file was named Foo.java you could have the following in the same file.

    Java Code:
    public class Foo {
    //methods
    }
    class Bar {
    //methods
    }
    class Otherclass {
    //methods
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    BlueMoon is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help with overloading methods and nested classes?

    Thank you for replying.
    I didn't have the quotes there initially, and I still had the same issue. I added them when I was just messing around with the program because I had no idea what to change. I still feel like something is wrong with the switch statement. When a student is added s1 = new Student. When I add a new student in the main method, including the actual name and other information, should I somehow be including s1? Or does the program itself assign s1 to the first student added?

    If the classes are organized in this way, should they be able to interact with each other? When I wrote the switch statement, 'first','last', 'home', and 'school' could be not be found. I had to declare this data in the course class, even though its already declared in a class called Student.

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Help with overloading methods and nested classes?

    Case statements fall through if you don't end them with a break.
    Java Code:
    switch (something) {
       case 1:
          System.out.println("one");
          break;
       case 2:
          System.out.println("two");
          break;
       default:
          System.out.println("default");
    }
    Without those break's in there the above would print (for a something == 1):
    one
    two
    default
    with them it would print just "one".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Help with overloading methods and nested classes?

    I homed in on the int switching on a character and missed the fact that there were no breaks there. I noticed it this morning when I read the OP's reply.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    BlueMoon is offline Member
    Join Date
    Nov 2013
    Posts
    4
    Rep Power
    0

    Default Re: Help with overloading methods and nested classes?

    Quote Originally Posted by Tolls View Post
    Case statements fall through if you don't end them with a break.
    Java Code:
    switch (something) {
       case 1:
          System.out.println("one");
          break;
       case 2:
          System.out.println("two");
          break;
       default:
          System.out.println("default");
    }
    Without those break's in there the above would print (for a something == 1):
    one
    two
    default
    with them it would print just "one".
    I had the breaks in there, and I thought I kept running into the same issue, but I probably just encountered a different problem, tried removing the breaks, and then completely forgot about them.
    Its entirely possible there are other mistakes I overlooked as well.
    So do switch statements always contain breaks, or are there cases where they are not needed?

    Now I can compile the program, but when I try to print a list of the students enrolled in a course, it just says null. It looks like s1, s2, s3, s4, and s5 are not being assigned the student information that I add in the main method. I did write s1 = new Student (first, last, home, school), which has the same number of parameters as a student object. Do I have to set it in the main method? Do I have to use s1 to create a student object? For example: instead of "Student john = new Student ("John", "Smith", home, school)", do I replace "john" with "s1". Although, when I tried doing this I think the program couldn't locate 's1'.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Help with overloading methods and nested classes?

    Quote Originally Posted by BlueMoon View Post
    So do switch statements always contain breaks, or are there cases where they are not needed?
    Actually, it is quite common to not have breaks on every case statement. If you have an identical action to be done for two or more case statements, then you can put them one after the other followed by the action and then a break.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default Re: Help with overloading methods and nested classes?

    Quote Originally Posted by BlueMoon View Post
    Now I can compile the program, but when I try to print a list of the students enrolled in a course, it just says null. It looks like s1, s2, s3, s4, and s5 are not being assigned the student information that I add in the main method. I did write s1 = new Student (first, last, home, school), which has the same number of parameters as a student object. Do I have to set it in the main method? Do I have to use s1 to create a student object? For example: instead of "Student john = new Student ("John", "Smith", home, school)", do I replace "john" with "s1". Although, when I tried doing this I think the program couldn't locate 's1'.
    Stick some debugging code in there (println statements) so you can what route the code is taking and the value of things.
    Or use a debugger.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Overloading Abstract Class Methods ??
    By sreeram in forum New To Java
    Replies: 5
    Last Post: 12-06-2012, 12:36 PM
  2. Problem when Overloading Classes
    By Army in forum New To Java
    Replies: 3
    Last Post: 04-12-2012, 09:07 AM
  3. Using Arrays and Overloading Methods.
    By dalek in forum New To Java
    Replies: 5
    Last Post: 10-25-2011, 04:52 AM
  4. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 09:45 AM
  5. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 02:34 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
  •