In: Computer Science
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:
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.
To the right of each row, show the average of the ratings for that issue.
Which issue received the highest point total? Display both the issue and the point total.
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 ;
}
}
}
// 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: