
INSTRUCTIONS: Explain each important segment of the code.
import javax.swing.JOptionPane;
public class Quiz
{
/**
* isValid accapts a string and checks to see if it is A, B, or C
* and returns a boolean value of true or false if it matches
*/
private static boolean isValid(String a)
{
a = a.toLowerCase();
if(a.equals("a") || a.equals("b") || a.equals("c") || a.equals("d"))
{
return true;
}
else
{
JOptionPane.showMessageDialog(null, "Please pick A, B, or C");
return false;
}
}
/**
* askQuestion recieves an array containing the question and all the
* answers. Returning the user input as a String
*/
private static String askQuestion(String[] q)
{
String fmtQuestion = ""; // String to hold the formatted question
String answer = "";
for(int i = 0; i < q.length; i++)
fmtQuestion += q[i] + "\n";
do
{
answer = JOptionPane.showInputDialog(null, fmtQuestion);
if(answer == null)
{
int quit = JOptionPane.showConfirmDialog(null, "Would you like to quit?", "Quit", JOptionPane.YES_NO_OPTION);
if(quit == 0)
return "ABORT";
else
continue;
}
} while (answer == null || !(isValid(answer)));
return answer;
}
/**
* isCorrect recieves two values a (correct answer) and r (incorrect
* answer). Returning a boolean value if the answer is correct or not
*/
private static boolean isCorrect(String a, String r) // a = correct answer, r = user response
{
r = r.toUpperCase();
if(a.equals(r))
{
JOptionPane.showMessageDialog(null, "Correct!");
return true;
}
else
{
JOptionPane.showMessageDialog(null, "The correct answer is: \n" + a);
return false;
}
}
/**
* showGrade accepts two values c (total correct answers) and i (total
* incorrect answers). A message dialog is displayed stating the total
* coreect, incorrect, and the grade for the test
*/
public static void showGrade(int c, int i)
{
int numberQuestions = c + i;
String fmtGrade = "";
int pointsPerQuestion = 100 / numberQuestions;
int grade = c * pointsPerQuestion;
fmtGrade += "You answered " + c + " correctly and " + i + " incorrectly";
fmtGrade += "\nYour grade is: " + grade + "%";
JOptionPane.showMessageDialog(null, fmtGrade);
}
public static void main(String[] args)
{
int i = 0; // iterator to be used later for the askQuestion loop
int correct = 0; // number of correct answers
int incorrect = 0; // number of incorrect answers
String response = ""; // holds the answer the user supplied temporarily
/*
* I know multi-dimensional arrays are not till next chapter but
* I already knew about them and felt that it really was the best
* option for this assignment
*/
JOptionPane.showMessageDialog(null, "Are you a true Potterhead?");
String[][] question = new String[10][5]; // Array to store questions
String[] correctAnswer = new String[10]; // Array to store correct answers
question[0][0] = "Who is not in Gryffindor?";
question[0][1] = "A) Harry Potter";
question[0][2] = "B) Ron Weasley";
question[0][3] = "C) Draco Malfoy";
question[0][4] = "D) Ginny Weasley";
correctAnswer[0] = "C";
question[1][0] = "Which magical creatures did Gilderoy Lockhart set loose in class?";
question[1][1] = "A) Bowtruckles";
question[1][2] = "B) Cornish Pixies";
question[1][3] = "C) Occamy";
question[1][4] = "D) Kneazle";
correctAnswer[1] = "B";
question[2][0] = "What killed Moaning Myrtle?";
question[2][1] = "A) Nagini";
question[2][2] = "B) Basilisk";
question[2][3] = "C) Occamy";
question[2][4] = "D) Boa Constrictor";
correctAnswer[2] = "B";
question[3][0] = "What Harry did to get him temporarily expelled from Hogwarts?";
question[3][1] = "A) He inflates Aunt Marge";
question[3][2] = "B) He turns Vernon into a ferret";
question[3][3] = "C) He disapparates";
question[3][4] = "D) He chases away demetors";
correctAnswer[3] = "D";
question[4][0] = "What is Slytherin's house ghost called?";
question[4][1] = "A) The Bloody Baron";
question[4][2] = "B) The Fat Friar";
question[4][3] = "C) Nearly-Headless Nick";
question[4][4] = "D) The Grey Lady";
correctAnswer[4] = "A";
(continuation of the codes in the photo below)
(continuation of the codes in the photo below)
![question[50] = "What is the new password for entering into the Gryffindor common
room?";
question(5)1] = "A) Pig Snout":
question[52] = "B) Caput Draconis":
question[53) = "C) Wattlebird":
question(5)4) = "D) Abstinence":
carrectAnswer(5) = "C":
question[60] = "Who sent Harry his Firebolt?":
question[6(1] = "A) Professor McGonagall":
question[62) - "B) Sirius Black":
questione3) = "C) Remus Lupin";
question[614] = "D) Professor Dumbledare";
correciAnswerf6] = B:
question70] = Which of these is Harry's boggart?":
question(71] = "A) Giant Spider";
question(72) = "B) Professor Snape"
question[73] = "C) The moon";
question(614) - "D) A dementor":
carrectAnswer[7] = "D";
question[B0] = "Which department does Mr. Arthur Weasley work in?";
question[B1] = "A) The Misuse of Muggle Atifacts Office":
question[B2] = "B) Department of Magical Accidents and Catastrophes":
question[B3] = "C) Department of International Magical Cooperation";
question[814] = "D) Minister of Magic and Support Staf";
carrectAnswer[8] = "A";
question[90] = "Who does Hermione go with to Slughom'sChristmas party?":
question[91] = "A) Neville Longbottom";
question[92) = "B) Carmac Mcdaggen":
question[93] = "C) Viktor Krum":
question[94] = "D) Dean Thomas":
carrectAnswer(9) = "B":
N loop through the question array asking each one
op
response - askOuestion(question):
if(response.equals("ABORT")
return;
iflisCorrect(correctAnswerfj, response)
carrect += 1;
else
incorrect += 1;
} while(i < questionlength);
showGrade(correct, incorrect):](https://content.bartleby.com/qna-images/question/536c9f3c-7f76-4204-ae42-d14690a1c64b/45fdc51d-2b6c-4fcc-8aa2-9bcbf007937c/ovgqgqf_thumbnail.png)

Step by stepSolved in 2 steps with 1 images

- This is the code that needs to be corrected: // This application gets a user's name and displays a greeting import java.util.Scanner; public class DebugThree3 { public static void main(String args[]) { String name; name = getName(); displayGreeting(name); } public static String getName(name) { String name; Scanner input = new Scanner(System.in); System.out.print("Enter name "); name = input.nexlLine(); return name; } public static displayGreeting(String name) { System.outprintln("Hello, " + name + "!"); } }arrow_forwardCode that need debugging fix with no errors // Gets a String from user // Converts the String to lowercase, and // displays the String's length // as well as a count of letters import java.util.*; public class DebugSeven4 { public static void main(String[] args) { Scanner kb = new Scanner(System.in); String aString ; int numLetters = 0; int stringLength; System.out.println("Enter a String. Include"); System.out.println("some uppercase letters, lowercase"); System.out.print("letters, and numbers >> "); aString = kb.nextLine(); stringLength = aString.length(); System.out.print("In all lowercase, the String is: "); for(int i = 0; i <= stringLength; i++) { char ch = Character.toLowerCase(aStringcharAt(i)); System.out.print(ch); if(!Character.isLetter(ch)) numLetters++; } System.out.println(); System.out.println ("The number of CHARACTERS…arrow_forwardJavaarrow_forward
- Java - Text Message Expanderarrow_forwardPlease explain Common String Mistake in the code below/question in Bold: public class StringMistakeDemo{public static void main(String[] args) {String a = "Roux";String b = "Roux";String c = new String("Roux"); /** Number 1* Consider the output from the this block of code. * Which line causes the "false"?* Why are the last two lines different?*/System.out.println();System.out.println("Roux" == "Roux"); //comparing two stringsSystem.out.println(a == b); //comparing two string variablesSystem.out.println(a == c); //comparing two other string variablesSystem.out.println(a.equals(c));//using the string class method "equals"/** Number 2* "identityHashCode" provides a numeric signature that includes the * memory location for the variable. * What does the following output tell you about how Java stores…arrow_forwardSimple try-catch Program This lab is a simple program that demonstrates how try-catch works. You will notice the output when you enter incorrect input (for example, enter a string or double instead of an integer). Type up the code, execute and submit the results ONLY. Do at least 2 valid inputs and 1 invalid. NOTE: The program stops executing after it encounters an error! CODE: import java.util.Scanner; public class TryCatchExampleSimple { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num = 0; System.out.println("Even number tester.\n"); System.out.println("Enter your name: "); String name input.nextLine(); } } while (true) { try { System.out.println("Enter an integer : "); num= input.nextInt (); } int mod = num%2; if (mod == 0) " System.out.println(name + else " System.out.println(name + num = 0; } catch (Exception e) { break; > System.out.println("ERROR: The number you entered is illegal!"); System.out.println("Exception error: "+e.toString());…arrow_forward
- Java String objects are immutable. Select one: O True Falsearrow_forward// Displays five random numbers between // (and including) user-specified values import java.util.Scanner; public class DebugSix4 { publicstaticvoidmain(String[] args) { int high, low, count =0; finalint NUM =5; Scanner input =newScanner(System.in); System.out.print("This application displays " + NUM + " random numbers"+ "\nbetween the low and high values you enter"+ "\nEnter low value now... "); low = input.nextInt() System.out.print("Enter high value... "); high = inputnextInt(); while(low < high) { System.out.println("The number you entered for a high number, " + high + ", is not more than " + low); System.out.print("Enter a number higher than " + low + "... "); high = input.nextInt(); while(count <NUM) { double result = Math.random(); int answer = (int) (result *10+ low); if(answer <= higher) { System.out.print(answer + " "); ++count; } } System.out.println("End of Application"); } }arrow_forward16arrow_forward
- Examine this code and determine what happens when it is run: 1 public class Test { int x; 2 3 public Test(String t){ System.out.println("Test"); } public static void main(String[] args) { Test test = new Test ("boo"); System.out.println(test.x); } 4 5 6 7 8 10 } The program has a compile error because Test does not have a default constructor. The program has a compile error because System.out.println method cannot be invoked from the constructor. ) The program runs successfully and prints out: Test O The program has a compile error because you cannot create an object from the class that defines the object.arrow_forwardQ: What's a better way to write this? (C#) public string SomeMethod(string OriginalZip) { string zip = "Zip Not Available"; zip = GetZip(zip, OriginalZip); return zip; } private static string GetZip(string zip, string OriginalZip) { int zipLength = (OriginalZip.HasValue) ? OriginalZip.Value.ToString().Length: 0; int NumOf Leading Zeroes = (zipLength > O && zipLength < 5) ? 5 - zipLength: 0; if (OriginalZip.HasValue) { i++) } zip = OriginalZip.Value.ToString(); for (int i = 0; i < NumOf Leading Zeroes; { } zip = "0" + zip; } return zip;arrow_forwardPractice / Frequency of Characters Write a method that returns the frequency of each characters of a given String parameter. If the given String is null, then return null If the given String is empty return an empty map Example input: output: empty map explanation: input is empty Example input: null output: null explanation: input is null. Since problem output is focused on the frequency we can comfortably use Map data structure. Because we can use characters as key and the occurrences of them as value. Example input: responsible output: {r=1, e=2, s=2, p=1, o=1, n=1, i=1, b=1, l=1} explanation: characters are keys and oc values ences arearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





