Question

In: Computer Science

I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1....

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

Solutions

Expert Solution

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


Related Solutions

I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
"You will need to compile each Java source file (provided below) separately, in its own file...
"You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory." My class is asking me to do this and i am not understanding how to do it. I use JGRASP for all of my coding and i can't find a tutorial on how to do it. I just need a step by step tutorial...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
Using java, I need to make a program that reverses a file. The program will read...
Using java, I need to make a program that reverses a file. The program will read the text file character by character, push the characters into a stack, then pop the characters into a new text file (with each character in revers order) EX: Hello World                       eyB       Bye            becomes    dlroW olleH I have the Stack and the Linked List part of this taken care of, I'm just having trouble connecting the stack and the text file together and...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
I need to file a tax return on TaxAct Software for this S Corporation on form...
I need to file a tax return on TaxAct Software for this S Corporation on form 1120S and the K-1, can anyone help with this? I need all the other forms needed to complete the tax return. In other words, complete tax returns with attachments. It comes from the Pearson's Federal Taxation 2020 Comprehensive, Timothy J. Rupert; Kenneth E. Anderson book. C:11-57 Comparison of Operating Activities. RST business entity reported the following items during the current year: Dividends from a...
I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT