Results 1 to 18 of 18
  1. #1
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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:

    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;
        }
    }
    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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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
    Java 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.

  7. #7
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default Re: Find the median of a list of grades in a .txt file

    Sorry,
    median(double.....

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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!

  9. #9
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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);
    					
    				
    			}
    
    }

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: Find the median of a list of grades in a .txt file

    Quote Originally Posted by Victor View Post
    Thanks, but i have google'd and google'd to no positive results.
    look here: Selection algorithm - Wikipedia, the free encyclopedia

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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!

  12. #12
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default Re: Find the median of a list of grades in a .txt file

    When you write,
    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)?

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Find the median of a list of grades in a .txt file

    Quote Originally Posted by Victor View Post
    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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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."

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default 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!

  16. #16
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    Victor is offline Master's Student
    Join Date
    Jan 2013
    Location
    Florida
    Posts
    13
    Rep Power
    0

    Default 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.

Similar Threads

  1. Passing Grades Help!
    By JojoDiaz in forum New To Java
    Replies: 16
    Last Post: 10-14-2011, 03:48 PM
  2. Project with Arrays and Grades
    By rochla16 in forum New To Java
    Replies: 13
    Last Post: 03-29-2011, 12:40 AM
  3. Array help counting # of grades
    By speaknspell in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 10:09 PM
  4. How to find OS Process List in JAVA ?
    By abstrusevaibhav in forum New To Java
    Replies: 0
    Last Post: 01-29-2009, 10:08 AM
  5. How to Find IE Browser SSL certificates list
    By dmkreddy_k in forum Advanced Java
    Replies: 0
    Last Post: 10-29-2008, 06:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •