Java How to Program (early objects) (10th Edition) Chapter 7 Question 40 7.40 (P
ID: 3751128 • Letter: J
Question
Java How to Program (early objects) (10th Edition)
Chapter 7 Question 40
7.40 (Polling) The Internet and the web are enabling more people to network, join a cause, voice
opinions, and so on. Recent presidential candidates have used the Internet intensively to get out
their messages and raise money for their campaigns. In this exercise, you’ll write a simple polling
program that allows users to rate five social-consciousness issues from 1 (least important) to 10
(most important). Pick five causes that are important to you (e.g., political issues, global environmental
issues). Use a one-dimensional array topics (of type String) to store the five causes. 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. Have your friends and family respond to the survey. Then have the
program display a summary of the results, including:
a) 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.
b) To the right of each row, show the average of the ratings for that issue.
c) Which issue received the highest point total? Display both the issue and the point total.
d) Which issue received the lowest point total? Display both the issue and the point total.
Explanation / Answer
The JAVA code for the above problem is given below :
import java.util.*;
class polling{//class declaration
public static void main (String[] args) {//main method starts
Scanner sc = new Scanner(System.in);//scanner class to take input
String types[] = new String[5];//types array for topics of priority
int i,j,sum,minsum,maxsum;
System.out.println("Enter any 5 types of high priority : ");//promting admin to enter the five topics
for(i=0;i<5;i++)
{
types[i] = sc.next();
}
int ratings[][] = new int[5][10];
System.out.println("Ask any 10 people to rate : ");//promting the people to rate
for(i=0;i<10;i++)
{
System.out.println("Person "+i +" : ");//Rating by person i
for(j=0;j<5;j++)
{
System.out.println("Enter ratings for " + types[j] + ": ");//Rating by person i for issue j
ratings[j][i] = sc.nextInt();
}
}
//output starts
System.out.print("Topic ");
for(i=0;i<10;i++)
System.out.print("Rating "+i);
System.out.println("Average rating ");
minsum = 100;
maxsum = -1;
for(i=0;i<5;i++)
{
System.out.print(types[i]+" ");//topic
sum = 0;
for(j=0;j<10;j++)
{
System.out.print(ratings[i][j] +" ");//ratings
sum += ratings[i][j];
}
if(sum > maxsum)
maxsum = sum;//finding maximum rated topic
if(sum < minsum)
minsum = sum;//finding minimum rated topic
double average = sum*1.0/10.0;
System.out.println(average);//average rating
}
System.out.println("Issues with highest priorities : ");
for(i=0;i<5;i++)
{
sum=0;
for(j=0;j<10;j++)
sum+=ratings[i][j];
if(sum == maxsum)
System.out.print(types[i]+" ");//issues with highest priorities
}
System.out.println();
System.out.println("Total points : "+maxsum);
System.out.println("Issues with lowest priorities : ");
for(i=0;i<5;i++)
{
sum=0;
for(j=0;j<10;j++)
sum+=ratings[i][j];
if(sum == minsum)
System.out.print(types[i]+" ");//issues with lowest priorities
}
System.out.println();
System.out.println("Total points : "+minsum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.