In: Computer Science
Using files, Write a Java program to read 2 tutorials group of
student id,
assignment mark (30), lab test mark(20) from 2 input files namely
group1.txt
and group 2.txt. Your program will calculate the total course work
marks and
write in to 3 output files with the following conditions:
if the total course work marks are more than 40 out 50 write in to
LIST1.TXT
if the total course work marks are between 30 to 40 then write in
to LIST2.TXT
if the total course work marks are less than 30 then write in to
LIST2.TXT
Program: In this program, we read in two text files and store the marks in two array lists, then we calculate the total marks and write into three files based on the ranges of the marks.
Below is the implementation:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Solution {
    // Main
    public static void main(String[] args) {
        // Create two array lists to store assignment marks and lab test marks
        ArrayList<Integer> assignmentMarks = new ArrayList();
        ArrayList<Integer> labTestMarks = new ArrayList();
        // Read operation
        // Create a try catch block to throw exceptions
        try {
            // Create File class object and pass input file name
            File file1 = new File("group1.txt");
            // Create Scanner class instance and pass file object as input
            Scanner read = new Scanner(file1);
            // Read till the end of file
            while (read.hasNext()) {
                // Store current marks
                int currMarks = read.nextInt();
                // Add to file
                assignmentMarks.add(currMarks);
            }
            // Create File class object and pass input file name
            File file2 = new File("group2.txt");
            // Create Scanner class instance and pass file object as input
            read = new Scanner(file2);
            // Read till the end of file
            while (read.hasNext()) {
                // Store current marks
                int currMarks = read.nextInt();
                // Add to file
                labTestMarks.add(currMarks);
            }
            // Close scanner instance
            read.close();
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // Write operation
        try {
            // Create three FileWriter class objects to write in three files
            FileWriter fileWriter1 = new FileWriter("LIST1.txt");
            FileWriter fileWriter2 = new FileWriter("LIST2.txt");
            FileWriter fileWriter3 = new FileWriter("LIST3.txt");
            // Traverse the marks list
            for (int i = 0; i < assignmentMarks.size(); i++) {
                int assignmentMark = assignmentMarks.get(i);
                int labTestMark = labTestMarks.get(i);
                // Sum up total marks
                int total = assignmentMark + labTestMark;
                // Check range of marks and write in corresponding file
                if (total > 40) {
                    fileWriter1.write("Student: " + (i + 1) + " -> Marks: " + total + "\n");
                } else if (total > 30) {
                    fileWriter2.write("Student: " + (i + 1) + " -> Marks: " + total + "\n");
                } else {
                    fileWriter3.write("Student: " + (i + 1) + " -> Marks: " + total + "\n");
                }
            }
            // Close file writer
            fileWriter1.close();
            fileWriter2.close();
            fileWriter3.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



group1.txt:

group2.txt:

LIST1.txt:

LIST2.txt:

LIST3.txt:
