Question

In: Computer Science

The StudentPoll.java document program contains an array of survey responses that are hard-coded into the program....

The StudentPoll.java document program contains an array of survey responses that are hard-coded into the program. Suppose we wish to process survey results that are stored in a file. This exercise requires two separate programs. First, create an application that prompts the user for survey responses and outputs each response to a file. Use a Formatter to create a file called numbers.txt. Each integer should be written using the method format. Then modify the attached program to read the survey responses from numbers.txt. The responses should be read from the file by using a Scanner. Use method nextInt to input one integer at a time from the file. The program should continue to read responses until it reaches the end of the file. The results should be output to the text file "output.txt".

StudentPoll.java

public class StudentPoll {
public static void main(String[] args) {
// student response array (more typically, input at runtime)
int[] responses =
{1, 2, 5, 4, 3, 5, 2, 1, 3, 3, 1, 4, 3, 3, 3, 2, 3, 3, 2, 14};
int[] frequency = new int[6]; // array of frequency counters

// for each answer, select responses element and use that value
// as frequency index to determine element to increment
for (int answer = 0; answer < responses.length; answer++) {
try {   
++frequency[responses[answer]];
}   
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e); // invokes toString method
System.out.printf(" responses[%d] = %d%n%n",   
answer, responses[answer]);   
}   
}

System.out.printf("%s%10s%n", "Rating", "Frequency");

// output each array element's value
for (int rating = 1; rating < frequency.length; rating++) {
System.out.printf("%6d%10d%n", rating, frequency[rating]);
}
}
}

Solutions

Expert Solution

Java Program:

Program 1:

import java.util.*;
import java.io.*;

public class StudentPoll_1 {

   public static void main(String[] args) throws FileNotFoundException {
      
       //Opening file for reading
       Formatter op = new Formatter("numbers.txt");
      
       //Scanner object
       Scanner reader = new Scanner(System.in);
      
       //Reading data from user
       while(true)
       {
           //Reading response
           System.out.println("\n Enter a survey response(-111 to end): ");
           int n = reader.nextInt();
          
           if(n == -111)
           {
               break;
           }
          
           //Storing in file
           op.format("%d ", n);
       }
      
       reader.close();
       op.close();
      
       System.out.println("\n Successfully written to file... \n");
   }
}

Program 2:

import java.util.*;
import java.io.*;

public class StudentPoll_2 {
   public static void main(String[] args) throws FileNotFoundException, IOException {
      
       //Opening file for reading
       FileWriter writter = new FileWriter("output.txt");
       Scanner reader = new Scanner(new File("numbers.txt"));
      
       int[] frequency = new int[6]; // array of frequency counters
       // for each answer, select responses element and use that value
       // as frequency index to determine element to increment
       while(reader.hasNext()) {
           int answer = reader.nextInt();
           try {   
               ++frequency[answer];
           }   
           catch (ArrayIndexOutOfBoundsException e) {
               System.out.println(e); // invokes toString method
               System.out.printf("\n%d\n", answer);   
           }   
       }
       writter.write("Rating \t Frequency\n");

       // output each array element's value
       for (int rating = 1; rating < frequency.length; rating++) {
           writter.write(rating + " \t\t " + frequency[rating] + "\n");
       }
      
       reader.close();
       writter.close();
   }
}

_________________________________________________________________________________________________________

Sample Run:


Related Solutions

Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of...
Seasons Re-Visited Write a program that contains an array of strings in main(). Your array of strings will contain the seasons, “Winter”, “Spring”, “Summer”, and “Fall”. Your program will then contain a function that will use your seasons array to display a menu and ask the user to choose their favorite season and return that value to the main() function. Your function must include a data validation loop. The user must enter in a 1 for winter, a 2 for...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Post a Python program that contains an array variable whose values are input by the user....
Post a Python program that contains an array variable whose values are input by the user. It should the perform some modification to each element of array using a loop and then the modified array should be displayed. Include comments in your program that describe what the program does. Also post a screen shot of executing your program on at least one test case.
The program is hard coded to only work with arrays of 10 numbers. Add a constant called size before main that is assigned the number of elements to be sorted.
#include#include/*   Program sorts an array of integers using a selection sort.   The general algorithm repeatedly finds the smallest number   in the array and places it at the front of the list. */ using namespace std; int find_small_index (int start_index, int numbers []); void swap_values (int index1, int index2, int numbers []); int main(int argc, char *argv[]) {     // array of numbers     int numbers [10] = {7, 9, 21, 16, 65, 8, 32, 1, 17, 41};     int start_index;  // current starting spot for search     int small_index;  // index of the smallest number in the array     int index;        // index used for print the array values          start_index = 0;     // continue finding the smallest value and placing it     // at the front of the list     while (start_index < 9)     {           small_index = find_small_index (start_index, numbers);           swap_values (small_index, start_index, numbers);           start_index++;     }          cout << "\n\nThe sorted array is:\n";     for (index = 0; index < 10; index++)         cout << numbers [index] << " ";     cout << "\n\n";                return 0; }   int find_small_index (int start_index, int numbers []) {     int small_index, // smallest index to be returned         index;       // current index being viewed          small_index = start_index;     for (index = start_index + 1; index < 10; index++)         if (numbers [index] < numbers [small_index])            small_index = index;     return small_index; }      void swap_values (int index1, int index2, int numbers []) {      int swapper;            swapper = numbers [index1];      numbers [index1] = numbers [index2];      numbers [index2] = swapper; }- The program is hard coded to only work with arrays of 10 numbers. Add a constant called size before main that is assigned the number of...
this has to be coded in c ++ • Write a program which generates a DIFFERENT...
this has to be coded in c ++ • Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to solve this. You can submit their code. Make sure you include a comment which shows from where the code was taken. You do not need to understand the code but if you can, thats great. This program will be used in the upcoming...
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
For your task, provide responses to the following in a Word document and submit them: project...
For your task, provide responses to the following in a Word document and submit them: project name - Build an IT infrastructure for a medium size company that does accounting Explain why you selected this project Based on your project selection, explain what requirements and restrictions would be encountered Explain how you would go about constructing this project using an engineering approach
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
In the Cell Phone Survey below, test the hypothesis that the mean responses for Value for...
In the Cell Phone Survey below, test the hypothesis that the mean responses for Value for the Dollar and Customer Service do not differ by gender. Gender Carrier Type Usage Signal strength Value for the Dollar Customer Service M AT&T Smart High 5 4 4 M AT&T Smart High 5 4 2 M AT&T Smart Average 4 4 4 M AT&T Smart Very high 2 3 3 M AT&T Smart Very high 5 5 2 M AT&T Smart Very high...
Draft a program that scans an alphanumeric array to determine the first alphabet in the array.  If...
Draft a program that scans an alphanumeric array to determine the first alphabet in the array.  If found, the program should print “alphabet found” its value and index.  If no alphabet is found, the program should print “no alphabet found.” Submit the asm/list file and screenshots that show the output of your code for the following example arrays: a. Array has only alphabets b. Array has only numerals c. Several arrays with a mix of alphabets and numerals positioned at different indices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT