(Polling) The Internet and the web are enabling more people to network, join a c
ID: 3871389 • Letter: #
Question
(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 environ- mental issues). Use a one-dimensional array topics (of type String) to store the five causes. To sum- marize 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.
Your task is to write an application to ask people’s preferences on a topic of your choice and compute statistics and display the results. Follow the direction in Exercise 7.40, but with your own topic, instead of surveying social-consciousness issues. An example topic is to survey the favorite winter Olympic games people want to watch among speed skating, snowboard, figure skating, bobsleigh, and ice hockey. Rate the item from 1 (least preferred) to 10 (most preferred). You must use at least one enhanced for statement. Please use a separate method to compute and display the results instead of implementing all the code in the main method. The method must have at least one parameter of an array that includes the user input.
Your code must compile and run from the windows command line using the following commands.
javac Polling.java
java Polling
You must add appropriate comments in you code as below and generate javadoc.
Add class level javadoc comment with @author tag and description about the class.
Add method level javadoc comments with @param and @return (if your method returns a
value) tags with the description of the method, parameters, and return value.
In the methods, please add end-of-line or block comments whenever necessary to help others
understand your code
DO NOT add comments such as “ // end of method”, “// end of class”. Such comments were
added in the textbook just teach you the Java grammar. Your application does not need to explain the Java grammar. Instead, you have to explain what your application does.
Please make sure the java documents you generated contains your javadoc comments properly.
Explanation / Answer
Polling.java
import java.util.Random;
import java.util.Scanner;
public class Polling {
public static final String[] ISSUESARR = {
"Global Warming",
"Earth Quakes",
"Stopping war",
"Equal Rights",
"Curing Cancer",
};
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int rate, totRating;
int minRate, maxRate, minRateIndex, maxRateIndex;
double avg;
//System.out.println(ISSUESARR.length);
int[][] pollingArray = new int[ISSUESARR.length][10];
for (int i = 0; i < pollingArray[0].length; i++) {
System.out.println("Person#" + (i + 1));
System.out.println("Rate between 1-10");
for (int j = 0; j < pollingArray.length; j++) {
System.out.print(ISSUESARR[j] + ":");
pollingArray[j][i] = validRating();
}
}
minRate = Integer.MAX_VALUE;
maxRate = Integer.MIN_VALUE;
minRateIndex = -1;
maxRateIndex = -1;
for (int i = 0; i < pollingArray.length; i++) {
System.out.printf("%20s :", ISSUESARR[i]);
totRating = 0;
for (int j = 0; j < pollingArray[0].length; j++) {
System.out.printf("%5d", pollingArray[i][j]);
totRating += pollingArray[i][j];
}
avg = ((double) totRating) / pollingArray[0].length;
System.out.println(" Avg: " + avg);
if (totRating < minRate) {
minRate = totRating;
minRateIndex = i;
}
if (totRating > maxRate) {
maxRate = totRating;
maxRateIndex = i;
}
}
System.out.println("Maximum points Polled for : " + ISSUESARR[maxRateIndex] + ": " + maxRate + " points");
System.out.println("Minimum points Polled for: " + ISSUESARR[minRateIndex] + ": " + minRate + " points");
}
/*
* This method will get the rating and check whether it is valid or not
*
* @Param void
*
* @return integer
*/
private static int validRating() {
int rate;
while (true) {
rate = sc.nextInt();
if (rate < 0 || rate > 10) {
System.out.println("** Must be between 1-10 **");
System.out.print("Enter again :");
continue;
} else
break;
}
return rate;
}
}
______________________
Output:
Person#1
Rate between 1-10
Global Warming:3
Earth Quakes:3
Stopping war:4
Equal Rights:5
Curing Cancer:6
Person#2
Rate between 1-10
Global Warming:6
Earth Quakes:6
Stopping war:5
Equal Rights:4
Curing Cancer:5
Person#3
Rate between 1-10
Global Warming:5
Earth Quakes:6
Stopping war:6
Equal Rights:7
Curing Cancer:7
Person#4
Rate between 1-10
Global Warming:8
Earth Quakes:8
Stopping war:8
Equal Rights:7
Curing Cancer:6
Person#5
Rate between 1-10
Global Warming:5
Earth Quakes:4
Stopping war:5
Equal Rights:6
Curing Cancer:7
Person#6
Rate between 1-10
Global Warming:8
Earth Quakes:7
Stopping war:6
Equal Rights:5
Curing Cancer:54
** Must be between 1-10 **
Enter again :4
Person#7
Rate between 1-10
Global Warming:5
Earth Quakes:6
Stopping war:7
Equal Rights:6
Curing Cancer:56
** Must be between 1-10 **
Enter again :4
Person#8
Rate between 1-10
Global Warming:1
Earth Quakes:2
Stopping war:3
Equal Rights:4
Curing Cancer:5
Person#9
Rate between 1-10
Global Warming:6
Earth Quakes:7
Stopping war:8
Equal Rights:1
Curing Cancer:2
Person#10
Rate between 1-10
Global Warming:3
Earth Quakes:4
Stopping war:5
Equal Rights:6
Curing Cancer:7
Global Warming : 3 6 5 8 5 8 5 1 6 3 Avg: 5.0
Earth Quakes : 3 6 6 8 4 7 6 2 7 4 Avg: 5.3
Stopping war : 4 5 6 8 5 6 7 3 8 5 Avg: 5.7
Equal Rights : 5 4 7 7 6 5 6 4 1 6 Avg: 5.1
Curing Cancer : 6 5 7 6 7 4 4 5 2 7 Avg: 5.3
Maximum points Polled for : Stopping war: 57 points
Minimum points Polled for: Global Warming: 50 points
______________________
2)
Survey.java
import java.util.Scanner;
public class Survey {
// Declaring static variables
static Scanner sc;
static String sports[] = {
"speed skating",
"snowboard",
"figure skating",
"bobsleigh",
"ice hockey"
};
public static void main(String[] args) {
// Getting how many persons you want to servey
System.out.print("How many People You want to survey :");
int noOfpeople = sc.nextInt();
// Creating an array
int ratings[] = new int[5];
// calling the methods
ratings = compute(ratings, noOfpeople);
displayResults(ratings);
}
/*
* This method will display the total rating of all sports
*
* @Param integer array
*
* @return void
*/
private static void displayResults(int[] ratings) {
for (int i = 0; i < ratings.length; i++)
System.out.println("Total Rating to " + sports[i] + " is " + ratings[i]);
}
/*
* This method will take the rating of all sports from each person
*
* @Param integer array(ratings) and integer(no of people)
*
* @return integer array(ratings)
*/
private static int[] compute(int[] ratings, int noOfpeople) {
int choice;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
sc = new Scanner(System.in);
int rate;
for (int i = 0; i < noOfpeople; i++) {
System.out.println("Person#" + (i + 1) + ":");
for (int j = 0; j < ratings.length; j++) {
System.out.print("Enter rating to " + sports[j] + " :");
ratings[j] += validRating();
}
}
return ratings;
}
/*
* This method will get the rating and check whether it is valid or not
*
* @Param void
*
* @return integer
*/
private static int validRating() {
int rate;
while (true) {
rate = sc.nextInt();
if (rate < 0 || rate > 10) {
System.out.println("** Must be between 1-10 **");
System.out.print("Enter again :");
continue;
} else
break;
}
return rate;
}
}
________________________
Output:
How many People You want to survey :5
Person#1:
Enter rating to speed skating :7
Enter rating to snowboard :8
Enter rating to figure skating :12
** Must be between 1-10 **
Enter again :8
Enter rating to bobsleigh :7
Enter rating to ice hockey :6
Person#2:
Enter rating to speed skating :8
Enter rating to snowboard :9
Enter rating to figure skating :9
Enter rating to bobsleigh :8
Enter rating to ice hockey :6
Person#3:
Enter rating to speed skating :6
Enter rating to snowboard :56
** Must be between 1-10 **
Enter again :8
Enter rating to figure skating :9
Enter rating to bobsleigh :8
Enter rating to ice hockey :10
Person#4:
Enter rating to speed skating :12
** Must be between 1-10 **
Enter again :7
Enter rating to snowboard :8
Enter rating to figure skating :9
Enter rating to bobsleigh :7
Enter rating to ice hockey :5
Person#5:
Enter rating to speed skating :8
Enter rating to snowboard :9
Enter rating to figure skating :10
Enter rating to bobsleigh :10
Enter rating to ice hockey :10
Total Rating to speed skating is 36
Total Rating to snowboard is 42
Total Rating to figure skating is 45
Total Rating to bobsleigh is 40
Total Rating to ice hockey is 37
________________Thank YOu
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.