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

Write a program that grades arithmetic quizzes as follows: (Use the below startu

ID: 3690630 • Letter: W

Question

Write a program that grades arithmetic quizzes as follows: (Use the below startup code for Quizzes.java and provide code as indicated)

1. Ask the user how many questions are in the quiz.

2. Use a for loop to load the array. Ask the user to enter the key (that is, the correct answers). There should be one answer for each question in the quiz, and each answer should be an integer. They can be entered on a single line, e.g., 34, 7, 13, 100, 8 might be the key for a 5-question quiz. You will need to store the key in an array called “key”.

3. Ask the user to enter the student’s answers for the quiz to be graded. There needs to be one answer for each question. Note that each answer can simply be compared to the key as it is entered. If the answer is correct, add 1 to a correct answer counter. Do this in a separate for loop from the loop used to load the array.

4. When the user has entered all of the answers to be graded, print the number correct and the percent correct.

5. When this works, nest your for loop and output statements in a do….while loop so that the user can grade any number of quizzes with a single key. After the results have been printed for each quiz, ask “Grade another quiz? (y/n).”

//******************************************************************
// Quizzes.java
// Grades multiple choice quizzes.
//******************************************************************

import java.util.Scanner;
import java.text.NumberFormat;


public class Quizzes
{
    //----------------------------------------------
    // Read in the number of questions followed by
    // the key, then read in each student's answers
    // and calculate the number and percent correct.
    //----------------------------------------------

public static void main(String[] args)
    {
   int numQuestions;
   int numCorrect;
   String anotherQuiz;
   int answer;
   NumberFormat percent = NumberFormat.getPercentInstance();

   Scanner scan = new Scanner (System.in);

   System.out.println ("Quiz Grading");
   System.out.println ();
   System.out.print ("Enter the number of questions on the quiz: ");
   numQuestions = scan.nextInt();

   //CREATE THE ARRAY FOR THE KEY

   System.out.print ("Enter the answer key: ");


   //LOAD THE ARRAY WITH INPUT FROM THE USER

   //OUTER LOOP TO ALLOW THE USER TO ENTER GRADES FOR ANY NUMBER OF

   //QUIZZES

      System.out.print("Enter the student answers: ");

    //LOOP TO GET ANSWERS FROM THE USER AND COUNT THE NUMBER OF

    // CORRECT ANSWERS

    //DISPLAY THE NUMBER OF CORRECT ANSWERS AND PERCENT

    //ASK USER IF THEY WISH TO GRADE ANOTHER QUIZ

Write a program that prompts the user for an integer, then asks the user to enter that many values (test with 5 integer values). Store these values (10, 20, 30, 40, 50), in an array and print the array.

Then reverse the array elements so that the first element becomes the last element, the second element becomes the second to last element, and so on, with the old last element now first. Do not just reverse the order in which they are printed; actually change the way they are stored in the array.

Do not create a second array; just rearrange the elements within the array you have. (Hint: Swap elements that need to change places. You can find a “swap” algorithm on page 678 in your text) When the elements have been reversed, print the array again. Use the below startup code for ReverseArray.java

//*******************************************************************
// ReverseArray.java
//
// Reads in an array and reverses it.
//*******************************************************************

import java.util.Scanner;
public class ReverseArray
{
    //--------------------------------------------------
    // Read in an array then reverse the order in which
    // the array elements are stored.
    //--------------------------------------------------


    public static void main(String[] args)
    {
   int numElements;
   Scanner scan = new Scanner (System.in);

   System.out.print ("Enter the number of elements in the array: ");
   numElements = scan.nextInt();

//Creates the array

   int[] a = new int[numElements];


//Loads the array

   System.out.println ("Enter the array elements (integers)...");
   for (int i = 0; i < numElements; i++)
   {
       System.out.print ("Enter element " + (i+1) + ": ");
       a[i] = scan.nextInt();
   }

//displays the array in its original order

   System.out.println ();
   System.out.println ("The array elements before reversing:");
   for (int i = 0; i < numElements; i++)
       System.out.print (a[i] + " ");
   System.out.println();

   // FOR LOOP TO REVERSE THE ELEMENTS

   // INSIDE THE FOR LOOP SWAP ELEMENT i WITH ELEMENT numElements-1-i



   // Print the array
   System.out.println (" The array after reversing: ");
   for (int i = 0; i < numElements; i++)
       System.out.print (a[i] + " ");
   System.out.println();
    }
}

Explanation / Answer

//******************************************************************
// Quizzes.java
// Grades multiple choice quizzes.
//******************************************************************

import java.util.Scanner;
import java.text.NumberFormat;

public class Quizzes {
   // ----------------------------------------------
   // Read in the number of questions followed by
   // the key, then read in each student's answers
   // and calculate the number and percent correct.
   // ----------------------------------------------

   public static void main(String[] args) {
       int numQuestions;
       int numCorrect;
       String anotherQuiz;
       int answer;
       NumberFormat percent = NumberFormat.getPercentInstance();

       Scanner scan = new Scanner(System.in);

       System.out.println("Quiz Grading");
       System.out.println();
       do {
           System.out.print("Enter the number of questions on the quiz: ");
           numQuestions = scan.nextInt();

           // CREATE THE ARRAY FOR THE KEY
           int[] key = new int[numQuestions];

           // LOAD THE ARRAY WITH INPUT FROM THE USER

           System.out.print("Enter the answer key: ");

           for (int i = 0; i < numQuestions; i++) {
               System.out
                       .print("Enter the key for Question " + (i + 1) + ": ");
               key[i] = scan.nextInt();
           }
           // OUTER LOOP TO ALLOW THE USER TO ENTER GRADES FOR ANY NUMBER OF
           // QUIZZES

           System.out.print("Enter the student answers: ");
           // LOOP TO GET ANSWERS FROM THE USER AND COUNT THE NUMBER OF
           // CORRECT ANSWERS
           numCorrect = 0;
           for (int i = 0; i < numQuestions; i++) {
               System.out.print("Enter the Answer for Question " + (i + 1)
                       + ": ");
               answer = scan.nextInt();
               if (answer == key[i]) {
                   numCorrect++;

               }

           }

           // DISPLAY THE NUMBER OF CORRECT ANSWERS AND PERCENT

           System.out.println("Number of Correct Answers :" + numCorrect);
           System.out.printf("Percent correct :%.2f ",
                   ((float) numCorrect / (float) numQuestions) * 100.0);

           // ASK USER IF THEY WISH TO GRADE ANOTHER QUIZ
           System.out.print("Grade another quiz? (y/n):");
           anotherQuiz = scan.next();
           if (anotherQuiz.equals("n"))
               break;
       } while (true);

   }

}
OUTPUT:
Quiz Grading

Enter the number of questions on the quiz: 5
Enter the answer key:
Enter the key for Question 1: 6
Enter the key for Question 2: 9
Enter the key for Question 3: 8
Enter the key for Question 4: 7
Enter the key for Question 5: 6
Enter the student answers:
Enter the Answer for Question 1: 6
Enter the Answer for Question 2: 9
Enter the Answer for Question 3: 8
Enter the Answer for Question 4: 5
Enter the Answer for Question 5: 6
Number of Correct Answers :4
Percent correct :80.00
Grade another quiz? (y/n):y
Enter the number of questions on the quiz: 5
Enter the answer key:
Enter the key for Question 1: 1
Enter the key for Question 2: 2
Enter the key for Question 3: 3
Enter the key for Question 4: 4
Enter the key for Question 5: 5
Enter the student answers:
Enter the Answer for Question 1: 1
Enter the Answer for Question 2: 2
Enter the Answer for Question 3: 3
Enter the Answer for Question 4: 4
Enter the Answer for Question 5: 9
Number of Correct Answers :4
Percent correct :80.00
Grade another quiz? (y/n):n

//*******************************************************************
// ReverseArray.java
//
// Reads in an array and reverses it.
//*******************************************************************
import java.util.Scanner;

public class ReverseArray {
   // --------------------------------------------------
   // Read in an array then reverse the order in which
   // the array elements are stored.
   // --------------------------------------------------

   public static void main(String[] args) {
       int numElements;
       Scanner scan = new Scanner(System.in);

       System.out.print("Enter the number of elements in the array: ");
       numElements = scan.nextInt();

       // Creates the array
       int[] a = new int[numElements];

       // Loads the array
       System.out.println("Enter the array elements (integers)...");
       for (int i = 0; i < numElements; i++) {
           System.out.print("Enter element " + (i + 1) + ": ");
           a[i] = scan.nextInt();
       }

       // displays the array in its original order
       System.out.println();
       System.out.println("The array elements before reversing:");
       for (int i = 0; i < numElements; i++)
           System.out.print(a[i] + " ");
       System.out.println();

       // FOR LOOP TO REVERSE THE ELEMENTS
       // INSIDE THE FOR LOOP SWAP ELEMENT i WITH ELEMENT numElements-1-i
       for (int i = 0; i < numElements / 2; i++) {
           int temp = a[i];
           a[i] = a[numElements - 1 - i];
           a[numElements - 1 - i] = temp;
       }

       // Print the array
       System.out.println(" The array after reversing: ");
       for (int i = 0; i < numElements; i++)
           System.out.print(a[i] + " ");
       System.out.println();
   }
}

OUTPUT:
Test 1:
Enter the number of elements in the array: 6
Enter the array elements (integers)...
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Enter element 6: 6

The array elements before reversing:
1 2 3 4 5 6

The array after reversing:
6 5 4 3 2 1

Test 2:
Enter the number of elements in the array: 5
Enter the array elements (integers)...
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5

The array elements before reversing:
1 2 3 4 5

The array after reversing:
5 4 3 2 1

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