Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using java to program Background: For this assignment, you will develop an objec

ID: 3854978 • Letter: U

Question

Using java to program

Background: For this assignment, you will develop an object-oriented program for e.xam questions. The types of questions are shortanswer questions, fillintheblank questions, and multiple choice questions. You will test your design by creating an e.xam, taking the ex.am, and printing the results of you taking the ex.am.

Your assignment is as follows: Create a Question class that constructs objects from a question String. This class resembles shortanswer questions which need to be graded by a human since the answer involves natural language. The class should have an instance method that prompts the user with the question and stores their answer. The class should also have an instance method that prints the question and the user’s answer.

Create a FillInTheBlankQuestion clas. It should construct objects from a question and its fillintheblank answer (both in the form of strings). Create the same methods you created for the Question class, but this time also print the correct answer when their answer was incorrect.

Create a MultipleChoiceQuestion. It should construct objects from a question String, an array of Strings to be used as the multiple choices, and the index of the correct answer in the array. Create all the appropriate methods from the Question class. Modify the method that prompts the user with the question and stores their answer to also display the multiple choices. This class should also be able to print whether or not the user’s answer was correct. If their answer was incorrect, also print the correct answer.

To demonstrate your object oriented design, write a main method in a separate class to create an ex.am using the question classe, take the ex.am, and print the results of taking the ex.am. Put all of your classes in separate .java files for this assignment and adhere to the class design guidelines and code conventions of the textbook.

Explanation / Answer

Given below is the code for the question. Please rate the answer if it helped .Thank you.

Question.java

import java.util.Scanner;

public class Question {

   private String question, answer;

   public Question(String question)

   {

       this.question = question;

   }

  

   public void prompt()

   {

       System.out.println(" Question: " + question);

       System.out.print("Type your answer: ");

   }

  

   public void receiveAnswer(Scanner keybd)

   {

       answer = keybd.nextLine().trim();

   }

}

FillInTheBlankQuestion.java

import java.util.Scanner;

public class FillInTheBlankQuestion {

   private String question, correctAnswer, userAnswer;

   public FillInTheBlankQuestion(String question, String correctAnswer)

   {

       this.question = question;

       this.correctAnswer = correctAnswer;

   }

  

   public void prompt()

   {

       System.out.println(" Question: " + question);

       System.out.print("Type your answer: ");

   }

  

   public void receiveAnswer(Scanner keybd)

   {

       userAnswer = keybd.nextLine().trim();

   }

  

   public void check()

   {

       if(userAnswer.equalsIgnoreCase(correctAnswer))

           System.out.println("You got it correct!");

       else

           System.out.println("Sorry, wrong answer ! The correct answer is " + correctAnswer);

   }

}

MultipleChoiceQuestion.java

import java.util.Scanner;

public class MultipleChoiceQuestion{

   private String question;

   private String[] choices;

   int correctChoice, userChoice;

  

   public MultipleChoiceQuestion(String question, String[] choices, int correctChoice)

   {

       this.question = question;

       this.choices = choices;

       this.correctChoice = correctChoice;

   }

  

   public void prompt()

   {

      

       System.out.println(" Question: " + question);

       System.out.println("Option are:");

       for(int i = 0; i < choices.length; i++)

           System.out.println(" " + (i+1) + ". " + choices[i]);

       System.out.print("What is your choice? ");

   }

  

   public void receiveAnswer(Scanner keybd)

   {

       userChoice = keybd.nextInt();

   }

  

   public void check()

   {

       if(userChoice == correctChoice)

           System.out.println("You got it correct!");

       else

           System.out.println("Sorry, wrong answer ! The correct choice is " + correctChoice);

   }

}

Test.java

import java.util.Scanner;

public class Test {

   Question shortAnsQs[];

   FillInTheBlankQuestion fillInQs[];

   MultipleChoiceQuestion mcQs[];

   //constructor to initialize questions for the exam object

   public Test()

   {

      

       shortAnsQs = new Question[2];

       shortAnsQs[0] = new Question("What is an Array in Java?");

       shortAnsQs[1] = new Question("Give some examples of numeric data types in Java.");

      

       fillInQs = new FillInTheBlankQuestion[2];

       fillInQs[0] = new FillInTheBlankQuestion("In Java, an int data type has _____ bits", "32");

       fillInQs[1] = new FillInTheBlankQuestion("______ primitive type is used to store true or false", "boolean");

      

       String choices[] = { "&", "&=", "|=", "<=" };

       mcQs = new MultipleChoiceQuestion[1];

       mcQs[0] = new MultipleChoiceQuestion("Which of these is not a bitwise operator", choices, 4);

      

   }

  

   public void start()

   {

       Scanner keybd = new Scanner(System.in);

       for(int i = 0; i < shortAnsQs.length; i++)

       {

           shortAnsQs[i].prompt();

           shortAnsQs[i].receiveAnswer(keybd);

          

       }

      

       for(int i = 0; i < fillInQs.length; i++)

       {

           fillInQs[i].prompt();

           fillInQs[i].receiveAnswer(keybd);

           fillInQs[i].check();

       }

      

       for(int i = 0; i < mcQs.length; i++)

       {

           mcQs[i].prompt();

           mcQs[i].receiveAnswer(keybd);

           mcQs[i].check();

       }

   }

  

   public static void main(String[] args) {

       Test test = new Test();

       test.start();

   }

}

output

Question: What is an Array in Java?

Type your answer: An array is used to store multiple obects of same data type

Question: Give some examples of numeric data types in Java.

Type your answer: int, float , double

Question: In Java, an int data type has _____ bits

Type your answer: 16

Sorry, wrong answer ! The correct answer is 32

Question: ______ primitive type is used to store true or false

Type your answer: boolean

You got it correct!

Question: Which of these is not a bitwise operator

Option are:

   1. &

   2. &=

   3. |=

   4. <=

What is your choice? 3

Sorry, wrong answer ! The correct choice is 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote