Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

First of all make a new Java class for this assignment. You should start in Eclipse with your CS1102 project from the previous assignment. Select

First of all make 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 make your own original quiz question.

Make a String called "question" and initialize it to the question you want to ask. Use the characters " " to start a new line. For example: String question = "What is a quiz? "; (Use your own quiz question, not this one.)

Add multiple choices starting with "A", "B", "C", "D", and "E" using the "+=" operator and " ". question += "A. a test of knowledge, especially a brief, informal test given to students "; 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions