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

For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The...
***IN JAVA*** Write a program contained a class Student which has firstName, lastName, mark, grade. The program should allow creation an array of object instances to assign firstName, lastName and mark from input user and perform and assign grade based on mark’s criteria displayed below. MARKS INTERVAL 95 - 100 90 - <95 85 - <90 80 - <85 75 - <80 70 - <75 65 - <70 60 - <65 0 - <60 LETTER GRADE A+ A B+ B...
using java For this assignment, you will write a program that guesses a number chosen by...
using java For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to choose a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
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...
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...
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...
11) Enter the following using Java. Use for loop to enter student id, student name, major...
11) Enter the following using Java. Use for loop to enter student id, student name, major and grades. Also grades should display their letter grades based on their conditions. After that display the name of students and their info, and also their letter grade After that display the sum of all grades and the average Output Example: Student ID: 0957644 Student Name: Pedro Hernandez Student Major: Business Information Systems Grades for Fall 2020: Introduction to Computer Science using Java: 85,...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT