How to Develop a Personality Test Application in Java
Personality tests quantitatively assess an individual's character traits based on her answers to a series of questions about circumstances or beliefs. These questions will usually ask an individual's level of agreement or disagreement with these circumstances or general beliefs. Making such a test in Java is a great way for beginner programmers to tackle a more advanced project, and a good way for the student of psychology to quickly collect data through these kinds of tests.
Instructions
-
-
1
Write out all the questions the tests will cover, and the categories into which different scores will fall.
-
2
Create a String Array that will hold each of your personality test questions. Add another dimension for question category if your questions will be scored differently. Use the basic syntax of "String[] MyQuestionArray = new String[n];" where "MyQuestionArray" is the name of your array of string questions and "n" is the number of questions the test will comprise.
-
-
3
Assign each question to an array element, using the appropriate derivation of the basic syntax "MyQuestionArray[n] = 'My Question';" where "n" is the array element to which you are assigning the question. Remember that element numbers start at zero, so your first question assignment will resemble the syntax "MyQuestionArray[0] = 'My Question';" and each question after the first will increment by one.
-
4
Create the graphical layout of your application with a JLabel that will hold the question. Declare the JLabel with the syntax "JLabel MyQuestion;" where "MyQuestion" is the name you will give this JLabel in your program in the area above any of your methods in your program class. In the class, use the syntax "MyQuestion = new JLabel(MyQuestionArray[n]);" to pull each question "n" from your question array to display in your JLabel.
-
5
Create the JRadioButtons that will hold the answers the test taker can choose from with the syntax "JRadioButton AnswerButton = new JRadioButton("Answer");" where "AnswerButton" is the unique name you give to that answer button an "Answer" is the answer the button will represent.
-
6
Add the JButton that will allow the user to submit her answers for scoring with the basic syntax "JButton SubmitAnswers = new JButton("Submit");" where "SubmitAnswers" is the name you want to give the button and "Submit" is the text you want the button to hold.
-
7
Add an "ActionListener" to the button with the syntax "SubmitAnswer.addActionListener(this);" and the method this button will trigger by declaring an "actionPerformed" method with the syntax "public void actionPerformed(ActionEvent e){
}" where the code to assess and score the user's answers will go within the "{ }" of this method.
-
8
Determine the answers the test taker provided with "if" statements that trigger if the user selected a specific button. JRadioButtons use a method called "isSelected()" that returns a boolean value of true or false. Thus, if a user selected a particular button then the statement "AnswerButton.isSelected()" where "AnswerButton" is the name of the particular button you are examining will equate to true. Consequently, putting the aforementioned statement inside the parenthesis of the "if" condition will cause the particular "if" condition to trigger if the user selected that button.
-
9
Put the appropriate code to score a particular answer within the "if" statement that will trigger when a particular button for a particular question is triggered.
-
1
References
- Photo Credit Creatas/Creatas/Getty Images