Hi, I was doing a practice exercise for the Java AP exam, and I wanted to test the methods that it told me to write. So I wrote out the program, and the helper program, and tried creating a main method to test it. When I did, I ran across a error that I run across really often, and is quite annoying:
non-static variable scoreList cannot be referenced from a static context
Usually, I just change a method to static, but I can't do that in this program because the variable it is talking about is given, as well as the two method headers. It also says:
cannot find symbol method record(int) for when I try to call the method in the main.
I want to know how to fix the non-static variable, because I seem to get that almost 50% of the time I write programs, and it's pretty annoying. I've done searches and found a lot of people that say to instantiate a constructor of that class, but what if it has none?
import java.io.*;
import java.util.ArrayList;
public class Stats
{
private ArrayList<ScoreInfo> scoreList;
public boolean record(int score)
{
int count = 0;
for(int counter = 0; counter < scoreList.size(); counter++)
{
if(score == scoreList.get(counter).getScore())
{
scoreList.get(counter).increment();
count++;
break;
}
}
if(count == 0)
{
ScoreInfo temp = new ScoreInfo(score);
scoreList.add(temp);
return true;
}
else
{
return false;
}
}
public void recordScores(int[] stuScores)
{
for(int counter = 0; counter < stuScores.length; counter++)
{
for(int newCount = 0; newCount < scoreList.size(); newCount++)
{
if(stuScores[counter] == scoreList.get(0).getScore())
{
scoreList.get(0).increment();
}
else
{
ScoreInfo temp = new ScoreInfo(stuScores[counter]);
scoreList.add(temp);
}
}
}
}
public static void main(String[] args)
{
ScoreInfo score1 = new ScoreInfo(80);
ScoreInfo score2 = new ScoreInfo(90);
ScoreInfo score3 = new ScoreInfo(100);
scoreList.add(score1);
scoreList.add(score2);
scoreList.add(score3);
int newint1 = 50;
if(scoreList.record(newint1) == true)
System.out.println("You have input a new score.");
else
System.out.println("The score has been incremented.");
}
}
public class ScoreInfo
{
private int score;
private int numStudents;
public ScoreInfo(int aScore)
{
score = aScore;
numStudents = 1;
}
public void increment()
{
numStudents++;
}
public int getScore()
{
return score;
}
public int getFrequency()
{
return numStudents;
}
}
By the way, the question is:
The following Stats class creates and maintains a database of student score information. The scores are sorted in sorted order in the database.
public class Stats
{
private ArrayList<ScoreInfo> scoreList;
//listed in increasing score order; no two ScoreInfo objects contan the same score.
public boolean record(int score)
{
//to be implemented in part(a)
}
public voic recordScores(int[] stuScores)
{
//to be implemented in part(b)
}
(a) Write the Stats methord record that takes a test score and records that score in the database. If the score already exists in the database, the frequency of that score is updated. If the score does not exist in the database, a new ScoreInfo object is created and inserted in the appropriate position so that the database is maintained in increasing score order. The method returns true if a new ScoreInfo object was added to the database, otherwise, it returns false.
So I also need an idea for how to insert the new object so it is in order.
Thanks a lot