In: Computer Science
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]);
}
}
}
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: