Question

In: Computer Science

Using files, Write a Java program to read 2 tutorials group of student id, assignment mark...

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

Solutions

Expert Solution

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:


Related Solutions

Write a Java program that will ask the user for his/her mark (numerical integer mark) and...
Write a Java program that will ask the user for his/her mark (numerical integer mark) and then convert this numerical mark into letter grades. The following is a guideline to the grading scale used. The numeric range within parenthesis maps to the preceding letter grade. If the user gave you a number greater than 100 or less than 0, you should print a message that the input is invalid. In this code, DO NOT USE && OPERATOR. You should use...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of...
Using a Java. 2. Write a Java program calculate_fare.java to take the input for number of miles, and the class of journey (1,2, or 3, for first, second, and third class respectively), for a train journey. The program should then calculate and display the fare of journey based on the following criteria: Note: Use Switch...case and if...else construct First (1) Class Second (1) Class Third (3) Class First 100 mile $ 3 per mile $ 2 per mile $ 1.50...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
C++. Write a program that will read in id numbers and place them in an array.The...
C++. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please enter an...
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID...
Write a Java program for a restaurant with the following features: ◦ Customer: Name, Surname, ID (incremental ID by 1 for each new customer), Email, Phone, Address. ◦ Service: ID (incremental ID by1 for each group),CustomerID, Priority (High, Medium, Low, Other), ResolutionTimeFrame (Measured in Man hours), AssignedUser, Status(resolved or not), Fee. ◦ User (simple user of system): ID, Name, Surname, Username and Password (insert from code five fixed users), Address , PhoneNumber ◦ Manager: Name, Surname, Username and Password (insert...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h,...
Write a program in C++ using the following 2 Files: Book.h and Source.cpp 1.)   In Book.h, declare a struct Book with the following variables: - isbn:   string - title:    string - price: float 2.)   In main (Source.cpp), declare a Book object named: book - Use an initialization list to initialize the object with these values:           isbn à 13-12345-01           title à   “Great Gatsby”           price à 14.50 3.)   Pass the Book object to a function named: showBook - The...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
For this assignment, write a program that will calculate the quiz average for a student in...
For this assignment, write a program that will calculate the quiz average for a student in the CSCI 240 course. The student's quiz information will be needed for later processing, so it will be stored in an array. For the assignment, declare one array that will hold a maximum of 12 integer elements (ie. the quiz scores). It is recommended that this program be written in two versions. The first version of the program will read a set of quiz...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT