Answered! Question: Write a program that reads the integers in the text file integers.dat and: Displays the number of……

Write a program that reads the integers in the text file integers.dat and:

Displays the number of integers in the file

Displays the number of even and odd integers

Displays the sum of the even integers and the sum of the odd integers as well as the sum of all the integers

Displays the largest integer and the smallest integer

Computes the average of the largest and smallest integer

Assume the contents of integers.dat (the file is being provided for you) are as follows:

123 475 61 77 910

234 138 134 95 674

345 31 211 952 873

22 7 876 347 450

The following is a sample output: User input in red

Welcome to Wake’s Integer processing Agency

What is name of the input file? integers.dat

The number of integers in the file is 20

There are 11 odd integers and 9 even integers

The sum of the odd integers is 2645

The sum of the even integers is 4390

The sum of all the integers is 7035

The largest integer is 952

The smallest integer is 7

The average of 952 and 7 is 479.5

Expert Answer

Below is your program: –

ReadIntegers.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class ReadIntegers {
public static void main(String[] args) throws FileNotFoundException {

ArrayList<Integer> allInts = new ArrayList<>();
ArrayList<Integer> allEvenInts = new ArrayList<>();
ArrayList<Integer> allOddInts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println(“Welcome to Wake’s Integer processing Agency”);
System.out.print(“What is name of the input file?”);
String fileName = sc.next();
readFile(allInts, allEvenInts, allOddInts, fileName);
System.out.println(“The number of integers in the file is ” + allInts.size());
System.out.println(
“There are ” + allOddInts.size() + ” odd integers and ” + allEvenInts.size() + ” even integers”);
System.out.println(“The sum of the odd integers is ” + getSum(allOddInts));
System.out.println(“The sum of the even integers is ” + getSum(allEvenInts));
System.out.println(“The sum of all the integers is ” + getSum(allInts));
Collections.sort(allInts);
int largest = allInts.get(allInts.size() – 1);
int smallest = allInts.get(0);
System.out.println(“The largest integer is ” + largest);
System.out.println(“The smallest integer is “+ smallest);
System.out.println(“The average of “+largest+” and “+smallest+” is “+((largest+smallest)/2.0));
}

public static void readFile(ArrayList<Integer> allInts, ArrayList<Integer> allEvenInts,
ArrayList<Integer> allOddInts, String fileName) throws FileNotFoundException {
Scanner sc = new Scanner(new File(“integers.dat”));
String line;
String[] word;
while (sc.hasNextLine()) {
line = sc.nextLine();
word = line.split(” “);
int x;
for (int i = 0; i < word.length; i++) {
x = Integer.parseInt(word[i]);
allInts.add(x);
if (x % 2 == 0) {
allEvenInts.add(x);
} else {
allOddInts.add(x);
}
}
}
}

public static int getSum(ArrayList<Integer> ints) {
int sum = 0;
for (int i = 0; i < ints.size(); i++) {
sum = sum + ints.get(i);
}
return sum;
}
}

Sample Run: –

Welcome to Wake’s Integer processing Agency
What is name of the input file?integers.dat
The number of integers in the file is 20
There are 11 odd integers and 9 even integers
The sum of the odd integers is 2645
The sum of the even integers is 4390
The sum of all the integers is 7035
The largest integer is 952
The smallest integer is 7
The average of 952 and 7 is 479.5

Still stressed from student homework?
Get quality assistance from academic writers!