Question

In: Computer Science

WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...

WRITE THIS JAVA CODE IN PSEUDOCODE!!

import java.util.Scanner;

public class License {

    public static void main(String[] args) {

        char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'};
        char userAnswers[] = new char[correctAnswers.length];


        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < userAnswers.length; i++) {

            String answer = "";
            System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1);
            do {
                answer = scanner.nextLine();
                if (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D"))) {
                    System.out.print("You didnt enter it A, B, C or D. Enter again: ");
                }

            } while (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D")));

            userAnswers[i] = answer.charAt(0);
        }

        int correctlyAnswered = getCorrectAnswerCount(correctAnswers, userAnswers);
        System.out.printf("\nYou answered %d questions correctly.\n", correctlyAnswered);
        System.out.printf("You answered %d questions incorrectly.\n\n", userAnswers.length - correctlyAnswered);

        if (correctlyAnswered != userAnswers.length) {
            displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers);
        }
        if(correctlyAnswered>=15) System.out.println("You have cleared the exam. Congratulations.");
        else System.out.println("You have failed the exam. Sorry!");

    }

    private static void displayIncorrectlyAnsweredQuestions(char[] correctAnswers, char[] userAnswers) {
        System.out.print("Questions Wrongly Answered: ");
        for (int i = 0; i < correctAnswers.length; i++) {
            if (correctAnswers[i] != userAnswers[i]) System.out.print((i + 1) + " ");
        }
        System.out.println();
    }

    private static int getCorrectAnswerCount(char[] correctAnswers, char[] userAnswers) {
        int count = 0;
        for (int i = 0; i < correctAnswers.length; i++) {
            if (correctAnswers[i] == userAnswers[i]) count += 1;
        }
        return count;
    }
}

Solutions

Expert Solution

PSEUDOCODE WITHOUT COMMENTS

START
   Initialize char array correctAnswers = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}
  
   Declare char array userAnswers with length of correctAnswers
  
   FOR i From 0 to length of userAnswers -1
       Declare answer as String
      
       DO
           Read answer
          
           IF answer not equals A, B, C, or D
               Display Error Message
           END IF
       WHILE (answer not equals A, B, C, or D)
      
       Set userAnswers[i] = answer
   END FOR
      
   Declare correctlyAnswered as int
  
   correctlyAnswered = getCorrectAnswerCount(correctAnswers, userAnswers)
  
   Display correctlyAnswered
  
   Display length of userAnswers - correctlyAnswered
  
   IF correctlyAnswered not equals length of userAnswers
       displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers)
   END IF
  
   IF correctlyAnswered greater than or equals 15
       Display Passed
   ELSE
       Display Failed
   END IF
  
   FUNCTION displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers)
       FOR i From 0 to length of correctAnswers - 1
           IF correctAnswers[i] not equals userAnswers[i]
               Display i + 1
           END IF
       END FOR
   END displayIncorrectlyAnsweredQuestions
  
   FUNCTION getCorrectAnswerCount(correctAnswers, userAnswers)
       Declare count as int and initialize as 0
      
       FOR i From 0 to length of correctAnswers - 1
           IF correctAnswers[i] equals userAnswers[i]
               Increment count
           END IF
       END FOR
      
       Return count
   END getCorrectAnswerCount
END

SCREEN SHOT

PSEUDOCODE WITH COMMENTS

START
   //declaring and initializing array correctAnswers
   Initialize char array correctAnswers = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}
  
   //declaring array userAnswers
   Declare char array userAnswers with length of correctAnswers
  
   //loop to read user answers
   FOR i From 0 to length of userAnswers -1
       //variable to read answer
       Declare answer as String
      
       //loop to read answer
       DO
           //read answer
           Read answer
          
           //if invalid answer
           IF answer not equals A, B, C, or D
               Display Error Message
           END IF
       //repeat until valid answer entered
       WHILE (answer not equals A, B, C, or D)
      
       //storing answer in array userAnswers
       Set userAnswers[i] = answer
   END FOR
  
   //variable to store number of correct answers
   Declare correctlyAnswered as int
  
   //calling getCorrectAnswerCount
   correctlyAnswered = getCorrectAnswerCount(correctAnswers, userAnswers)
  
   //displaying number of correct answers
   Display correctlyAnswered
  
   //displaying number of wrong answers
   Display length of userAnswers - correctlyAnswered
  
   //if there is wrong answers
   IF correctlyAnswered not equals length of userAnswers
       //calling displayIncorrectlyAnsweredQuestions
       displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers)
   END IF
  
   //if correct answers greater than or equals 15
   IF correctlyAnswered greater than or equals 15
       Display Passed
   //if correct answers less than 15
   ELSE
       Display Failed
   END IF
  
   //function displayIncorrectlyAnsweredQuestions
   FUNCTION displayIncorrectlyAnsweredQuestions(correctAnswers, userAnswers)
       //loop to display wrong answers
       FOR i From 0 to length of correctAnswers - 1
           //if correct answer not equals user answer
           IF correctAnswers[i] not equals userAnswers[i]
               //display question number of wrong answer
               Display i + 1
           END IF
       END FOR
   END displayIncorrectlyAnsweredQuestions
  
   //function getCorrectAnswerCount
   FUNCTION getCorrectAnswerCount(correctAnswers, userAnswers)
       //variable to store number of correct answers
       Declare count as int and initialize as 0
      
       //loop to count number of correct answers
       FOR i From 0 to length of correctAnswers - 1
           //if correct answer equals user answer
           IF correctAnswers[i] equals userAnswers[i]
               //increments count
               Increment count
           END IF
       END FOR
      
       //returns count
       Return count
   END getCorrectAnswerCount
END

SCREEN SHOT


Related Solutions

Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT