Results 1 to 5 of 5
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Yet another "non-static method cannot be referenced fro a static context" problem

    Hello again folks

    Need a little help here. I'm having a problem with this code.

    Java Code:
    import java.util.*;
    public class Student extends TutorGroup
    {
       public static final int NUMBER_OF_TMAS = 2;
       
       private String name; // the student's name
       public int[] tmaMarks = new int[Student.NUMBER_OF_TMAS] ;
       private char grade;  // the student's final grade
        */
       public Student(String aName)
       {
          super();
          this.name = aName;
          //tmaMarks to be initialised here
          Arrays.fill(this.tmaMarks,0);
          this.grade = 'X';
       }
      public void setMark(int tma, int mark)
       {
          if (tma > tmaMarks.length)
          {
            int[] newTmaMarks = new int[tma];
            newTmaMarks[tma-1] = mark;
            tmaMarks = newTmaMarks;
          }
          else
          {
         tmaMarks[tma - 1] = mark;     
          }
       }   
       public int[] getMarks()
       {
          return this.tmaMarks;
       }
    }
    Java Code:
    import java.util.*;
    import ou.*;
    public class TutorGroup
    {
       /* instance variables */
       Map<String, Student> students; 
       Map<Character, Student> results; 
       int aInt = 0;
       /**
        * Constructor for objects of class TutorGroup
        */
       public TutorGroup()
       {      
          students = new HashMap<String, Student>();          
       }
       /* instance methods */
       public void addStudent(String name)
       {
         // students.put(name, Integer.parseInt(Arrays.toString(this.Student.getMarks())) );
         Student aStudent = new Student(name);
         students.put(name, aStudent);
       }
        public void enterMarksForTMA(int tma)
       {     
          Set<String> keys = students.keySet();
          if (tma > 5)
          {
             OUDialog.alert("Invalid TMA number");
          }
          else     
          {
               for (int i=0; i < 5;i++)      
             {   
             for(String allStudents : keys)
                {
                tma = Integer.parseInt(OUDialog.request("Enter TMA "  + (i + 1)  + " mark for " + allStudents)); 
                 Student.setMark((i+1), tma); 
                }   
             }
          }
       }  
    }
    The issue lies with the line in the TutorGroup class
    Java Code:
    Student.setMark((i+1), tma);
    I know what I'm doing isn't allowed in java, but what I can't work out is how to solve this. What I'm attempting to to is this. The TutorGroup class has to create a Map where the names of students act as the keys. The Map should contain instances of Student objects. The Class Student holds and array of marks. The TutorGroup Class should provide the array with the values provided by the method enterMarksForTMA(). The Student class will do some calculations on the marks then this should be applied to the Student object held in the Map refrenced by the key. How do I write the code to do that? I haven't the faintest idea.

    BTW, the class TutorGroup is still incomplete as I can't overcome this sticking point. Any help would be greatly appreciated.

    Thanks

  2. #2
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    Why does Student extend TutorGroup? This sounds wrong.

    Java Code:
    students.get(allStudents).setMark((i+1), tma);
    ?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I agree with Onra, Student should absolutely not extend TutorGroup.

    then in this code, please look at the comments:

    Java Code:
    for(String allStudents : keys)  {
       tma = Integer.parseInt(OUDialog.request("Enter TMA "  + (i + 1)  + " mark for " + allStudents)); 
       // Student.setMark((i+1), tma);  // Don't do this!!
       // If you have a String, allStudents, and a HashMap<String, Student>, 
       // how can you extract the Student of interest?
    }

  4. #4
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Onra View Post
    Why does Student extend TutorGroup? This sounds wrong.

    Java Code:
    students.get(allStudents).setMark((i+1), tma);
    ?
    This^^ brilliant. Works brill. Also, not sure why Student extended TutorGroup, fix now.

    However, I now have a new issue. The assignment states that the code must be able to handle more TMA marks than the final variable in Student
    Java Code:
     public static final int NUMBER_OF_TMAS = 2;
    hence the method:
    Java Code:
     public void setMark(int tma, int mark)
       {
         // tma = TutorGroup.enterMarksForTMA(tma);
          if (tma > tmaMarks.length)
          {
            int[] newTmaMarks = new int[tma];
            newTmaMarks[tma-1] = mark;
            tmaMarks = newTmaMarks;
          }
          else
          {
         tmaMarks[tma - 1] = mark;     
          }
       }
    The assignment specifically says that the method
    Java Code:
       public void enterMarksForTMA(int tma)
    should check that the tma value is valid, without knowing what the upper value of TMA is. Therefore, when I send the message
    Java Code:
    student.enterMarksForTMA(3);
    the first time round, the code needs to understand that the Array in Student is of the size 3. Then, if I send a new message
    Java Code:
    student.enterMarksForTMA(4);
    I should get an alert that states that the TMA number is invalid. Any pointers on how I can achieve this?
    Thanks again for all your help.

  5. #5
    Onra is offline Member
    Join Date
    Nov 2009
    Posts
    41
    Rep Power
    0

    Default

    the code needs to understand that the Array in Student is of the size 3.
    With which method can you check the size of an array?

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 11:12 AM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  3. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 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
  •