Question

USCIS has asked you to write a program that grades the practice test for the naturalization...

USCIS has asked you to write a program that grades the practice test for the naturalization test. The
exam has 10 multiple-choice questions. Here are the correct answers:


1. B
2. D
3. A
4. A
5. C
6. A
7. B
8. A
9. C
10. D


A learner have to answer less than 2 questions wrong to pass the test.
Write a class called CivicExam that has 1) an array field, which holds the correct answers to the
exam, called keys. 2) another array field, named answers, which holds the learners’ answers. The
class should have the following methods and method headers:


1) CivicExam, constructor, that take the learner’s answers.
public CivicExam(char[] ans)


2) passed. Return true if the learner passed the test, or false if the learner failed.
public boolean passed()


3) totalIncorrect. Return the total number of incorrectly answered questions
public int totalIncorrect()


4) questionsMissed. Return an int array containing the question numbers of the questions
that the learner missed.
public int[] questionMissed ()


In this class create a main method, demonstrate the class that asks the user to enter a learner’s
answers (input validation: only accept the letters A,B,C, or D as answers), and then displayed the
results returned from the CivicExam class’s methods

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

OUTPUT

A


B
C
A
F
Please enter correct option
D
S
Please enter correct option

5
false
8

Line break represents missed questions

CivicExam.java

import java.util.Scanner;

public class CivicExam {

    private static char[] learner_answer = new char[10];
    private static char[] keys = new char[]{'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};

    public CivicExam(char[] ans) {
        learner_answer = ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int counter = 0;
        while ( counter != 10 ){
            String h = scanner.nextLine();
            if(h.equals("A") || h.equals("B") || h.equals("C") || h.equals("D") || h.length() == 0){
                if( h.length() == 0 ){
                    learner_answer[counter] = ' ';
                }
                else {
                    char ans = h.charAt(0);
                    learner_answer[counter] = ans;
                }
                counter++;
            }
            else {
                System.out.println("Please enter correct option");
            }
        }
        System.out.println(questionMissed().length);
        System.out.println( passed() );
        System.out.println( totalInCorrect() );
    }

    public static int[] questionMissed() {
        int miss = 0;
        for (char c : learner_answer) {
            if (c == ' ') {
                miss++;
            }
        }
        int[] missed = new int[miss];
        int j = 0;
        for (int i = 0; i < learner_answer.length ; i++) {
            if (learner_answer[i] == ' ') {
                missed[j] = (i + 1);
                j++;
            }
        }
        return missed;
    }

    public static boolean passed() {
        int wrong = answer_check();
        return wrong < 2;
    }

    public static int totalInCorrect() {
        return answer_check();
    }

    private static int answer_check() {
        int wrong = 0;
        for (int i = 0; i < learner_answer.length; i++) {
            if (learner_answer[i] != keys[i]) {
                wrong++;
            }
        }
        return wrong;
    }
}
Add a comment
Know the answer?
Add Answer to:
USCIS has asked you to write a program that grades the practice test for the naturalization...
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
  • License test The Los Angeles driver's license office has asked you to create an application that...

    License test The Los Angeles driver's license office has asked you to create an application that grades the written portion of the drivers license test. The test has 15 multiple-choice questions. Here are the correct answers (You may create your own correct answers if you like) 1. C 2. D     3. B 4. A      5. C 6. D 7. B     8. A     9. C     10. D 11. A 12. C   13. D    14. A     15. B A student must answer 10...

  • Java: The local Driver's License Office has asked you to write a program that grades the...

    Java: The local Driver's License Office has asked you to write a program that grades the written portion of the driver's   license exam. The exam has 20 multiple choice questions.   Here are the correct answers:    1. B  6. A  11.B  16. C    2. D  7. B  12.C  17. C    3. A   8. A  13.D  18. B    4. A  9. C  14.A  19. D    5. C  10. D  15.D  20. A    Your program should store the correct answers in an array. (Store each question's answer in an element of a String array.) The program...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • Look at this partial class definition, and then answer questions a - b below: Class Book...

    Look at this partial class definition, and then answer questions a - b below: Class Book    Private String title    Private String author    Private String publisher    Private Integer copiesSold End Class Write a constructor for this class. The constructor should accept an argument for each of the fields. Write accessor and mutator methods for each field. Look at the following pseudocode class definitions: Class Plant    Public Module message()       Display "I'm a plant."    End Module...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Write a CarInterface to have the following method headers: public int pumpGas(int gas); // no more...

    Write a CarInterface to have the following method headers: public int pumpGas(int gas); // no more than 15 gallons return cost at $3.25 per gallon public void goFast(int fast); // add fast to speed public void goSlow(int slow); subtract slow from speed but if negative set speed to zero public void stop(); //set speed to zero Make sure to write a toString for the class Car. Test the Car class in main by creating an array of size 10. Test...

  • The local driver’s license office has asked you to write a program that grades the written...

    The local driver’s license office has asked you to write a program that grades the written portion of the driver’s license Rules and Signals Test. The driver license test has 25 multiple choice questions. The set of answer keys is: 1.A 6.B 11.C 16.B 2 21.B 2.C 7.C 12.A 17.A 22.C 3.B 8.D 13.B 18.C 23.A 4.B 9.A 14.C 19.A 24.D 5.D 10.B 15.A 20.D 25.B First, the application displays messages to ask for the information of current candidate about...

  • Hello, can someone please fix my code? It runs perfect but I'm having trouble with the...

    Hello, can someone please fix my code? It runs perfect but I'm having trouble with the index for the incorrect answer array; please run some tests to ensure the code runs properly, thank you! Here is the code: ___________________________________________________________________________________________________________________________ import java.util.Scanner; public class DriverTest{    final static int QuestionTotal = 10;    public static void main(String[] args){          // scanner and answer key        Scanner scan = new Scanner(System.in);        final char[] answers = {'A', 'B', 'C',...

  • C++ Question Question 3 [43] The Question: You are required to write a program for an...

    C++ Question Question 3 [43] The Question: You are required to write a program for an Online Tutoring Centre to determine the weekly rate depending on the number of sessions the students attend and then to calculate total revenue for the center. The Online Tutoring Centre charges varying weekly rates depending on the on grade of the student and number of sessions per week the student attends as shown in Table 1 below. Grade 8 9 Session 1 75 80...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

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