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