Thursday, 15 May 2014

java - (Another) "non-static method cannot be referenced from a static context" issue -



java - (Another) "non-static method cannot be referenced from a static context" issue -

error: non-static method cannot referenced static context.

in case method called readfile().

hi. i'm experiencing same error countless novice programmers have before, despite reading hours on end, can't work out how apply knowledge in situation.

i think code may need restructured including total text of class.

i prefer house main() method in little main class, have placed in same class here simplicity. same error no matter set it.

the readfile() method placed within main() method, i’d prefer larn how create little modular methods , phone call them main() method. closefile() separate method too.

the programme supposed to:

open .dat file read in file info regarding exam results perform calculations on information output results of calculations

each line of file info individual student. single consists of 3 exam papers. calculations regard individual students. calculations regard entire collection of students (ie class).

nb: word “class” used in code, refers academic class of students, not class in sense of oo programming.

i have tried various ways solve problem.

current approach house info concerning single pupil exam in instance of class “exam”. corresponds single line of input file, plus subsequent calculations concerning other attributes of instance only.

these attributes populated values during while loop of readfile(). when while loop ends, 3 calculations concern entire collection of exams (ie entire academic class) called.

a secondary question is: under comment “declare attributes”, i’ve separated attributes of class 2 subgroups: think should defined class variables (with keyword static). think should defined instance variables. guide me on whether should add together keyword static in first group.

a related question is: should methods perform calculations using entire collection of instances declared static / class methods too? when tried got similar errors when these tried phone call instance methods.

thanks.

ps: regarding forum: have enclosed code code blocks, java syntax not highlighted. perhaps alter after submit post. if not i'd happy reformat if can tell me how.

pps: homework assignment. have created code below myself. "homework" tag obsolete, didn't utilize it.

input file name: "results.dat" input file path: "c:/users/admin/desktop/a1p3e1 files/results.dat" input file contents (randomly generated data):

573,kalia,lindsay,2,8,10 966,cheryl,sellers,8,5,3 714,shea,wells,7,6,2 206,april,mullins,8,2,1 240,buffy,padilla,3,5,2 709,yoko,noel,3,2,5 151,armand,morgan,10,9,2 199,kristen,workman,2,3,6 321,iona,maynard,10,2,8 031,christen,short,7,5,3 863,cameron,decker,6,4,4 986,kieran,harvey,7,6,3 768,oliver,rowland,8,9,1 273,clare,jacobs,9,2,7 556,chaim,sparks,4,9,4 651,paloma,hurley,9,3,9 212,desiree,hendrix,7,9,10 850,mckenzie,neal,7,5,6 681,myra,ramirez,2,6,10 815,basil,bright,7,5,10

java file name: "exam.java" java bundle name: "a1p3e1" java project name: "a1p3e1" java file contents:

/** todo * [+] error block - add together statement * [?] studentnumber - alter info type integer (or maintain string) * [?] alter scope of non-instance variables static (eg classexamgradesum) * [*] solve "non-static method cannot referenced static context" error * */ bundle a1p3e1; // assignment 1 - part 3 - exercise 1 import java.io.*; import java.util.*; /** * * @author */ public class exam { // (1) declare attributes // (-) class attributes protected scanner filein; protected scanner linein; private string line; private string [] splitline; private string infilepath = "c:/users/admin/desktop/a1p3e1 files/results.dat"; private int fileinrowcount = 20; private int fileincolumncount = 6; private int fileoutrowcount = 20; private int fileoutcolumncount = 14; // private int classexamgradesum = 0; private int classexamgradesum; private double classexamgradeaverage = 0.0; private int [] classexamgradefrequency = new int [10]; protected exam exam [] = new exam [fileinrowcount]; // (-) instance attributes private string studentnumber; private string forename; private string surname; private int paper1grade; private int paper2grade; private int paper3grade; private string paper1outcome; private string paper2outcome; private string paper3outcome; private int fileinrowid; private int failcount; private int gradeaveragerounded; private int gradeaveragequalified; private string examoutcome; // (3) tostring method overridden @override public string tostring () { homecoming "\n pupil number: " + studentnumber + "\n forename: " + forename + "\n surname: " + surname + "\n paper 1 grade: " + paper1grade + "\n paper 2 grade: " + paper2grade + "\n paper 3 grade: " + paper3grade + "\n paper 1 outcome: " + paper1outcome + "\n paper 2 outcome: " + paper2outcome + "\n paper 3 outcome: " + paper3outcome + "\n file in row id: " + fileinrowid + "\n fail count: " + failcount + "\n exam grade rounded: " + gradeaveragerounded + "\n exam grade qualified: " + gradeaveragequalified + "\n exam outcome: " + examoutcome; } // (4) accessor methods public string getstudentnumber () { homecoming studentnumber; } public string getforename () { homecoming forename; } public string getsurname () { homecoming surname; } public int getpaper1grade () { homecoming paper1grade; } public int getpaper2grade () { homecoming paper2grade; } public int getpaper3grade () { homecoming paper3grade; } public string getpaper1outcome () { homecoming paper1outcome; } public string getpaper2outcome () { homecoming paper2outcome; } public string getpaper3outcome () { homecoming paper3outcome; } public int getfileinrowid () { homecoming fileinrowid; } public int getfailcount () { homecoming failcount; } public int getgradeaveragerounded () { homecoming gradeaveragerounded; } public int getgradeaveragequalified () { homecoming gradeaveragequalified; } public string getexamoutcome () { homecoming examoutcome; } // (5) mutator methods public void setstudentnumber (string studentnumber) { this.studentnumber = studentnumber; } public void setforename (string forename) { this.forename = forename; } public void setsurname (string surname) { this.surname = surname; } public void setpaper1grade (int paper1grade) { this.paper1grade = paper1grade; } public void setpaper2grade (int paper2grade) { this.paper2grade = paper2grade; } public void setpaper3grade (int paper3grade) { this.paper3grade = paper3grade; } public void setpaper1outcome (string paper1outcome) { this.paper1outcome = paper1outcome; } public void setpaper2outcome (string paper2outcome) { this.paper2outcome = paper2outcome; } public void setpaper3outcome (string paper3outcome) { this.paper3outcome = paper3outcome; } public void setfileinrowid (int fileinrowid) { this.fileinrowid = fileinrowid; } public void setfailcount (int failcount) { this.failcount = failcount; } public void setgradeaveragerounded (int gradeaveragerounded) { this.gradeaveragerounded = gradeaveragerounded; } public void setgradeaveragequalified (int gradeaveragequalified) { this.gradeaveragequalified = gradeaveragequalified; } public void setexamoutcome (string examoutcome) { this.examoutcome = examoutcome; } // (2) constructor methods // (-) constructor method - no arguments public exam () { this.studentnumber = ""; this.forename = ""; this.surname = ""; this.paper1grade = 0; this.paper2grade = 0; this.paper3grade = 0; this.paper1outcome = ""; this.paper2outcome = ""; this.paper3outcome = ""; this.fileinrowid = 0; this.failcount = 0; this.gradeaveragerounded = 0; this.gradeaveragequalified = 0; this.examoutcome = ""; } // (-) constructor method - arguments (1) public exam ( string studentnumber, string forename, string surname, int paper1grade, int paper2grade, int paper3grade, string paper1outcome, string paper2outcome, string paper3outcome, int fileinrowid, int failcount, int gradeaveragerounded, int gradeaveragequalified, string examoutcome) { this.studentnumber = studentnumber; this.forename = forename; this.surname = surname; this.paper1grade = paper1grade; this.paper2grade = paper2grade; this.paper3grade = paper3grade; this.paper1outcome = paper1outcome; this.paper2outcome = paper2outcome; this.paper3outcome = paper3outcome; this.fileinrowid = fileinrowid; this.failcount = failcount; this.gradeaveragerounded = gradeaveragerounded; this.gradeaveragequalified = gradeaveragequalified; this.examoutcome = examoutcome; } // (-) constructor method - arguments (2) public exam ( string studentnumber, string forename, string surname, int paper1grade, int paper2grade, int paper3grade) { this.studentnumber = studentnumber; this.forename = forename; this.surname = surname; this.paper1grade = paper1grade; this.paper2grade = paper2grade; this.paper3grade = paper3grade; this.paper1outcome = ""; this.paper2outcome = ""; this.paper3outcome = ""; this.fileinrowid = 0; this.failcount = 0; this.gradeaveragerounded = 0; this.gradeaveragequalified = 0; this.examoutcome = ""; } // (6) main method public static void main (string[] args) throws exception { exam.readfile (); } // (7) other methods // (-) read file instances of exam class // limitation: hard coded 6 column source file public void readfile () throws exception { seek { filein = new scanner(new bufferedreader(new filereader(infilepath))); int = 0; while (filein.hasnextline ()) { line = filein.nextline(); splitline = line.split (",", 6); // create instances of exam file info , calculated info exam [i] = new exam ( splitline [0], splitline [1], splitline [2], integer.parseint (splitline [3]), integer.parseint (splitline [4]), integer.parseint (splitline [5]), convertgradetooutcome (paper1grade), convertgradetooutcome (paper2grade), convertgradetooutcome (paper3grade), + 1, failcount (), gradeaveragerounded (), gradeaveragequalified (), convertgradetooutcome (gradeaveragequalified)); filein.nextline (); ++; } classexamgradefrequency (); classexamgradesum (); classexamgradeaverage (); // close file filein.close (); } grab (filenotfoundexception | numberformatexception e) { // filein.next (); system.err.println("error: " + e.getmessage()); //system.out.println ("file error - io exception"); } (exam : exam) { system.out.println(i.tostring()); system.out.println(); } // system.out.println(classexamgradesum); // system.out.println(); system.out.println(classexamgradeaverage); system.out.println(); system.out.println(classexamgradefrequency); system.out.println(); } // (-) fail count (1 student, 3 papers) public int failcount () { // if (paper1grade > 6){ failcount = failcount + 1; } if (paper2grade > 6){ failcount = failcount + 1; } if (paper3grade > 6){ failcount = failcount + 1; } homecoming failcount; } // (-) grade average rounded (1 student, 3 papers) public int gradeaveragerounded () { gradeaveragerounded = (int) math.ceil( (paper1grade + paper2grade + paper3grade) / 3); homecoming gradeaveragerounded; } // (-) grade average qualified (1 student, 3 papers) public int gradeaveragequalified (){ gradeaveragequalified = gradeaveragerounded; if (failcount >= 2 && gradeaveragerounded <= 6) { gradeaveragequalified = 7; } homecoming gradeaveragequalified; } // (-) convert grade outcome (pass / fail) public string convertgradetooutcome (int grade) { string outcome; if (grade <= 6){ outcome = "pass"; } else if (grade > 6){ outcome = "fail"; } else { outcome = "unknown (error)"; } homecoming outcome; } // (-) class exam grade sum (all students, papers) /** assumption: average grade class average of grades awarded, * using rounded figures, not raw per paper results */ public void classexamgradesum () { classexamgradesum = 0; // each loop (to iterate through collection of exam instances) (exam : exam) { classexamgradesum = classexamgradesum + i.gradeaveragequalified; } } // (-) class exam grade average (all students, papers) /** assumption: average grade class average of grades awarded, * using rounded figures, not raw per paper results * assumption: <fileinrowcount> right */ public double classexamgradeaverage () { classexamgradeaverage = classexamgradesum / fileinrowcount; homecoming classexamgradeaverage; } // (-) class exam grade frequency (count of instances of each final grade) /** example: * frequency of average grade "5" * stored in array <classexamgradefrequency [4]> */ public void classexamgradefrequency () { // each loop (to iterate through collection of exam instances) (exam : exam) { classexamgradefrequency [i.getgradeaveragequalified () - 1] ++; } } }// endof class

readfile instance method. create instance of exam use:

new exam().readfile();

given exam has many instance variables, of used in readfile method, method should not static. (use of static class variables creates code smell , should not considered.)

given readfile reads multiple entries file many exam objects, split out read functionality new examreader class.

aside: flexibility utilize list instead of fixed size array

exam exam [];

could be

list<exam> examlist;

java call static-methods non-static main-method

No comments:

Post a Comment