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

Create a program that contains two functions: main and a void function to read d

ID: 3596519 • Letter: C

Question

Create a program that contains two functions: main and a void function to read data from a file and process it. Your main function should prompt the user to enter the name of the file (Lab7.txt), store this name, and then call the function sending the name of the file to the function. After the function has completed, the main function should output the total number of values in the file, how many of the values were odd, how many of the values were even, and how many of the values were zero.

Your other function should accept the string object containing the name of the data file and other parameters as needed. This function should define a file object for input, open the file, skip over the first line (do not store the first line). Then this function should use a single loop to count the total number of values in the file, how many of those values were even, how many of those values were odd, and how many of those values were zero (do not include zeros as even). After all the values have been read and processed, this function should send back to main the number of values in the file, the number of zeros, the number of odd and the number of even values. Carefully consider which parameters should be passed by value and which should be reference parameter.

Remember to write your function definition after main and write a function declaration (prototype) before main for your function. Write comments immediately before your new function describing the task of the function, input, output, and processing for the second function. Do not use the same variable name in multiple (more than one) functions.

Lab 7 data   
31
41
-37
41
13
-40
-22
5
46
46
-34
47
46
-1
30
-36
-8
42
29
46
16
-46
35
43
18
0
24
-11
16
-33
21
-47
-22
-45
-40
32
19
-18
45
-47
-6
-12
27
0
-31
-1
-5
15
21
25
-22
18
16
-34
-38
0
46
-16
9
-28
25
-24
1
20
39
46
5

Explanation / Answer

Please find my implementation.

import java.io.File;

import java.io.FileNotFoundException;

import java.util.Scanner;

public class ReadData {

  

   public static void countNumbers(String fileName) throws FileNotFoundException {

      

       Scanner sc = new Scanner(new File(fileName));

      

       int total = 0;

       int even = 0;

       int odd = 0;

       int zeros = 0;

      

       // skipping first line

       //sc.nextLine();

       while(sc.hasNextLine()) {

          

           String line = sc.nextLine();

           //System.out.println(line);

           try{      

               int num = Integer.parseInt(line.trim());

               total++;

               if(num % 2 == 0)

                   even++;

               else

                   odd++;

              

               if(num == 0)

                   zeros++;

              

           }catch(Exception e) {

               //System.out.println(e);

           }

       }

      

       sc.close();

       System.out.println("Total numbers: "+total);

       System.out.println("Total Even Numbers: "+even);

       System.out.println("Total Odd Numbers: "+odd);

       System.out.println("Total Zeros: "+zeros);

   }

  

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

      

       Scanner sc = new Scanner(System.in);

      

       System.out.print("Enter input file name: ");

       String fileName = sc.next();

      

       sc.close();

      

       countNumbers(fileName);

      

      

   }

}

/*

Sample run:

Enter input file name: int_data.txt

Total numbers: 67

Total Even Numbers: 36

Total Odd Numbers: 31

Total Zeros: 3

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote