Question

In: Computer Science

ANSWER IN JAVA Write a simple polling program that allows users to rate five topics from...

ANSWER IN JAVA

Write a simple polling program that allows users to rate five topics from 1 (least important) to 10 (most important). Pick five topics that are important to you (e.g., political issues, global environmental issues, food, video games). Use a one-dimensional array topics (of type String) to store the five issues. To summarize the survey responses, use a 5-row, 10-column two-dimensional array responses (of type int), each row corresponding to an element in the topics array. When the program runs, it should ask the user to rate each issue. Multiple people should be able to respond to the survey during a single run of the program. Once all responses have been logged, have the program display a summary of the results, including:

  1. A tabular report with the five topics down the left side and the 10 ratings across the top, listing in each column the number of ratings received for each topic.

  2. To the right of each row, show the average of the ratings for that issue.

  3. Which issue received the highest point total? Display both the issue and the point total.

  4. Which issue received the lowest point total? Display both the issue and the point to

I've gotten stuck and don't know how to progress forward. if someone could go into detail on how to finish it or make comments as you're editing that would be great. thanks

import java.util.Scanner;

public class Polling_2 {

   public static void main(String[] args) {
       // TODO Auto-generated method stub

       Scanner scnr = new Scanner(System.in);
       String[] topics = {"Cars", "Politics", "Money", "Social", "Food"};
       int [][] myArray = new int [5][11];
       int i;
       int j;
       int peoples;
       int responses;
      
       for (i = 0; i < myArray.length; i++ ) {
           for (j = 0; j < myArray[i].length; j++) {
               myArray[i][j] = scnr.nextInt();
           }
       }
      
              
      
       System.out.print("Enter the amount of people rating: ");
           peoples = scnr.nextInt();
           while ( i < peoples) {
               for int j = 0; j < 5; j++ {
                   System.out.print( myArray[i][j] + " " );
               int x = scnr.nextInt();
           int   responses[j][x] = int responses [j][x] + 1 ;
           }
      
      
      
      
   }

}

Solutions

Expert Solution

// Java program that allows users to rate five topics from 1 (least important) to 10 (most important).

import java.util.Scanner;

public class Polling_2 {

       public static void main(String[] args) {

             Scanner scnr = new Scanner(System.in);

             // topics array

             String[] topics = {"Cars", "Politics", "Money", "Social", "Food"};

             // ratings array

             int [][] ratings = new int [topics.length][];

            

             int i;

             int j;

             int people;

             int rating;

            

             // initialize the ratings array initially with all 0

             for( i=0;i<ratings.length;i++)

             {

                    ratings[i] = new int[10];

                    for( j=0;j<ratings[i].length;j++)

                           ratings[i][j] = 0;

             }

            

             // input of number of people rating

             System.out.print("Enter the number of people rating: ");

             people = scnr.nextInt();

            

             // loop to allow user to rate each topic

             for( i=0;i<people;i++)

             {

                    System.out.println("Rate each topic in the scale 1-10 for the below "+topics.length+" topics : ");

                    for(j=0;j<topics.length;j++)

                    {

                           System.out.print("Enter your rating for "+topics[j]+" : ");

                           rating = scnr.nextInt();

                           // validate rating and re-prompt if invalid

                           while(rating < 1 || rating > 10)

                           {

                                 System.out.println("Rating should be between [1,10]");

                                 System.out.print("Enter your rating for "+topics[j]+" : ");

                                 rating = scnr.nextInt();

                           }

                          

                           ratings[j][rating-1]++; // increment the corresponding rating entry

                    }

             }

            

             // create array for storing average rating, taking integer average

             int avgRating[] = new int[topics.length];

             String highestPointIssue="" , lowestPointIssue="" ;

             int highestPointTotal = 0 , lowestPointTotal= 0;

            

             // loop to calculate the average rating, lowest point total and highets point total

             for(i=0;i<ratings.length;i++)

             {

                    avgRating[i] = 0;

                    for(j=0;j<ratings[i].length;j++)

                           avgRating[i] += (ratings[i][j]*(j+1));

                   

                    if(i == 0) // for first entry initialize the variables

                    {

                           highestPointTotal = avgRating[i];

                           lowestPointTotal= avgRating[i];

                           highestPointIssue = topics[i];

                           lowestPointIssue = topics[i];

                    }else

                    {

                           if(avgRating[i] > highestPointTotal)

                           {

                                 highestPointIssue = topics[i];

                                 highestPointTotal = avgRating[i];

                           }

                          

                           if(avgRating[i] < lowestPointTotal)

                           {

                                 lowestPointIssue = topics[i];

                                 lowestPointTotal= avgRating[i];

                           }

                    }

                   

                    avgRating[i] = avgRating[i]/people;

             }

            

             // output the topics and its count of ratings and average rating in tabular format

             System.out.printf("\n%-20s","");

             for(i=0;i<10;i++)

                    System.out.printf("%-10d",(i+1));

             System.out.printf("%20s","Average Rating");

             System.out.println();

             for(i=0;i<ratings.length;i++)

             {

                    System.out.printf("\n%-20s",topics[i]);

                    for(j=0;j<ratings[i].length;j++)

                           System.out.printf("%-10d",ratings[i][j]);

                    System.out.printf("%20d",avgRating[i]);

             }

            

             // output the topics with highest and lowest point total

             System.out.println("\nHighest Point total : "+highestPointTotal+" Issue : "+highestPointIssue);

             System.out.println("Lowest Point total : "+lowestPointTotal+" Issue : "+lowestPointIssue);

            

             scnr.close();

            

       }

}

//end of program

Output:


Related Solutions

In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write JAVA program that finds 3 students with the best scores. The program asks users for...
Write JAVA program that finds 3 students with the best scores. The program asks users for scores of 5 students. The program prints the first, second, third place students and scores. You can assume that there is no two students with the same score. <EXAMPLE> enter the score of each student score of student 1: 50 score of student 2: 70 score of student 3: 30 score of student 4: 90 score of student 5: 40 1st place is student...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them...
IN JAVA: Write a simple program that takes 5 inputs from the user and save them into a Text File. The inputs are Student Name, Student Address and student Date of Birth. Also write a simple program that reads and display the input from the first program text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT