In: Computer Science
I NEED IT PRINT IN THIS FORM JAVA
How is a Java source file executed?
1. It is compiled and runs natively
2. It is interpreted but not compiled
3. It is compiled into byte code and executed by the Java Virtual Machine
4. It is magic
Please enter your answer
1
Sorry that answer is not correct. The correct answer is 3
Correct output:
How is a Java source file executed?
1. It is compiled and runs natively
2. It is interpreted but not compiled
3. It is compiled into byte code and executed by the Java Virtual Machine
4. It is magic
Please enter your answer
3
Congratulations!!!! That is the right answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class HWData
{
private ArrayList<String> question;
private ArrayList<String> answer1;
private ArrayList<String> answer2;
private ArrayList<String> answer3;
private ArrayList<String> answer4;
private File inFile;
private ArrayList<Integer> correct;
/** Method: HWData
* No argument constructor
*/
public HWData()
{
try
{
inFile = new File("data.txt");
Scanner input = new Scanner(inFile);
question = new ArrayList<String>();
answer1 = new ArrayList<String>();
answer2 = new ArrayList<String>();
answer3 = new ArrayList<String>();
answer4 = new ArrayList<String>();
correct = new ArrayList<Integer>();
while (input.hasNextLine())
{
question.add(input.nextLine());
answer1.add(input.nextLine());
answer2.add(input.nextLine());
answer3.add(input.nextLine());
answer4.add(input.nextLine());
String number = input.nextLine();
try
{
correct.add(Integer.parseInt(number));
}
catch (NumberFormatException nfe)
{
System.out.println("An invalid number for
an answer was encountered.");
}
}
}
catch(FileNotFoundException fnf)
{
System.out.println("Sorry the file was not
found.");
}
}
/** Method: getQuestion
*
* @param int number of question
* @return String containing the question
*/
public String getQuestion(int number)
{
return question.get(number);
}
/** Method: getAnswer1
*
* @param int number of answer1
* @return String containing answer1
*/
public String getAnswer1(int number)
{
return answer1.get(number);
}
/** Method: getAnswer2
*
* @param int number of answer2
* @return String containing answer2
*/
public String getAnswer2(int number)
{
return answer2.get(number);
}
/** Method: getAnswer3
*
* @param int number of answer3
* @return String containing answer3
*/
public String getAnswer3(int number)
{
return answer3.get(number);
}
/** Method: getAnswer4
*
* @param int number of answer4
* @return String containing answer4
*/
public String getAnswer4(int number)
{
return answer4.get(number);
}
/** Method: getCorrect
*
* @param int number of question
* @return int containing the correct response
*/
public int getCorrect(int number)
{
return correct.get(number);
}
/** Method: getNumberOfQuestions
*
* @param none
* @return int number of questions in the file
*/
public int getNumberOfQuestions()
{
return question.size();
}
}
//DRIVER METHOD (ADD BELOW CODE MAIN METHOD)
public static void main(String[] args)
{
HWData hwData = new HWData();
for(int
i=0;i<hwData.getNumberOfQuestions();i++)
{
System.out.println(hwData.getQuestion(i));
System.out.println(hwData.getAnswer1(i));
System.out.println(hwData.getAnswer2(i));
System.out.println(hwData.getAnswer3(i));
System.out.println(hwData.getAnswer4(i));
System.out.println("Please enter
your answer");
Scanner sc = new
Scanner(System.in);
int correct = sc.nextInt();
if(hwData.getCorrect(i)==correct)
{
System.out.println("Congratulations!!!! That is the right
answer");
}
else
{
System.out.println("Sorry that answer is not correct. The correct
answer is "+hwData.getCorrect(i));
}
}
}
//total code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class HWData
{
private ArrayList<String> question;
private ArrayList<String> answer1;
private ArrayList<String> answer2;
private ArrayList<String> answer3;
private ArrayList<String> answer4;
private File inFile;
private ArrayList<Integer> correct;
/** Method: HWData
* No argument constructor
*/
public HWData()
{
try
{
inFile = new File("data.txt");
Scanner input = new Scanner(inFile);
question = new ArrayList<String>();
answer1 = new ArrayList<String>();
answer2 = new ArrayList<String>();
answer3 = new ArrayList<String>();
answer4 = new ArrayList<String>();
correct = new ArrayList<Integer>();
while (input.hasNextLine())
{
question.add(input.nextLine());
answer1.add(input.nextLine());
answer2.add(input.nextLine());
answer3.add(input.nextLine());
answer4.add(input.nextLine());
String number = input.nextLine();
try
{
correct.add(Integer.parseInt(number));
}
catch (NumberFormatException nfe)
{
System.out.println("An invalid number for an answer was
encountered.");
}
}
}
catch(FileNotFoundException fnf)
{
System.out.println("Sorry the file was not found.");
}
}
/** Method: getQuestion
*
* @param int number of question
* @return String containing the question
*/
public String getQuestion(int number)
{
return question.get(number);
}
/** Method: getAnswer1
*
* @param int number of answer1
* @return String containing answer1
*/
public String getAnswer1(int number)
{
return answer1.get(number);
}
/** Method: getAnswer2
*
* @param int number of answer2
* @return String containing answer2
*/
public String getAnswer2(int number)
{
return answer2.get(number);
}
/** Method: getAnswer3
*
* @param int number of answer3
* @return String containing answer3
*/
public String getAnswer3(int number)
{
return answer3.get(number);
}
/** Method: getAnswer4
*
* @param int number of answer4
* @return String containing answer4
*/
public String getAnswer4(int number)
{
return answer4.get(number);
}
/** Method: getCorrect
*
* @param int number of question
* @return int containing the correct response
*/
public int getCorrect(int number)
{
return correct.get(number);
}
/** Method: getNumberOfQuestions
*
* @param none
* @return int number of questions in the file
*/
public int getNumberOfQuestions()
{
return question.size();
}
public static void main(String[] args)
{
HWData hwData = new HWData();
for(int
i=0;i<hwData.getNumberOfQuestions();i++)
{
System.out.println(hwData.getQuestion(i));
System.out.println(hwData.getAnswer1(i));
System.out.println(hwData.getAnswer2(i));
System.out.println(hwData.getAnswer3(i));
System.out.println(hwData.getAnswer4(i));
System.out.println("Please enter
your answer");
Scanner sc = new
Scanner(System.in);
int correct = sc.nextInt();
if(hwData.getCorrect(i)==correct)
{
System.out.println("Congratulations!!!! That is the right
answer");
}
else
{
System.out.println("Sorry that answer is not correct. The correct
answer is "+hwData.getCorrect(i));
}
}
}
}