JAVA Your task is to write an application to ask people’s preferences on a topic
ID: 3870583 • Letter: J
Question
JAVA
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
Note : Could u please check the results..If u want me to do any modifications.I will do it.Thank You
___________________
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.