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....
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
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.
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details...
*OBJECT ORIENTED PROGRAMMING* *JAVA PROGRAMMING* Create a program that simulates a race between several vehicles. Details don't matter code must just have the following: Design and implement an inheritance hierarchy that includes Vehicle as an abstract superclass and several subclasses. Include a document containing a UML diagram describing your inheritance hierarchy. Include at least one interface that contains at least one method that implementing classes must implement. Include functionality to write the results of the race to a file; this...
in C programming language, write and test a function that writes and returns through an output...
in C programming language, write and test a function that writes and returns through an output parameter the longest common suffix of two words. (e.g. The longest common suffix of "destination" and "procrastination" is "stination", of "globally" and "internally" is "ally, and of "glove" and "dove" is the empty string)
The programming language is Java. Write the code you would use to fill the variable cubed...
The programming language is Java. Write the code you would use to fill the variable cubed with the value stored in x to the third power.
Java Programming : Email username generator Write an application that asks the user to enter first...
Java Programming : Email username generator Write an application that asks the user to enter first name and last name. Generate the username from the first five letters of the last name, followed by the first two letters of the first name. Use the .toLowerCase() method to insure all strings are lower case. String aString = “Abcd” aString.toLowerCase(); aString = abcd Use aString.substring(start position, end position + 1) aString.substring(0, 3) yields the first 3 letters of a string If the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT