Find the median of a list of grades in a .txt file
Hello folks. I have a program here, that reads a .txt file, that houses an array of grades. I have already coded the MAX, MIN, AVG, amount of grades pertaining to grade letter (i.e A's, B's, C's, D's, and F's apiece) and number of total grades within the array. For those issues it runs perfectly, as it extracts the exam.txt file i have the grades stored in. However, i now have to find the median of the list of grades in the given .txt file. I am having trouble trying to code the median. Here is what i have thus far in it's entirety:
import java.util.*;
import java.io.*;
import java.text.*;
public class Grades
{
//This method produces the heading for the program or you may say that it introduces the program.
public void introduction()
{
System.out.println("***Let's see those grades Einstein!!***");
System.out.println();
}
public static void main(String[] args) throws Exception
{
double sum = 0;
int gradeCount = 0;
int MAX = 0;
int MIN = 0;
double AVG = 0;
int gCount = 0;
int gCount1 = 0;
int gCount2 = 0;
int gCount3 = 0;
int gCount4 = 0;
int [] examGrades;
char letterGrade;
String[] currGrades;
Scanner input = new Scanner(System.in);
System.out.println("***Welcome to the Exam Statistics Program!!***");
System.out.println();
System.out.println("Please enter the name of your data file: ");
String txtFile = input.next();
FileReader fr = new FileReader(Grades.class.getProtectionDomain().getC odeSource().getLocation().getPath() + txtFile);
Scanner input1 = new Scanner(fr);
currGrades = input1.nextLine().split(" ");
input1.close();
examGrades = new int[currGrades.length];
for(int g = 0; g< examGrades.length; g++)
{
examGrades[g] = Integer.parseInt(currGrades[g]);
if(g == 0)
{
MIN = examGrades[g];
MAX = examGrades[g];//I added MAX here too cause it should be here, but everything will be greater than 0.
}
if (examGrades[g]<=0)
{
break;
}
gradeCount++;
sum += examGrades[g];
AVG = sum/gradeCount;
if (examGrades[g] >= 0)
{
if (examGrades[g] < MIN)
{
MIN = examGrades[g];
}
else if (examGrades[g] > MAX)
{
MAX = examGrades[g];
}
if ((examGrades[g] >= 90) && (examGrades[g] <= 100))
{
letterGrade = 'A';
gCount++;
}
if ((examGrades[g] >= 80) && (examGrades[g] <= 89))
{
letterGrade = 'B';
gCount1++;
}
if ((examGrades[g] >= 70) && (examGrades[g] <= 79))
{
letterGrade = 'C';
gCount2++;
}
if ((examGrades[g] >= 60) && (examGrades[g] <= 69))
{
letterGrade = 'D';
gCount3++;
}
if ((examGrades[g] <= 59) && (examGrades[g] >= 0))
{
letterGrade = 'F';
gCount4++;
}
System.out.println(examGrades[g] + " ");
}
}
DecimalFormat df = new DecimalFormat("#.##");
System.out.println("Maximun score:" + MAX);
System.out.println("Minimum score:" + MIN);
System.out.println("Average score:" + df.format(AVG));
System.out.println("Number of scores by letter grade, is as follows:");
System.out.println("A:" + gCount);
System.out.println("B:" + gCount1);
System.out.println("C:" + gCount2);
System.out.println("D:" + gCount3);
System.out.println("F:" + gCount4);
// System.out.println("Median of grades entered is:");
System.out.println("The total number of grades in your text file is:" + gradeCount);
}
}
May i please get some assistance of finding the median of a list of numbers in the array? The array is called examGrades if it helps. Thanks.
Re: Find the median of a list of grades in a .txt file
What trouble does it give you? Do you get an Exception? Some strange behavior? Something else?
Also, please use code tags when posting code: BB Code List - Java Programming Forum - Learn Java Programming
Re: Find the median of a list of grades in a .txt file
Ok, i'll remember the code tags from now on, thanks. As for the program, it runs efficiently, with the exception of the median. I had the following, within the for loop:
Code:
public static double median(double[] m) {
int middle = m.length/2;
if (m.length%2 == 1) {
return m[middle];
} else {
return (m[middle-1] + m[middle]) / 2.0;
}
}
But that wouldn't even compile, so i commented it out. I have never done median code before, so i am starting from scratch on how to do it.
Re: Find the median of a list of grades in a .txt file
Why doesn't it compile? Did you try fixing the compilation error?
Re: Find the median of a list of grades in a .txt file
Finding the median in a naive way takes O(n*log(n)) steps (sorting); google for 'median' and pages show up that explain how to do it in O(n) steps.
kind regards,
Jos
Re: Find the median of a list of grades in a .txt file
Thanks, but i have google'd and google'd to no positive results. I have implemented the code above but it states that Code:
public static double median(double[] m) {
cannot be resolved to a type.
The median(examGrades.............portion is the part that's highlighted with that notice.
Re: Find the median of a list of grades in a .txt file
Sorry,
median(double.....
Re: Find the median of a list of grades in a .txt file
If you want help, you're going to have to provide an SSCCE that demonstrates exactly what you're doing. We can't really tell what's going on with small snippets of your code.
Re: Find the median of a list of grades in a .txt file
Code:
import java.util.*;
import java.io.*;
import java.text.*;
public class Grades
{
//This method produces the heading for the program or you may say that it introduces the program.
public void introduction()
{
System.out.println("***Let's see those grades Einstein!!***");
System.out.println();
}
public static void main(String[] args) throws Exception
{
double sum = 0;
int gradeCount = 0;
int MAX = 0;
int MIN = 0;
double AVG = 0;
int gCount = 0;
int gCount1 = 0;
int gCount2 = 0;
int gCount3 = 0;
int gCount4 = 0;
int [] examGrades;
char letterGrade;
String[] currGrades;
Scanner input = new Scanner(System.in);
System.out.println("***Welcome to the Exam Statistics Program!!***");
System.out.println();
System.out.println("Please enter the name of your data file: ");
String txtFile = input.next();
FileReader fr = new FileReader(Grades.class.getProtectionDomain().getCodeSource().getLocation().getPath() + txtFile);
Scanner input1 = new Scanner(fr);
currGrades = input1.nextLine().split(" ");
input1.close();
examGrades = new int[currGrades.length];
for(int g = 0; g< examGrades.length; g++)
{
examGrades[g] = Integer.parseInt(currGrades[g]);
if(g == 0)
{
MIN = examGrades[g];
MAX = examGrades[g];//I added MAX here too cause it should be here, but everything will be greater than 0.
}
if (examGrades[g]<=0)
{
break;
}
gradeCount++;
sum += examGrades[g];
AVG = sum/gradeCount;
if (examGrades[g] >= 0)
{
if (examGrades[g] < MIN)
{
MIN = examGrades[g];
}
else if (examGrades[g] > MAX)
{
MAX = examGrades[g];
}
if ((examGrades[g] >= 90) && (examGrades[g] <= 100))
{
letterGrade = 'A';
gCount++;
}
if ((examGrades[g] >= 80) && (examGrades[g] <= 89))
{
letterGrade = 'B';
gCount1++;
}
if ((examGrades[g] >= 70) && (examGrades[g] <= 79))
{
letterGrade = 'C';
gCount2++;
}
if ((examGrades[g] >= 60) && (examGrades[g] <= 69))
{
letterGrade = 'D';
gCount3++;
}
if ((examGrades[g] <= 59) && (examGrades[g] >= 0))
{
letterGrade = 'F';
gCount4++;
}
System.out.println(examGrades[g] + " ");
}
/* public static double median(examGrades[] m)
{
int middle = m.length/2;
if (m.length%2 == 1)
{
return m[middle];
}
else
{
return (m[middle-1] + m[middle]) / 2.0;
}
}*/
}
DecimalFormat df = new DecimalFormat("#.#");
System.out.println("Maximun score:" + MAX);
System.out.println("Minimum score:" + MIN);
System.out.println("Average score:" + df.format(AVG));
System.out.println("Number of scores by letter grade, is as follows:");
System.out.println("A:" + gCount);
System.out.println("B:" + gCount1);
System.out.println("C:" + gCount2);
System.out.println("D:" + gCount3);
System.out.println("F:" + gCount4);
// System.out.println("Median of grades entered is:" + median);
System.out.println("The total number of grades in your text file is:" + gradeCount);
}
}
Re: Find the median of a list of grades in a .txt file
Quote:
Originally Posted by
Victor
Thanks, but i have google'd and google'd to no positive results.
look here: Selection algorithm - Wikipedia, the free encyclopedia
kind regards,
Jos
Re: Find the median of a list of grades in a .txt file
I see. It looks like you're trying to define a method inside of your main() method, which you can't do. Put your method at the same level as your main() method, not inside it.
Re: Find the median of a list of grades in a .txt file
When you write, Quote:
Put your method at the same level as your main() method, not inside it.
......can you tell me exactly what you mean? I am wondering if i should declare the median code within the Grades class section, but outside the main(method)?
Re: Find the median of a list of grades in a .txt file
Quote:
Originally Posted by
Victor
When you write, ......can you tell me exactly what you mean? I am wondering if i should declare the median code within the Grades class section, but outside the main(method)?
What happened when you tried that?
Re: Find the median of a list of grades in a .txt file
When i place the median code in the class before the main() method, it tells me that examGrades, "cannot be resolved to a type."
Re: Find the median of a list of grades in a .txt file
If you want help, you're going to have to post an SSCCE with updated code.
Did you try googling your error? How do you define an array? How do you declare method parameters? (those are good questions for the basic tutorials) How are you defining the array parameter? What exactly is examGrades?
Re: Find the median of a list of grades in a .txt file
Thanks for your help guys, no luck, but oh well. Very much appreciated nonetheless.
Re: Find the median of a list of grades in a .txt file
So this thread went from an efficient way to calculate the mean of a sequence of numbers to just compilation errors?
kind regards,
Jos
Re: Find the median of a list of grades in a .txt file
I still can't calculate the median properly. I have experimented with where to put the block of code for it, but it isn't working out for me.