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

In this assignment, you will practice reading from files and creating, filling,

ID: 3824984 • Letter: I

Question

In this assignment, you will practice reading from files and creating, filling, and gathering information from arrays. This part is designed to be fairly simple and will make use of topics from loops and arrays alike. For this portion, you will need two arrays; one to store Booleans and one to store integers. Prompt the user for the number of elements that you want in each array (this number will be used to create both arrays and MUST be positive. Display errors and reprompt until a valid positive number is entered). Create the two arrays to have the user’s specified number of elements. Then, ask the user for two filenames, one that contains random integers and another that contains random Booleans. Use the values from each file to store the numbers/Booleans contained in the file in each element of their respective array. (You can assume that I will enter the file with the correct corresponding number of elements that was entered by the user. Example, if I enter a 6 for total element, I will enter two files with 6 elements - one for each array.) The Boolean array should only contain elements that hold Boolean values (true and false). The integer array should hold values from [-1000, 1000] -- I have generated two different files for each for you to test your code with. One each with 100 items and one each with 1000 items. Once filled with the values from the files, you should display the contents of the array on one line, with each elements value separated by commas and spaces (try not to display a ending comma after the last element). For the Boolean array, display the contents as well as how many elements are true, how many are false, and the index of the first true element (display -1 here, if array contains all false values). For the integer array, display the contents along with how many numbers are odd,which elements are odd (do not repeat numbers more than once, also separate with commas on one line), index of the last zero (display a -1 here, if array contains no zeros), minimum number, maximum number, sum, and what the average (mean value) of all of the elements is (displayed to exactly 3 decimal places). USE JAVA AND NOT C++. FILE NAMES ARE rand_bool6.txt, rand_bool15.txt, rand_bool1000.txt, rand_int6.txt, rand_int15.txt, rand_int1000.txt.

Explanation / Answer

import java.io.*;
import java.util.Scanner;
public class ReadConsole {

public static void main(String args[]) throws IOException {
Scanner reader = new Scanner(System.in); // Reading from System.in
int boolLength, intLength;
do{
System.out.println("Enter a number for the length of Boolean array: ");
boolLength = reader.nextInt();
} while(boolLength <= 0)
do{
System.out.println("Enter a number for the length of Integer array: ");
intLength = reader.nextInt();
} while(intLength <= 0)
int[] intArray = new int[intLength];
boolean[] boolArray = new boolean[boolLength];

String boolFileName, intFileName;
System.out.print("Input Boolean filename:");
boolFileName = Scanscan.nextLine();
System.out.print("Input Integer filename:");
intFileName = Scanscan.nextLine();
File boolFile = new File(boolFileName);
File intFile = new File(intFileName);
try{
int sum, countEven, countOdd;
Scanner sc = new Scanner(intFile);
sum = countEven = countOdd = 0;
while (sc.hasNext()) {
int i = sc.nextInt();
System.out.print(i + ',');
if(i % 2 == 0)
countEven++;
else
countOdd++;
sum += i;
}
sc.close();
int avg = sum/ (countEven + countOdd);
System.out.print("Number of Evens:" + countEven );
System.out.print("Number of Odds:" + countOdd );
System.out.print("Average:" + avg );

Scanner input = new Scanner(intFile);
int trueCount, falseCount, count, firstTrue;
trueCount = falseCount = count = 0;
while (input.hasNext()) {
String bool = input.next();
System.out.print(bool + ',');
count++;
if(bool.equals("true")) {
if(trueCount == 0)
firstTrue = count;
trueCount++;
}
else {
falseCount++;
}
}
input.close();
System.out.print("Number of True values:" + trueCount );
System.out.print("Number of False values:" + falseCount );
System.out.print("First true position:" + firstTrue );
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}