Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: help with grades

  1. #1
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default help with grades

    ok have this program that reads in questions and answers from a txt file . then it reads the stusents answers and other info from a txt file and calculates their score. the problem i have is that everyone regardless of their score gets an A grade
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
     
    public class JonesT {
    	public static final int MAX_QUESTIONS = 20; 
    	public static final int MAX_STUDENTS  = 40;
     
    	/**i
    	 * @param args
    	 * @throws FileNotFoundException 
    	 */
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		// TODO Auto-generated method stub
    		//int numQuestions = readPositiveInteger("How many questions in test");
    		String [] questions = new String [MAX_QUESTIONS]; 
    		boolean [] answers = new boolean [MAX_QUESTIONS];
    		//int students = readPositiveInteger("How many Students");
    		String [] studentID = new String [MAX_STUDENTS];
    		String [] studentName = new String [MAX_STUDENTS];
    		int [] score = new int [MAX_STUDENTS];
    		char [] grade = new char [MAX_STUDENTS];
    		int numQuestions = getQuiz(questions, answers);
    		int numStudents = takeQuiz(studentID, studentName, score, answers, numQuestions);
    		int max = maximum(score, numStudents);
    		System.out.println("The best score is"+ max);
    		calcGrades(score,  grade, max);
    		displayStudentDetails(studentID,studentName, numStudents,score,grade);
     
     
    	}
     
    	public static void displayStudentDetails(String[] studentID, String [] studentName,int numStudents,int [] score,char [] grade) 
    	{
    		for( int i =0; i<numStudents; i++)
    		{
    			System.out.println(studentID[i] + " " +""+studentName[i]+ score[i] + " " + grade[i]);
    		}
     
    	}
    	public static void calcGrades(int[] score, char[] grade, int max) 
    	{
    		for( int i =0; i<score.length; i++)
    		{
    			if ((score[i] <= max) && (score[i] >= max-2))
    			{
    				grade[i] = 'A';
    			}
    			else if ((score[i] <= max-3) && (score[i] >= max-4))
    			{
    				grade[i] = 'B';
    			}
    			else if ((score[i] <= max-5) && (score[i] >= max-6))
    			{
    				grade[i] = 'C';
    			}
     
    			else if ((score[i] <= max-7) && (score[i] >= max-8))
    			{
    				grade[i] = 'D';
    			}
    			else
    			{
    				grade[i] = 'F';
    			}
    		}
    		}
     
     
    	public static boolean readBoolean(String prompt)
    	{//Start of readBoolean method
     
    		// Create a scanner object for input from the console window
    		Scanner keyboard = new Scanner(System.in);
     
    		// boolean flag which is used to control the 
    		// data validation loop
     
    		boolean goodValue = false; // Assume the worst	
    		do
    		{
    			System.out.print(prompt);  // ask for the value
    			if(!keyboard.hasNextBoolean()) // check if what's in the keyboard buffer is not an integer
    			{
    				System.out.println("You must enter true or false!"); // display an error message
    				keyboard.nextLine(); // consume the bad value entered
    			}
    			else
    				goodValue = true; // value entered is good
    		} while(!goodValue);
    		// at this point we know the value in the
    		// keyboard buffer is numeric so we can go ahead and
    		// return it.
    		return keyboard.nextBoolean();
     
    	}//End of readBoolean method
     
     
    	public static int getQuiz (String[] questions, boolean[]answers) throws FileNotFoundException
    	{//Start of getQuiz method
     
    		/// Create a File object with a validated file name
    		File file = getValidFile("Enter the name of the test file");
    		Scanner inputFile = new Scanner(file);
    		int numQuestions = readFile(inputFile, questions, answers);
    		// Close the file.
    		inputFile.close();
    		return numQuestions;
    	}
     
     
     
    	public static int takeQuiz(String [] studentID, String[] studentName, int[] score, boolean[] answers, int numQuestions) throws FileNotFoundException
    	{ 		Scanner keyboard = new Scanner(System.in);
    //	String studentID;
    	//String name;
     
    	int numStudents = readPositiveInteger("Enter number of students");
    	for (int i = 0; i < numStudents; i++)
    	{
    		File file = getValidFile("Enter the name of the another file for student" + (i+1));
    		Scanner inputFile = new Scanner(file);
    		studentID[i]=inputFile.nextLine();
    		studentName[i]=inputFile.nextLine();
    		for (int j=0;j < numQuestions;j++)
    		{
    			boolean answer=inputFile.nextBoolean();
    			if ( answer == answers[j])
    			{
    				score[i]++;
    			}
    		}
     
    		inputFile.close();		
     
    	}
     
    	return numStudents;
     
    	}
     
     
     
     
     
     
     
    	public static String readString(String prompt)
    	{//Start of readString method
     
    		String name;
    		Scanner keyboard = new Scanner(System.in);
    		do
    		{
    			System.out.print(prompt);
    			name = keyboard.nextLine();
    			if (name.length() <= 1)
    			{
    				System.out.println("Error password must have more than one charactor");	
    			}
     
    		}while (name.length() <=1);
    		return name;
     
    	}//End of readString method
     
     
    	/**
    	 * The readPositiveInteger method reads a positive integer value
    	 * from the console window and will display an appropriate
    	 * error message if a non-positive integer value is entered.
    	 * @param prompt A prompt to request the user to enter a value
    	 * @return The positive integer value entered.
    	 */
    	public static int readPositiveInteger(String prompt)
    	{//Start of readPositiveInteger
     
    		int value;
    		do
    		{
    			value = readInteger(prompt); // ask for and read an integer value
    			if (value < 0) // check if the value entered is less than 0
    				// display an error message
    				System.out.println("Error - you must enter a positive integer value!");
    		} while (value <0);
    		// at this point we know the value entered is positive
    		// so return it
    		return value;
     
    	}//End of readPositiveInteger
     
     
    	public static int readInteger(String prompt)
    	{//Start of readInteger method
     
    		// Create a scanner object for input from the console window
    		Scanner keyboard = new Scanner(System.in);
     
    		// boolean flag which is used to control the 
    		// data validation loop
     
    		boolean numGood = false; // Assume the worst	
    		do
    		{
    			System.out.print(prompt);  // ask for the value
    			if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not an integer
    			{
    				System.out.println("You must enter an integer value!"); // display an error message
    				keyboard.nextLine(); // consume the bad value entered
    			}
    			else
    				numGood = true; // value entered is good
    		} while(!numGood);
    		// at this point we know the value in the
    		// keyboard buffer is numeric so we can go ahead and
    		// return it.
    		return keyboard.nextInt();
     
    	}//End of readInteger method
     
     
     
     
    	/**
    	 * Read and displays the contents of an input file
    	 * @param inputFile The Scanner object that refers to the file that is to be read 
    	 * @param answers 
    	 * @param questions 
    	 * @return 
    	 */
    	public static int readFile(Scanner inputFile, String[] questions, boolean[] answers)
    	{
    		// Read lines from the file until no more are left.
    		int index = 0;
    		while (inputFile.hasNext())
    		{
    			// Read the next line.
    			questions[index] = inputFile.nextLine();
    			System.out.println(questions[index]);
    			answers[index] = inputFile.nextBoolean();
     
    			System.out.println(answers[index]);
    			inputFile.nextLine();
    			index++;
    		}
    		return index;
     
    	}
     
    	/**
    	 * This method creates a file object corresponding to a valid file name 
    	 * @return	A file object corresponding to a valid file name
    	 * @throws FileNotFoundException
    	 */
    	public static File getValidFile(String prompt) 
    	{
     
    		String filename;     // The name of the file
    		File file;			
     
    		// Create a Scanner object for keyboard input.
    		Scanner keyboard = new Scanner(System.in);
     
    		// Get a valid file name.
    		do
    		{
    			System.out.print(prompt);
    			filename = keyboard.nextLine();
    			file = new File(filename);
    			if (!file.exists())
    				System.out.println("The specifed file does not exist - please try again!");
    		}while( !file.exists());
    		return file;
    	}
     
    	/**
    	 * Finds the maximum value in an array
    	 * @param numbers The array whose contents are to be examined
    	 * @param numValues 
    	 * @return The maximum value in the array
    	 */
    	public static int maximum(int[] numbers, int numValues) 
    	{
    		int highest = -2147483647;
    		if (numValues != 0)
    		{
    			highest = numbers[0];
    			for (int i = 1; i < numValues; i++)
    			{
    				if (numbers[i] > highest)
    					highest = numbers[i];
    			}
    		}
     
    		return highest;
     
    	}
     
    }


    THIS AN EXAMPLE OF OUTPUT

    Enter the name of the test fileTwoTimesTables.txt
    2*1=0?
    false
    2*2=4?
    true
    2*3=8?
    false
    2*5=10?
    true
    2*3=6?
    true
    2*9=18?
    true
    2*12=23?
    false
    2*11=4?
    false
    2*10=20?
    true
    2*8=16?
    true
    2*7=19?
    false
    Enter number of students1
    Enter the name of the another file for student1JohnTest17042013.txt
    The best score is8
    R00123455 John O Sullivan8 A


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    problem i have is that everyone regardless of their score gets an A grade
    How does the score get converted to a letter grade? Have you tried debugging that code to see what it is doing and why it gives the letter grade it does? Add some println() statements to print out the values of the variables that control what letter grade is assigned so you will see what the computer see when it executes the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    i am passing the bestscore ie say 8 to calgrades() and it is i think using this to work out the grades

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    What prints out for the values of the variables used to control the assigning of the grade?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    Quote Originally Posted by Norm View Post
    What prints out for the values of the variables used to control the assigning of the grade?
    max is printing whatever the bestscore is

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    if ((score[i] <= max) && (score[i] >= max-2))
    what are the values of score[i] and max?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    scorei is holding the same value as max ie both are taking say 6 if the person get 6 right

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    Does that mean the code is executing correctly? This statement is always true, so the the grade should be 'A'
    if ((score[i] <= max) && (score[i] >= max-2))
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    no because the number of questions in the file is 11 so 6 should give a grade of c i think

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    Then what value should max have for the code in post#8 to work as desired?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    The value should be the number of questions that have been asked/contained in the file but how do i pass that

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    Pass it in the call to the method:
    public static void calcGrades(int[] score, char[] grade, int max)
    Its the last arg passed to the method.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    SOLAS (April 23rd, 2013)

  14. #13
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    Quote Originally Posted by Norm View Post
    Pass it in the call to the method:
    public static void calcGrades(int[] score, char[] grade, int max)
    Its the last arg passed to the method.
    am i passing the file?

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    Sorry, what file? The args for the method are shown in the posted code.
    Which of the args are you asking about?

    If you want to see what is in each of the arrays being passed, use this statement (with the correct array name):
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    Quote Originally Posted by Norm View Post
    Sorry, what file? The args for the method are shown in the posted code.
    Which of the args are you asking about?

    If you want to see what is in each of the arrays being passed, use this statement (with the correct array name):
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    norm your a legend my friend have got it now
    this is new output
    Enter the name of the test fileTwoTimesTables.txt
    2*1=0?
    false
    2*2=4?
    true
    2*3=8?
    false
    2*5=10?
    true
    2*3=6?
    true
    2*9=18?
    true
    2*12=23?
    false
    2*11=4?
    false
    2*10=20?
    true
    2*8=16?
    true
    2*7=19?
    false
    Enter number of students3
    Enter the name of the another file for student1LisaTest17042013.txt
    Enter the name of the another file for student2MaryTest17042013.txt
    Enter the name of the another file for student3JohnTest17042013.txt
    The best score is8
    R00123456 Lisa O Sullivan4 D
    R00123451 Mary O Sullivan6 C
    R00123455 John O Sullivan8 B

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with grades

    The print out needs a space before the number following the name.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help with grades

    Quote Originally Posted by SOLAS View Post
    norm your a legend my friend have got it now
    Please mark your threads as SOLVED with the thread tools directly above the first post in a thread when your problem has been solved.
    I have marked this thread as SOLVED for you.

  19. The Following User Says Thank You to jps For This Useful Post:

    SOLAS (April 23rd, 2013)

  20. #18
    Member
    Join Date
    Apr 2013
    Location
    Ireland
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: help with grades

    @norm yeah will tidy it up now
    @jps thanks very much noted for next time

Similar Threads

  1. Trimming, parsing, verifying grades entered are valid
    By MissMandy in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2013, 07:59 AM
  2. [SOLVED] Get percent of failing grades from an array
    By thom4065 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2013, 04:54 PM
  3. Creating a java program to calculate what grades i need?
    By Stinger25 in forum Java Theory & Questions
    Replies: 2
    Last Post: December 1st, 2012, 02:04 AM
  4. Students/Grades
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 12th, 2011, 10:42 AM
  5. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 12:26 AM