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

Write a java program that • creates three text files: grade1.txt, grade2.txt, gr

ID: 3762674 • Letter: W

Question

Write a java program that

• creates three text files: grade1.txt, grade2.txt, grade3.txt
            -Each contains 5 students’ names (first name, last name) and grades (integers) that are obtained from the keyboard input.

• combine all three files into one text file: allgrades.txt
           - It contains all 15 students' names and grades from the 3 text file.
o It also contains letter grades for each student
                    :If grade < 60, it adds letter grade "F"
                     :If 60 <= grade <70, it adds letter grade "D".
                     :If 70 <= grade < 80, it adds letter grade "C".
                     :If 80 <= grade < 90, it adds letter grade "B".
                     :If 90 <= grade, it adds letter grade "A".

• displays the contents of text file allgrade.txt on the console

Explanation / Answer


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

/**
* @author srinu
*
*/
public class GradeCalc {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub

       String finalData = "";
       System.out.println("Enter Data for grade1.txt :");
       finalData = readData("grade1.txt");

       System.out.println("Enter Data for grade2.txt :");
       finalData += readData("grade2.txt");

       System.out.println("Enter Data for grade3.txt :");
       finalData += readData("grade3.txt");

       writeDataToFile("allgrades.txt", finalData);
       display("allgrades.txt");

   }

   public static String readData(String fileName) {
       // first name, last name) and grades
       String firstName, lastName;
       int[] grades = new int[4];
       String data = "", finalData = "";

       for (int i = 1; i <= 5; i++) {
           Scanner scanner = new Scanner(System.in);
           System.out.print("Enter Student" + i + " First name:");
           firstName = scanner.nextLine();

           System.out.print("Enter Student" + i + " Last name:");
           lastName = scanner.nextLine();

           System.out.print("Enter Student" + i + " Grade1:");
           grades[0] = scanner.nextInt();

           System.out.print("Enter Student" + i + " Grade2:");
           grades[1] = scanner.nextInt();

           System.out.print("Enter Student" + i + " Grade3:");
           grades[2] = scanner.nextInt();

           System.out.print("Enter Student" + i + " Grade4:");
           grades[3] = scanner.nextInt();

           data += firstName + " " + lastName + " " + grades[0] + " "
                   + grades[1] + " " + grades[2] + " " + grades[3] + " ";
           finalData += firstName + " " + lastName + " " + grades[0] + " "
                   + grades[1] + " " + grades[2] + " " + grades[3] + " "
                   + getGrade(grades[0]) + " " + getGrade(grades[1]) + " "
                   + getGrade(grades[2]) + " " + getGrade(grades[3]) + " ";
           //System.out.println("final data=="+finalData);

       }
       writeDataToFile(fileName, data);
       return finalData;

   }

   public static char getGrade(int grade) {
       /*
       * :If grade < 60, it adds letter grade "F" :If 60 <= grade <70, it adds
       * letter grade "D". :If 70 <= grade < 80, it adds letter grade "C". :If
       * 80 <= grade < 90, it adds letter grade "B". :If 90 <= grade, it adds
       * letter grade "A".
       */
       if (grade >= 90 && grade <= 100) {
           return 'A';
       } else if (grade >= 80 && grade <= 89) {
           return 'B';
       } else if (grade >= 70 && grade <= 79) {
           return 'C';
       } else if (grade >= 60 && grade <= 69) {
           return 'D';
       } else {
           return 'F';
       }

   }

   public static void writeDataToFile(String fileName, String data) {

       BufferedWriter output = null;
       try {
           File file = new File(fileName);
           output = new BufferedWriter(new FileWriter(file));
           output.write(data);
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           try {
               output.close();
           } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
       }

   }

   public static void display(String fileName) {

       try {
           File file = new File(fileName);
           Scanner scanner = new Scanner(file);
           while (scanner.hasNextLine()) {
               System.out.println(scanner.nextLine());
           }
           scanner.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}

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