Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA: Create a class that reads the file you made in the last exercise. Read all

ID: 3705526 • Letter: J

Question

JAVA:

Create a class that reads the file you made in the last exercise. Read all 1000 numbers, add them up and get the average. Also find the max and the min. Print them out and compare with what you calculated last time. determine the most frequent number and compare with the last time also.

Previous exercise:

Create a file. Generate 1000 random integers between 1 and 100. Write the integers to the file. Find the average of the numbers, the largest and the smallest. Write these to the file also. determine the most frequent number. Write it to the file.

Solution for previous exercise:

import java.util.*;

import java.io.*;

class Solution

{

public static void main(String[] args)

{

try{



int[] arr = new int[1000];

  

   int[] count = new int[101];

  

Random rand = new Random();

  

int i;

  

int largest = Integer.MIN_VALUE;

int smallest = Integer.MAX_VALUE;

int sum = 0;

  

for( i = 0 ; i < 1000 ; i++ )

{


arr[i] = rand.nextInt( 100 ) + 1;

  

count[ arr[i] ]++;

  

sum += arr[i];

  

if( arr[i] > largest )

largest = arr[i];

  

if( arr[i] < smallest )

smallest =arr[i];

}

  



double average = (double)sum / 1000.0;

  

int most_frequent = -1;

  

  
int freq = -1;

  

for( i = 1 ; i <= 100 ; i++ )

{

if( count[i] > freq )

{

freq = count[i];



most_frequent = i;

}

}

  


FileWriter fw = new FileWriter("output.txt");

  

fw.write("1000 integers are ... ");

  



for( i = 0 ; i < 1000 ; i++ )

fw.write( String.valueOf(arr[i]) + " " );

  

fw.write(" Largest Element : " + String.valueOf(largest));

fw.write(" Smallest Element : " + String.valueOf(smallest));

fw.write(" Average : " + String.valueOf(average));

fw.write(" Most Frequent Element : " + String.valueOf(most_frequent));

  

   fw.close();

}

catch(Exception e)

{

e.printStackTrace();

}

}

}

Explanation / Answer

Solution.java

import java.util.*;

import java.io.*;

class Solution

{

public static void main(String[] args)

{

try {

int[] arr = new int[1000];

int[] count = new int[101];

Scanner scan = new Scanner(System.in);

System.out.println("Enter the file name: ");

String fileName = scan.next();

File file = new File(fileName);

if(file.exists()) {

int i;

Scanner read = new Scanner(file);

int largest = Integer.MIN_VALUE;

int smallest = Integer.MAX_VALUE;

int sum = 0;

for (i = 0; i < 1000; i++)

{

arr[i] = read.nextInt();

count[arr[i]]++;

sum += arr[i];

if (arr[i] > largest)

largest = arr[i];

if (arr[i] < smallest)

smallest = arr[i];

}

double average = (double) sum / 1000.0;

int most_frequent = -1;

int freq = -1;

for (i = 1; i <= 100; i++)

{

if (count[i] > freq)

{

freq = count[i];

most_frequent = i;

}

}

FileWriter fw = new FileWriter("D:\output.txt");

fw.write("1000 integers are ... ");

for (i = 0; i < 1000; i++)

fw.write(String.valueOf(arr[i]) + " ");

fw.write(" Largest Element : " + String.valueOf(largest));

fw.write(" Smallest Element : " + String.valueOf(smallest));

fw.write(" Average : " + String.valueOf(average));

fw.write(" Most Frequent Element : "

+ String.valueOf(most_frequent));

fw.close();

}

else {

System.out.println("File does not exis");

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

Output:

Enter the file name: D: umbers.txt

1000 integers are ...
24
12
30
100
76
56
37
37
74
92
45
63
52
78
20
40
54
71
85
31
81
71
20
98
25
79
32
54
61
31
55
44
44
12
61
41
47
14
17
44
26
3
82
52
49
42
1
93
1
62
53
24
71
73
20
58
17
58
48
94
15
98
18
3
30
45
98
66
57
33
65
14
80
67
13
29
62
2
35
18
82
84
75
29
18
57
5
43
9
23
7
97
39
25
36
100
59
56
55
53
2
15
13
83
23
7
61
74
75
23
46
80
45
23
4
47
46
15
19
66
60
75
6
88
90
98
16
67
58
43
61
47
78
74
35
32
4
24
76
34
20
37
20
50
40
55
69
54
55
63
87
21
55
61
61
54
11
34
34
81
53
14
69
64
29
32
62
43
90
97
24
42
1
96
40
20
61
93
56
41
18
78
81
26
10
6
88
26
74
97
79
5
69
60
17
51
68
31
75
35
18
35
59
34
51
33
47
91
36
12
30
2
66
26
99
21
25
34
47
31
15
16
38
99
14
74
50
14
74
66
12
29
40
41
43
29
56
22
43
18
31
17
43
3
26
84
96
52
71
76
54
82
18
41
60
36
27
99
35
79
87
13
88
15
51
20
73
33
94
56
87
77
8
4
22
53
48
4
9
73
41
40
12
19
35
57
56
3
61
60
78
87
87
63
84
28
81
55
13
88
72
36
40
97
39
88
17
64
48
42
12
15
50
55
23
61
27
33
38
84
95
64
42
21
58
50
7
36
55
75
35
55
26
96
69
4
86
89
3
63
72
75
93
35
51
57
53
86
61
67
39
23
82
64
18
11
28
93
2
96
72
91
71
94
88
22
79
92
31
45
11
40
33
93
2
87
96
85
32
88
100
81
71
89
46
59
92
27
66
72
4
91
46
20
17
100
95
84
13
89
18
49
11
93
40
45
14
53
56
100
10
60
58
13
68
73
58
66
26
90
94
35
81
69
26
85
45
35
26
28
31
9
93
99
13
67
72
20
10
61
52
5
92
2
3
45
16
99
47
61
66
32
42
10
74
50
83
99
79
29
24
72
58
62
26
55
30
37
89
16
64
61
36
61
98
49
73
39
15
19
93
43
78
76
53
77
59
27
50
70
62
83
63
14
5
83
61
4
59
81
27
91
84
99
23
39
16
82
21
57
72
98
80
84
19
100
47
35
30
47
47
1
8
100
70
84
17
65
35
51
14
61
46
19
36
18
2
60
14
53
72
33
95
99
59
77
61
26
37
22
97
22
17
52
71
58
73
88
33
22
72
43
88
70
6
78
67
83
9
47
92
58
57
45
57
39
58
94
12
67
41
90
48
97
89
92
16
31
93
13
85
64
8
93
64
64
96
14
74
84
7
45
10
1
60
16
5
68
91
74
60
58
9
21
77
18
35
89
15
68
40
82
54
87
34
77
52
39
30
25
39
47
85
97
95
65
13
29
40
88
76
86
32
69
80
60
39
64
30
6
13
53
48
93
63
17
11
17
79
98
18
94
14
36
23
16
13
39
60
30
56
46
26
5
38
66
25
95
47
44
47
52
44
57
20
69
29
52
14
16
1
26
83
52
94
45
20
34
46
31
10
70
92
39
40
75
93
14
59
74
75
48
94
50
68
14
32
82
51
23
61
52
17
39
22
49
79
12
17
62
72
97
45
57
30
34
59
46
48
65
79
97
31
63
58
29
3
62
64
56
54
23
85
85
16
49
93
36
50
52
82
72
77
53
55
87
30
71
51
7
32
71
90
44
86
81
72
20
16
86
100
6
32
2
69
44
63
95
57
64
34
53
6
1
66
54
99
35
40
42
15
45
74
79
45
16
96
98
36
48
19
90
29
74
88
66
75
25
68
3
63
99
95
16
78
5
23
61
73
31
9
48
56
65
40
18
74
55
2
91
85
36
39
57
25
67
55
75
20
67
12
99
42
52
54
36
14
47
12
18
21
46
65
79
69
94
79
52
83
50
95
75
96
69
22
52
10
57
99
3
55
79
83
15
34
11
91
89
33
3
21
65
99
40
38
98
16
6
39
25
91
18
35
63
76
38
54
11
99
100
88
76
85
72
77
33
23
64
64
16
96
22
54
73
64
61
79
54
51
61
72
76
23
97
63
77
74
32
44
62
80
48
25
61
58
66
66
80
85
2
79
39
2
64
19
55
43
73
37
85
18
44
66
11
57
36
95
91
46
83
12
45
26
73
22
94
74
51
78
63
44
15
20
1
77
48
99
43
11
96
10
90
47
91
32
16
82
48
21
8

Largest Element : 100
Smallest Element : 1
Average : 50.825
Most Frequent Element : 61