Question

(Polling) The Internet and the web are enabling more people to network, join a cause, voice...

(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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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("\tAvg: " + avg);
if (totRating < minRate) {
minRate = totRating;
minRateIndex = i;
}
if (totRating > maxRate) {
maxRate = totRating;
maxRateIndex = i;
}
}

System.out.println("Maximum points Polled for :\t" + ISSUESARR[maxRateIndex] + ":\t" + maxRate + " points");
System.out.println("Minimum points Polled for:\t" + ISSUESARR[minRateIndex] + ":\t" + 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

Add a comment
Know the answer?
Add Answer to:
(Polling) The Internet and the web are enabling more people to network, join a cause, voice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 1: Adding a Test Harness to the Person class To make it easier to test...

    Exercise 1: Adding a Test Harness to the Person class To make it easier to test code that you have written for a Java class you can add to that class a main method that acts as a "test harness". A test harness is a main method that includes calls to methods that you wish to test. For convention this main method is the last method of the class. If you have a test harness, you do not need to...

  • Write a simple polling program that allows users to rate 5 issues from 1 (low importance)...

    Write a simple polling program that allows users to rate 5 issues from 1 (low importance) to 10 (high importance). Use a one dimensional array of type String to store the five causes. Prompt for the number to be polled. Use a 5X(numPolled) int array to store the results of the survey, but please prompt for the file name. When you run the program ask a user rate each issue. The five issues are Environment, War and Peace, Economy, Education,...

  • write a java program that does the following Part one Use a For loop to compute...

    write a java program that does the following Part one Use a For loop to compute the sum of all the odd numbers from 1 through 99. Your result should be labeled, and the value should be 2500. Print your name 7 times using a While loop. String dogNames[ ] = {"Sam","Buster","Fido","Patches","Gromit","Flicka"}; Using the array defined here, print the values of the array vertically and horizontally using one For-Each loop. Reverse the logic (Nested loops: Indent text) so that the...

  • code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation...

    code in java please:) Part I Various public transporation can be described as follows: A PublicTransportation class with the following: ticket price (double type) and mumber of stops (int type). A CityBus is a PublicTransportation that in addition has the following: an route number (long type), an begin operation year (int type), a line name (String type), and driver(smame (String type) -A Tram is a CityBus that in addition has the following: maximum speed (int type), which indicates the maximum...

  • ArrayQueue

    Implement the ArrayQueue classIn the ‘Queues’ lecture, review the ‘Introduce next lab’ section.  See here that we can use a circular array to implement the queue data structure.  You must write a class named ArrayQueue that does this.ArrayQueue will be a generic class, that implements our generic QueueInterface interface.  This demonstrates the Java interface feature, where we have already implemented queue dynamically, using the LinkedQueue class covered during the lecture.Many classes are given to youDownload and unzip the Circular array project from Canvas, ‘Queues’ module, Example programs.  Open the project in...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

  • In a new file located in the same package as the class Main, create a public...

    In a new file located in the same package as the class Main, create a public Java class to represent a photograph that consists of a linear (not 2D) array of pixels. Each pixel is stored as an integer. The photograph class must have: (a) Two private fields to represent the information stored about the photograph. These are the array of integers and the date the photograph was taken (stored as a String). The values in the array must be...

  • All code will be in Java, and there will be TWO source code. I. Write the...

    All code will be in Java, and there will be TWO source code. I. Write the class  MailOrder to provide the following functions: At this point of the class, since we have not gone through object-oriented programming and method calling in detail yet, the basic requirement in this homework is process-oriented programming with all the code inside method processOrderof this class. Inside method processOrder, we still follow the principles of structured programming.   Set up one one-dimensional array for each field: product...

  • Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment...

    Advanced Object-Oriented Programming using Java Assignment 4: Exception Handling and Testing in Java Introduction -  This assignment is meant to introduce you to design and implementation of exceptions in an object-oriented language. It will also give you experience in testing an object-oriented support class. You will be designing and implementing a version of the game Nim. Specifically, you will design and implement a NimGame support class that stores all actual information about the state of the game, and detects and throws...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT