CS 1102 - Unit 3 Programming Assignment In this assignment, you will modify your Quiz class from the previous assignment. You will create a method that asks a quiz question and a method that checks the answer, and you will use those methods to ask multiple questions. Your program will then report how many questions the user got correct. This program will be the basis for future Programming Assignments in this course. These instructions assume that you are using the Eclipse IDE on your own computer or you may use a different Java environment as long as you can provide Java code and screen shots for your assignment submission. First open your previous Programming Assignment in Ecli
import javax.swing.JOptionPane;
public class Main {
public static void main(String[] args) {
String q1;
q1 = "What in the name of the current course?\n";
q1+="A. BC 1100 Programming 9\n";
q1+="B. CC 1000 Programming 3\n";
q1+="C. CS 1102 Programming 1\n";
q1+="D. KFC 0000 Programming 7\n";
q1+="E. Bouth A AND C 10\n";
check(q1,"C");
String q2 = "?\n";
q2 = "F.?\n";
q2 = "G.?\n";
q2 = "H.?\n";
q2 = "A.?\n";
q2 = "K.\n";
check(q1,"K");
String q3 = "?\n";
q3 = "O.?\n";
q3 = "E.?\n";
q3 = "N.?\n";
q3 = "V?\n";
q3 = "S.?\n";
check(q1,"E");
int nCor=0;
int nQuizs=0;
JOptionPane.showMessageDialog(null,nCor + " correct out of " +nQuizs+ " questions");
}
public static String ask(String question) {
while(true) {
String ans = JOptionPane.showInputDialog(question);
ans = ans.toUpperCase();
if(!(ans.equals("A") || ans.equals("B") || ans.equals("C") || ans.equals("D"))){
JOptionPane.showMessageDialog(null,"Invalid Answer");
continue;
}
return ans;
}
}
static void check(String question, String correctAnswer) {
int nQuizs=0;
nQuizs++;
String ans = ask(question);
if(ans.equals(correctAnswer)) {
JOptionPane.showMessageDialog(null,"Correct!");
int nCor = 0;
nCor++;
}
else {
JOptionPane.showMessageDialog(null,"Inorrect.Please try Again");
}
}
}
Comments
Leave a comment