Write an application that displays a series of at least four survey questions; t
ID: 3908929 • Letter: W
Question
Write an application that displays a series of at least four survey questions; the survey can be on any social or political topic you want, and each question should have at least three possible numeric-choice answers. At the end of the survey, use a dialog box to ask whether the user wants to (1) enter another set of responses to the same set of questions, or (2) quit. Continue to accept sets of responses until the user chooses to quit, and then display the results of the survey—for each question indicate how many users chose the first option, second option, and so on. Save the file as Survey.java.
Explanation / Answer
Below is your code. If you have any problem with this code. Please let me know in comments.
public class Survey {
private static String surveyQ1;
private static String surveyQ2;
private static String surveyQ3;
private static String surveyQ4;
private static int ans[][] = new int[4][3];
private static int userC;
private static Scanner input;
public static void main(String[] args) {
int ansI, choice;
input = new Scanner(System.in);
surveyQ1 = "How good is our coffee ( 1/2/3 ): ";
surveyQ2 = "How good is our burger ( 1/2/3 ): ";
surveyQ3 = "How good is our tea ( 1/2/3 ): ";
surveyQ4 = "How good is our overall service ( 1/2/3 ): ";
for (int i = 0; i < ans.length; i++) {
for (int j = 0; j < ans[i].length; j++) {
ans[i][j] = 0;
}
}
userC = 0;
do {
userC++;
System.out.println("User Number : " + userC);
System.out.println(surveyQ1);
ansI = input.nextInt();
countAnswers(0, ansI - 1);
System.out.println(surveyQ2);
ansI = input.nextInt();
countAnswers(1, ansI - 1);
System.out.println(surveyQ3);
ansI = input.nextInt();
countAnswers(2, ansI - 1);
System.out.println(surveyQ4);
ansI = input.nextInt();
countAnswers(3, ansI - 1);
choice = JOptionPane.showConfirmDialog(null, "Do you want to enter into survey again: ", "Select an option",
JOptionPane.YES_NO_OPTION);
} while (choice == JOptionPane.YES_OPTION);
displayResults();
}
private static void displayResults() {
System.out.println("Number of users participated in the survey: "+userC);
System.out.println("Below is the details of all the questions answered : ");
System.out.println(surveyQ1);
System.out.println("Option 1 : "+ans[0][0]);
System.out.println("Option 2 : "+ans[0][1]);
System.out.println("Option 3 : "+ans[0][2]);
System.out.println(" "+surveyQ2);
System.out.println("Option 1 : "+ans[1][0]);
System.out.println("Option 2 : "+ans[1][1]);
System.out.println("Option 3 : "+ans[1][2]);
System.out.println(" "+surveyQ3);
System.out.println("Option 1 : "+ans[2][0]);
System.out.println("Option 2 : "+ans[2][1]);
System.out.println("Option 3 : "+ans[2][2]);
System.out.println(" "+surveyQ4);
System.out.println("Option 1 : "+ans[3][0]);
System.out.println("Option 2 : "+ans[3][1]);
System.out.println("Option 3 : "+ans[3][2]);
}
private static void countAnswers(int i, int j) {
ans[i][j]++;
}
}
Output
User Number : 1
How good is our coffee ( 1/2/3 ):
2
How good is our burger ( 1/2/3 ):
3
How good is our tea ( 1/2/3 ):
1
How good is our overall service ( 1/2/3 ):
2
User Number : 2
How good is our coffee ( 1/2/3 ):
2
How good is our burger ( 1/2/3 ):
2
How good is our tea ( 1/2/3 ):
2
How good is our overall service ( 1/2/3 ):
3
User Number : 3
How good is our coffee ( 1/2/3 ):
1
How good is our burger ( 1/2/3 ):
1
How good is our tea ( 1/2/3 ):
1
How good is our overall service ( 1/2/3 ):
1
User Number : 4
How good is our coffee ( 1/2/3 ):
3
How good is our burger ( 1/2/3 ):
3
How good is our tea ( 1/2/3 ):
3
How good is our overall service ( 1/2/3 ):
3
User Number : 5
How good is our coffee ( 1/2/3 ):
2
How good is our burger ( 1/2/3 ):
2
How good is our tea ( 1/2/3 ):
2
How good is our overall service ( 1/2/3 ):
2
Number of users participated in the survey: 5
Below is the details of all the questions answered :
How good is our coffee ( 1/2/3 ):
Option 1 : 1
Option 2 : 3
Option 3 : 1
How good is our burger ( 1/2/3 ):
Option 1 : 1
Option 2 : 2
Option 3 : 2
How good is our tea ( 1/2/3 ):
Option 1 : 2
Option 2 : 2
Option 3 : 1
How good is our overall service ( 1/2/3 ):
Option 1 : 1
Option 2 : 2
Option 3 : 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.