Monday, October 17, 2011

Java code for quiz

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;


class Foo{
public String subject;
public String module;
public String question_type;
public String ref_in_book;

public String question;
public String correct_ans;

public String first_option;
public String second_option;
public String third_option;
public String fourth_option;


public Foo(String subject, String module, String question_type, String
ref_in_book, String question, String correct_ans){
this.subject = subject;
this.module = module;
this.question_type = question_type;
this.ref_in_book = ref_in_book;

this.question=question;
this.correct_ans=correct_ans;
this.first_option = first_option;
this.second_option = second_option;
this.third_option = third_option;
this.fourth_option = fourth_option;
}
}

public class ReadSimpleFile {
public static void main(String[] args) {

BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

File file = new File("c:\\downloads\\ap_questions.txt");
if(!file.exists())
{
System.out.println("File does not exist");
System.exit(0);
}
else
{
try{

BufferedReader i = new BufferedReader(new
InputStreamReader(new FileInputStream(file),"UTF8"));

FileWriter fstream = new FileWriter(
"c:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs
\\out.html");
BufferedWriter out = new BufferedWriter
(fstream);

String str1 ;
String temp[];
int line_count=0;

//Map<String, String> map = new
HashMap<String, String>();

ArrayList<Foo> foos = new ArrayList<Foo>();

while ( (str1 = i.readLine()) != null ) {
String delimiter = "\\|";
temp = str1.split(delimiter);

temp[0].trim(); // subject =
maths physics chemistry

temp[1].trim(); // Module name =
samantara shredi , gunothara_shredi
temp[2].trim(); // Question_type =
single_answer multiple choice match_following
temp[3].trim(); // Reference in text
book ? Ex 1.3 5A, 1.2 2ii

temp[4].trim(); // Actual question
temp[5].trim(); // Correct_answer;
/*temp[6].trim(); // First optional
answer
temp[7].trim(); // Second optional
answer
temp[8].trim(); // Third guess answer
temp[9].trim(); // Fourth guess
answer
*/
foos.add(new Foo ( temp[0], temp[1],
temp[2],temp[3], temp[4], temp[5] ));
}

// You will collect this as an input
argument.
String subject_search = "maths";

// You will collect this as an input argument
from user.
String question_type = "single_answer";

out.write("<html>" );
out.write("<head>" );
out.write("<meta http-equiv=\"content-type\"
content=\"text/html;charset=UTF-8\" /> ");
out.write("<h3> ಸಮಾಂತರ ಶ್ರೇಡಿ </h3>\n");

out.write("</head>" );

for ( int s=0; s< foos.size(); s++ ) {
int count = s + 1;
out.write(count + ": "+foos.get(s).
question +"<br>");

if ( ( count % 10 ) == 0 ) { out.write(
"<p style=\"page-break-before: always\"> "); }
}
out.write( "<p style=\"page-break-before:
always\"> ");

out.write("Correct Answers: ");

for ( int s=0; s< foos.size(); s++ ) {
int count = s + 1;
out.write("<b>" + count + ") </b>"
+foos.get(s).correct_ans + " &nbsp; &nbsp; &nbsp; ");
}

out.write("</html>" );

out.close();
}

catch(UnsupportedEncodingException ue){
System.out.println("Not supported : ");
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
}
}