Question

In: Computer Science

Java programming language Write a Java application that simulates a test. The test contains at least...

Java programming language

Write a Java application that simulates a test. The test contains at least five questions. Each question should be a multiple-choice question with 4 options.
Design a QuestionBank class. Use programmer-defined methods to implement your solution. For example:
- create a method to simulate the questions – simulateQuestion
- create a method to check the answer – checkAnswer
- create a method to display a random message for the user – generateMessage
- create a method to interact with the user - inputAnswer
Use a loop to show all the questions.
For each question:
- If the user finds the right answer, display a random congratulatory message (“Excellent!”, ”Good!”, ”Keep up the good work!”, or “Nice work!”).
- If the user responds incorrectly, display an appropriate message and the correct answer (“No. Please try again”, “Wrong. Try once more”, “Don't give up!”, “No. Keep trying..”).
- Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer.
- Use a switch statement to issue the responses, as in the following code:
switch ( randomObject.nextInt( 4 ) )
{
case 0:
return( "Very good!" );
……
}
At the end of the test display the number of correct and incorrect answers, and the percentage of the correct answers.
Your main class will simply create a QuestionBank object ( in the driver class – QuestionBankTest.java) and start the test by calling inputAnswer method.

Solutions

Expert Solution

//----- QuestionBank.java -----------
import java.util.Random;
import java.util.Scanner;
class QuestionBank
{
   //array of questions.
   private final String[] questions = {
       "What is the Capital of England?",
       "What is the Sum of first 10 numbers?",
       "Which continent is called as Dark Continent?",
       "What is the highest peak in the world?",
       "What is the result of 2 + 3 * 10 / 2 = ?"
       };
   //answers for above questions
   private final String[] answers={
       "London",
       "55",
       "Africa",
       "Mount Everest",
       "17"
       };
   //options for each of the above questions.
   private final String[][] options = {
       {"London","New York","Rome","Washington"},
       {"10","55","54","52"},
       {"America","Europe","Asia","Africa"},
       {"Mount Cameroon","Mount Everest","Chomo Lonzo","Vinson Massif"},
       {"16","50","17","25"}
   };
  
   //random object
   Random randomObject;
   //scanner
   Scanner in;
  
   QuestionBank()
   {
       randomObject = new Random();
       in = new Scanner(System.in);
      
   }
   //function that simulates the questions
   //by printing the question and its options for given question index.
   int simulateQuestion(int qInd)
   {
      
       System.out.println("\n"+(qInd+1)+". "+questions[qInd]);
       int opLen = options[qInd].length;
       for(int i = 0 ; i < opLen ; i++)
       {
           System.out.println("\n"+(char)('a'+i)+". "+options[qInd][i]);
       }
       return qInd;
   }
   //function that takes the option user entered.(a,b,c,d,)
   //and gets the option answer (a -> london) selected and
   //checks the option answer with the answers[qInd]
   boolean checkAnswer(String answer,int qInd)
   {
       if(answer.length() != 1)
       {
           return false;
       }
      
       int opLen = options[qInd].length;
       String choosenAns="";
       boolean found = false;
       String i_thOp;
       for(int i =0; i<opLen;i++)
       {
           i_thOp = ""+(char)('a'+i);
          
           if(answer.equals(i_thOp))
           {
               choosenAns = options[qInd][i];
               found = true;
               break;
           }
       }
      
       if(!found)
       {
           return false;
       }
       else
       {
          
           return answers[qInd].equals(choosenAns);
       }
   }
   //function that simulates the process.
   //by printing the question , options and checking the answer
   //iterating thorugh all questions.
   void inputAnswer()
   {
       int posScore = 0;
       int negScore = 0;
       for(int i = 0;i<questions.length;i++)
       {
           int qInd = simulateQuestion(i);
           System.out.print("\nEnter your option: ");
           String ans = in.nextLine();
           System.out.println("");
           if(checkAnswer(ans,qInd))
           {
               System.out.println(generateMessage(true));
               posScore++;
           }
           else
           {
               System.out.println(generateMessage(false));
               negScore++;
           }
       }
       System.out.println("\nTotal Right Answer: "+posScore);
       System.out.println("Total Wrong Answer: "+negScore);
       float percent = (float)posScore /(posScore + negScore);
       System.out.println(String.format("Percentage of Right Answers: %.2f",percent));
      
   }
   //function that returns the message
   //for given type.
   //if boolean positive set to true then it returns the random
   //positive messages stored in posMessages array.
   //or else it returns the negative messages in negMessages array.
   String generateMessage(boolean positive)
   {
       //if want to print to positive messsages.
       if(positive)
       {
           //positive messages.
           switch(randomObject.nextInt(4))
           {
               case 0:
                   return "Excellent!";
                  
               case 1:
                   return "Good!";
                  
               case 2:
                   return "Keep up the good work!";
                  
               case 3:
                   return "Nice work!";
                      
           }
       }
       else
       {
           //negative messages
          
           switch(randomObject.nextInt(4))
           {
               case 0:
                   return "No. Please try again";
                  
               case 1:
                   return "Wrong. Try once more" ;
                  
               case 2:
                   return "Don't give up!" ;
                  
               case 3:
                   return "No. Keep trying..";
                      
           }
       }
       return"";
   }
  
  
  
}
//---------- QuestionBankTest.java ------------
class QuestionBankTest
{
   public static void main(String[] args)
   {
       QuestionBank driver = new QuestionBank();
       driver.inputAnswer();
   }
}


Related Solutions

Programming language= In Java and Oracle Sql You are required to develop a simple HR application...
Programming language= In Java and Oracle Sql You are required to develop a simple HR application for a small accounting firm that wishes to keep track of all the employees at the firm; storing details about their salary, phone numbers and Date of Birth. The firm has many departments and there are 5 to 20 employees in each department. The department information includes department name, description and total number of employees in that department. The company also provides vehicles for...
Write a java console application,. It simulates the vending machine and ask two questions. When you...
Write a java console application,. It simulates the vending machine and ask two questions. When you run your code, it will do following: Computer output: What item you want? User input: Soda If user input is Soda Computer output: How many cans do you want? User input:            3 Computer output: Please pay $3.00. END The vending machine has 3 items for sale: Soda the price is $1.50/can. Computer should ask “How many cans do you want?” Chips, the price is $1.20/bag....
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and...
Programming language is Java: Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Also please discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming...
In the programming language java: Write a class encapsulating the concept of a telephone number, assuming a telephone number has only a single attribute: aString representing the telephone number. Include a constructor, the accessor and mutator, and methods 'toString' and 'equals'. Also include methods returning the AREA CODE (the first three digits/characters of the phone number; if there are fewer than three characters in the phone number of if the first three characters are not digits, then this method should...
Java programming Write the max-heapify code and test it and then write the program to find...
Java programming Write the max-heapify code and test it and then write the program to find the three largest values of the array without sorting the entire array to get values.
Java Programming Language Scenario: write a program that will prompt a user for 10 legendary people/characters....
Java Programming Language Scenario: write a program that will prompt a user for 10 legendary people/characters. After the entries are complete the program will display all 10 characters information. Store these characters in an array. Requirements: Create a user-defined class with the fields below: First Last Nickname Role Origin Create a main-source that will use the user-defined class and do the prompting. Create get/set methods in your user-defined class and methods in the main-source. Use an array to store the...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT