Results 1 to 5 of 5
- 03-13-2011, 04:19 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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; } }The issue lies with the line in the TutorGroup classJava 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); } } } } }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.Java Code:Student.setMark((i+1), tma);
BTW, the class TutorGroup is still incomplete as I can't overcome this sticking point. Any help would be greatly appreciated.
Thanks
- 03-13-2011, 04:48 PM #2
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
Why does Student extend TutorGroup? This sounds wrong.
?Java Code:students.get(allStudents).setMark((i+1), tma);
-
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? }
- 03-13-2011, 11:05 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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 Studenthence the method:Java Code:public static final int NUMBER_OF_TMAS = 2;
The assignment specifically says that the methodJava 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; } }should check that the tma value is valid, without knowing what the upper value of TMA is. Therefore, when I send the messageJava Code:public void enterMarksForTMA(int tma)
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 messageJava Code:student.enterMarksForTMA(3);
I should get an alert that states that the TMA number is invalid. Any pointers on how I can achieve this?Java Code:student.enterMarksForTMA(4);
Thanks again for all your help.
- 03-14-2011, 12:05 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
Non-static method cannot be referenced from a static context
By GRoss in forum New To JavaReplies: 7Last Post: 05-19-2010, 11:12 AM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
Method cannot be referenced from a static context - HELP
By jmorris in forum New To JavaReplies: 11Last Post: 11-19-2008, 03:13 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks