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

Write a program (PersonalityTest.java) to score users responses to the classic M

ID: 3673866 • Letter: W

Question

Write a program (PersonalityTest.java) to score users responses to the classic Myers-Briggs personality test. Assume that the test has 70 questions that determine a persons personality in four dimensions. Each question has two answer choices that well call the A and B answers. Questions are organized into 10 groups of seven questions, with the following repeating pattern in each group:

·         The first question in each group (questions 1, 8, 15, ... 64) tells whether the person isExtrovert or Introvert.

·         The next two questions (questions 2 and 3, 9 and 10, 16 and 17, ..., 65 and 66) test whether the person is guided by his/her Sense or iNtuition.

·         The next two questions (questions 4 and 5, 11 and 12, 18 and 19, ..., 67 and 68) test whether the person focuses on Thinking or Feeling.

·         The final two questions in each group (questions 6 and 7, 13 and 14, 20 and 21, ... 69 and 70) test whether the person prefers to Judge or be guided by Perception.

In other words, if we consider extrovert/introvert (E/I) to be dimension 1, sensing/intuition (S/N) to be dimension 2, thinking/feeling (T/F) to be dimension 3, and judging/perception (J/P) to be dimension 4, the map of questions to their respective dimensions would look like this:

12233441223344122334412233441223344122334412233441223344122334412 23344 BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABA AAAAA

The following is a sample input file of names and responses:

Betty Boop

BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA

Snoopy

AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB

Bugs Bunny

aabaabbabbbaaaabaaaabaaaaababbbaabaaaabaabbbbabaaaabaabaaaaaabbaaaaabb

Daffy Duck

BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA

The frumious bandersnatch

-BBaBAA-BBbBBABBBBA-BaBBBBBbbBBABBBBBBABB-BBBaBBABBBBBBB-BABBBBBBBBBBB

Minnie Mouse

BABA-AABABBBAABAABA-ABABAAAB-ABAAAAAA-AAAABAAABAAABAAAAAB-ABBAAAAAAAAA

Luke Skywalker

bbbaaabbbbaaba-BAAAABBABBAAABBAABAAB-AAAAABBBABAABABA-ABBBABBABAA-AAAA

Han Solo

BA-ABABBB-bbbaababaaaabbaaabbaaabbabABBAAABABBAAABABAAAABBABAAABBABAAB

Princess Leia

BABBAAABBBBAAABBA-AAAABABBABBABBAAABAABAAABBBA-AABAABAAAABAAAAABABBBAA

The input file contains a name followed by his/her responses to the test. The answer to a question is case insensitive, and can be A, B, or where a dash represents that the person skipped that question. If less than 50% of a persons responses are B for a given personality dimension, the persons type for that dimension should be the first of its two choices. If the person answers B more than 50%, the persons type for that dimension is the second choice. If the person has the same number of As and Bs for a dimension, give them an X (as with Han Solo).

Ask the user to enter the input filename and check if the file exists. If yes, process the file. Otherwise, ask the user again to enter the input filename until a correct name is entered. You may assume that the input file contains no errors and that nobody has skipped all questions for a dimension.

Given the above input file, the resulting output would be:

Betty Boop:

answers: [1A-9B, 17A-3B, 18A-2B, 18A-2B]

percent B: [90, 15, 10, 10]

type: ISTJ

Bugs Bunny:

answers: [8A-2B, 11A-9B, 17A-3B, 9A-11B]

percent B: [20, 45, 15, 55]

type: ESTP

Luke Skywalker:

answers: [1A-8B, 7A-11B, 14A-5B, 15A-5B]

percent B: [89, 61, 26, 25]

type: INTJ

Han Solo:

answers: [2A-8B, 9A-9B, 11A-9B, 15A-5B]

percent B: [80, 50, 45, 25]

type: IXTJ

....

Thank You very much

Explanation / Answer

Personality.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Personality {

   public static final String [] TYPE1 = new String [] {"I","N","F","P"};
   //less than 50 percent B
   public static final String [] TYPE2 = new String [] {"E","S","T","J"};

   // The main method to process the data from the personality tests
   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in); // do not make any other Scanners connected to System.in
       Scanner fileScanner = getFileScanner(keyboard);
       processFile(fileScanner);
       // CS312 students - your code and methods calls go here
       fileScanner.close();
       keyboard.close();
   }

   // Method to choose a file.
   // Asks user for name of file.
   // If file not found create a Scanner hooked up to a dummy set of data
   // Example use:
   public static Scanner getFileScanner(Scanner keyboard){
       Scanner result = null;
       try {
           System.out.print("Enter the name of the file with the personality data: ");
           String fileName = keyboard.nextLine().trim();
           System.out.println();
           result = new Scanner(new File(fileName));
       }
       catch(FileNotFoundException e) {
           System.out.println("Problem creating Scanner: " + e);
           System.out.println("Creating Scanner hooked up to default data " + e);
           String defaultData = "1 DEFAULT DATA "
                   + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                   + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
           result = new Scanner(defaultData);
       }
       return result;
   }

   /**
   * this method processes the whole file and uses arrays and method calls to complete the whole program. then calls method
   * to print answers
   * @param fileScanner- this is the file inputted by user sent in to this method
   */
   public static void processFile(Scanner fileScanner)
   {  
       int [] percentageB = new int [] {0,0,0,0};
       int records = fileScanner.nextInt();
       fileScanner.nextLine();
       for(int i = 0; i < records; i++)
       {
           String name = getName(fileScanner);
           char [] answers = lineToArray(fileScanner);
           percentageB[0] = extraAndIntroverts(answers);
           percentageB[1] = getPercent(1,answers);
           percentageB[2] =getPercent(3,answers);
           percentageB[3] = getPercent(5,answers);
           String answer = computeAnswer(percentageB);
           printAnswers(percentageB,name,answer);
       }
   }

   /**
   * this method prints all the answers including percentages, names and final personality type
   * @param percentageB-array with percentages of all 4 question types
   * @param name-name string of person
   * @param answer-final answer string of personality type
   */
   public static void printAnswers(int [] percentageB,String name, String answer)
   {  
       if(name.length()>30)
           System.out.printf("%30s" ,name + ": ");
       else
           System.out.printf("%30s", name + ": ");
       for(int i = 0; i < 4; i++)
       {
           String noAnswer = "NO ANSWERS";
           if(percentageB[i] == -1)
               System.out.printf("%10s", noAnswer);
           else
               System.out.printf("%10s",percentageB[i]);
           System.out.print(" ");
       }
       System.out.printf( "= %s", answer);
       System.out.println();
   }
   public static String getName(Scanner fileScanner)
   {
       String name = fileScanner.nextLine();
       return name;
   }
   public static char[] lineToArray(Scanner fileScanner)
   {
       String answerLine = fileScanner.nextLine();
       char [] letters = answerLine.toCharArray();
       return letters;
   }

   /**
   * this method calculates the percentages for the question type of extra/introverts with only 1 question every 7 questions
   * @param answers- array sent in with the line of answer choices whether A or B for all 70 questions
   * @return- returns the percentage of B's for question type one in the line
   */
   public static int extraAndIntroverts(char [] answers)
   {
       int countA = 0;
       int countB = 0;
       for(int i = 0; i < answers.length;)
       {
           char x = answers[i];
           x = Character.toUpperCase(x);
           if( x == 'A')
               countA ++;
           if( x == 'B')
               countB++;
           i += 7;
       }
       int percentAndT = percentB(countA,countB);
       return percentAndT;
   }

   /**
   * this method is a general method to get the percentages for the question types that have two questions each
   * it calls another method that returns the count of A and B and then calls another method to get the percentage
   * according to counts A and B
   * @param startLoop- each question type starts at a different value such as 1,3,5 which is sent into the method using
   * to count A's and B's
   * @param answers-array sent in with the line of answer choices whether A or B for all 70 questions
   * @return- returns percentage of B's
   */
   public static int getPercent(int startLoop, char [] answers)
   {
       int[] counts = countLetters(startLoop,answers);
       int countA = counts[0];
       int countB = counts[1];
       int percentB = percentB(countA,countB);
       return percentB;
   }

   /**
   * this method takes in the starting loop variable depending on the question type and then uses the array with answer choices
   * to count the number of A choices and B choices
   * @param j-loop start variable depending on which question type it is calculating for
   * @param answers- array sent in with the line of answer choices whether A or B for all 70 questions
   * @return-returns an array with the counts of A choices and B choices
   */
   public static int [] countLetters(int j,char [] answers)
   {
       int countA = 0;
       int countB = 0;
       int [] counts = new int [2];
       for(int i = j; i < answers.length;i +=7)
       {
           char x = answers[i];
           char y = answers[i+1];
           x= Character.toUpperCase(x);
           y = Character.toUpperCase(y);
           if(x == 'A')
               countA++;
           if(y == 'A')
               countA++;
           if(x == 'B')
               countB++;
           if(y == 'B')
               countB++;
       }
       counts[0] = countA;
       counts[1] = countB;
       return counts;
   }

   /**
   * this method takes in count A and B and does the calculations for the percentage of B's for the question type in the line
   * @param countA- number of A choices
   * @param countB- number of B choices
   * @return- percentage of B's
   */
   public static int percentB(int countA,int countB)
   {
       if(countA == 0 && countB == 0)
           return -1;
       double percentageB = 0;
       int totalCount = countA + countB;
       if(totalCount > 0)
           percentageB = ((double)(countB)/totalCount) * 100;
       int percentB = (int)(percentageB + 0.5);
       return percentB;
   }

   /**
   * this method computes the final answer by checking the percent of B's and seeing if it is above 50, or less, = 0 or = -1
   * if equal to -1 it means no answers were chosen
   * @param percentageB- percent of B's sent in for that line/person
   * @return- returns a string with the personality type of the person
   */
   public static String computeAnswer(int[] percentageB)
   {
       String answer = "";
       for(int i =0; i < percentageB.length; i++)  
       {
           if(percentageB[i] > 50)
               answer += TYPE1[i];
           else if(percentageB[i] < 50 && percentageB[i] > -1)
               answer += TYPE2[i];
           else if(percentageB[i] == 50)
               answer += "X";
           else if(percentageB[i] == -1)
               answer += "-";
       }
       return answer;
   }
}


a8data.txt

11
Betty Boop
BABAAAABAAAAAAABAAAABBAAAAAABAAAABABAABAAABABABAABAAAAAABAAAAAABAAAAAA
Snoopy
AABBAABBBBBABABAAAAABABBAABBAAAABBBAAABAABAABABAAAABAABBBBAAABBAABABBB
Bugs Bunny
AABAABBABBBAAAABAAAABAAAAABABBBAABAAAABAABBBBABAAAABAABAAAAAABBAAAAABB
Daffy Duck
BAAAAA-BAAAABABAAAAAABA-AAAABABAAAABAABAA-BAAABAABAAAAAABA-BAAABA-BAAA
The frumious bandersnatch
-BBABAA-BBBBBABBBBA-BABBBBBBBBBABBBBBBABB-BBBABBABBBBBBB-BABBBBBBBBBBB
Minnie Mouse
BABA-AABABBBAABAABA-ABABAAAB-ABAAAAAA-AAAABAAABAAABAAAAAB-ABBAAAAAAAAA
Luke Skywalker
BBBAAABBBBAABA-BAAAABBABBAAABBAABAAB-AAAAABBBABAABABA-ABBBABBABAA-AAAA
Han Solo
BA-ABABBB-BBBAABABAAAABBAAABBAAABBABABBAAABABBAAABABAAAABBABAAABBABAAB
Princess Leia
BABBAAABBBBAAABBA-AAAABABBABBABBAAABAABAAABBBA-AABAABAAAABAAAAABABBBAA
Captain America
BBAA-BABAAABAABAABAAABBABABABAAA-ABBAAABAABAAABAABABA-AA-AAAAAABB-ABAA
No Answer Dude
----------------------------------------------------------------------

sample output


Enter the name of the file with the personality data: a8data.txt                                                                                            
                                                                                                                                                            
                  Betty Boop:         90         15         10         10 = ISTJ                                                                            
                      Snoopy:         30         45         30         70 = ESTP                                                                            
                  Bugs Bunny:         20         45         15         55 = ESTP                                                                            
                  Daffy Duck:        100          6         20          6 = ISTJ                                                                            
   The frumious bandersnatch:         86         95         75         78 = INFP                                                                            
                Minnie Mouse:         67         28         32          5 = ISTJ                                                                            
              Luke Skywalker:         89         61         26         25 = INTJ                                                                            
                    Han Solo:         80         50         45         25 = IXTJ                                                                            
               Princess Leia:         80         50         50          5 = IXXJ                                                                            
             Captain America:        100         21         35         15 = ISTJ                                                                            
              No Answer Dude: NO ANSWERS NO ANSWERS NO ANSWERS NO ANSWERS = ----                                                                            

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