Question

CS 1102 - Unit 2 Programming Assignment First create a new Java class for this assignment....

CS 1102 - Unit 2 Programming Assignment First create a new Java class for this assignment. You should start in Eclipse with your CS1102 project from the previous assignment.

• Select "New"->"Class" from the File menu.

• Enter the Name "Quiz".

• Check the box "public static void main(String[] args)".

• Click "Finish".

Now have your program ask a multiple-choice quiz question. You should create your own original quiz question.

• Create a String called "question" and initialize it to the question you want to ask.

Use the characters "\n" to start a new line.

For example: String question = "What is a quiz?\n";

(Use your own quiz question, not this one.)

Add multiple choices starting with "A", "B", "C", "D", and "E" using the "+=" operator and "\n".

question += "A. a test of knowledge, especially a brief, informal test given to students\n";

Ask the user your question using "JOptionPane.showInputDialog".

Add the following import statement at the top of your code, before "public class Quiz", to give your program access to JOptionPane.

import javax.swing.JOptionPane;

• Ask your question and store the answer in a String. In particular, add the following statement in your main method after setting all the lines of your question String.

String answer = JOptionPane.showInputDialog(question);

Try running your program.

• Select "Run Configurations..." from the Run menu.

• Change the Name from "SuperPower" to "Quiz".

• Change the Main Class from "SuperPower" to "Quiz".

• Click "Apply".

• Click "Run".

A dialog box should appear with your question and a text field for the answer. (In the Virtual Lab, a "File Changed" dialog may pop up. Click "Yes". This will bring the main Eclipse window to the front, hiding the message dialog from your program. Move the main Eclipse window or minimize it, and you will see the message dialog behind it.)

Now modify your program to check the answer.

• Change the answer to upper case so lower-case input works.

answer = answer.toUpperCase();

• Use an if statement to check the answer and display a message if it is correct. For example, if the correct answer is "A":

if (answer.equals("A")) {

JOptionPane.showMessageDialog(null,"Correct!");

}

Run your program and enter various answers. Make sure you get the correct response. Modify your program so that it responds differently to different input.

• It should display one message if the answer is correct, like "Correct!"

• It should display a different message if the answer is valid ("A", "B", "C", "D", or "E") but incorrect, like "Incorrect. Please try again."

• It should display a third message if the answer is invalid (anything other than single characters "A", "B", "C", "D", and "E"), like "Invalid answer. Please enter A, B, C, D, or E."

Run your program and test each of these possibilities.

Modify your program so that it asks the question and responds with a dialog until the user enters the correct answer.

• Move the statements that record the answer and display a response into a while loop.

• Use "break" or "return" to exit the loop when the answer is correct.

Now test your final program.

Upload the following for your Programming Assignment submission.

• Quiz.java

• Screen shot showing the input dialog with the quiz question

• Screen shot showing the message dialog for the correct answer

• Screen shot showing the message dialog for a valid but incorrect answer

• Screen shot showing the message dialog for an invalid answer

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the Code in Java below for the above question:

package HomeworkLib;

import javax.swing.JOptionPane;

public class Quiz {

   public static void main(String[] args) {
       String question;
       question = "What was the name of Google in late 90s?\n";
       question+="A. Googol\n";
       question+="B. Gigablast\n";
       question+="C. Backrub\n";
       question+="D. Google\n";
       while(true) {
           String answer = JOptionPane.showInputDialog(question);
           answer = answer.toUpperCase();
          
           if(answer.equals("C")) {
               JOptionPane.showMessageDialog(null,"Correct!");
               break;
           }
           else if(answer.equals("A") || answer.equals("B") || answer.equals("D")) {
               JOptionPane.showMessageDialog(null,"Inorrect.Please try Again");
           }
           else{
               JOptionPane.showMessageDialog(null,"Invalid Answer");
           }
       }
   }
}

Refer to the Screenshot attached below to better understand the code and indentation:

Output:

Entered answer as C.

  

If answered other than option C.
  

If answered invalid option
  

If my answer helps you then please Upvote,it means alot
for further queries comment below.

Thank You

Add a comment
Know the answer?
Add Answer to:
CS 1102 - Unit 2 Programming Assignment First create a new Java class for this assignment....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT