Results 1 to 18 of 18
- 01-30-2013, 05:03 PM #1
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
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.
- 01-30-2013, 05:07 PM #2
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 ProgrammingHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-30-2013, 05:15 PM #3
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
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:
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.Java 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; } }
- 01-30-2013, 05:40 PM #4
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?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-30-2013, 06:04 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-30-2013, 07:32 PM #6
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
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
cannot be resolved to a type.Java Code:public static double median(double[] m) {
The median(examGrades.............portion is the part that's highlighted with that notice.
- 01-30-2013, 07:33 PM #7
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
Re: Find the median of a list of grades in a .txt file
Sorry,
median(double.....
- 01-30-2013, 07:37 PM #8
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.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-30-2013, 08:02 PM #9
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
Re: Find the median of a list of grades in a .txt file
Java 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); } }
- 01-30-2013, 08:04 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Find the median of a list of grades in a .txt file
look here: Selection algorithm - Wikipedia, the free encyclopedia
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-30-2013, 08:35 PM #11
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.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-30-2013, 09:47 PM #12
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
Re: Find the median of a list of grades in a .txt file
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)?Put your method at the same level as your main() method, not inside it.
- 01-31-2013, 02:17 PM #13
Re: Find the median of a list of grades in a .txt file
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-31-2013, 02:56 PM #14
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
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."
- 01-31-2013, 03:15 PM #15
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?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 01-31-2013, 07:13 PM #16
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
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.
- 01-31-2013, 08:32 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-31-2013, 08:51 PM #18
Master's Student
- Join Date
- Jan 2013
- Location
- Florida
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Passing Grades Help!
By JojoDiaz in forum New To JavaReplies: 16Last Post: 10-14-2011, 03:48 PM -
Project with Arrays and Grades
By rochla16 in forum New To JavaReplies: 13Last Post: 03-29-2011, 12:40 AM -
Array help counting # of grades
By speaknspell in forum New To JavaReplies: 4Last Post: 04-16-2009, 10:09 PM -
How to find OS Process List in JAVA ?
By abstrusevaibhav in forum New To JavaReplies: 0Last Post: 01-29-2009, 10:08 AM -
How to Find IE Browser SSL certificates list
By dmkreddy_k in forum Advanced JavaReplies: 0Last Post: 10-29-2008, 06:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks