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

Create a Student class which will contain the following A String object to hold

ID: 3762305 • Letter: C

Question

Create a Student class which will contain the following

A String object to hold a student’s first name

A String object to hold a student’s last name

An array object to hold four integers representing student’s grades in four different courses

A method to read the student data from a file. The student data in the file is formatted as follows (one line per student):

first name, a space, last name, a space, four integer values separated by space

A method to write to a file after the converting the numeric grade to letter grade

90 – 99: A

80 – 89: B

70 – 79: C

60 – 69: D

< 60     : F

Write a line for each student using the following format

Student’s first name in 20 characters, a space, last name in 10 characters, a space

Student’s letter grades separated by space

For students, the data in the input file is recorded as follows:

John Smith 93 66 87 58

Peter Pan 86 78 95 83

The output in the file for these students should look like

John                 Smith      A D B F

Peter                Pan          B C A B

Explanation / Answer

/**
* @author srinu
* can be used to hold the data
*/
public class Student {
   String firstName, lastName;

   int grades[] = new int[4];

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public int[] getGrades() {
       return grades;
   }

   public void setGrades(int[] grades) {
       this.grades = grades;
   }

  
   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

}

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

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

   public static void main(String arg[]){
       ArrayList<Student> students=setStudents();
      
       writeStudent(students);
      
      
   }
   /**
   * @param students
   */
   public static void writeStudent(ArrayList<Student> students){
      
       try {

          

           File file = new File("studentgrades.txt");

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
          
           Iterator<Student> it=students.iterator();
           while(it.hasNext()){
               Student student=it.next();
               int grades[]=student.getGrades();
               String content = student.getFirstName()+" "+student.getLastName()+" "+caliculatGrade(grades[0])+" "+caliculatGrade(grades[1])+" "+caliculatGrade(grades[2])+" "+caliculatGrade(grades[3])+" ";
               System.out.print(content);
              
              
               bw.write(content);
           }
          
           bw.close();

           //System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   /**
   * @param grade
   * @return
   */
   public static char caliculatGrade(int grade){
      
       /*90 – 99: A
       80 – 89: B
       70 – 79: C
       60 – 69: D
       < 60 : F*/
      
       if(grade>=90&&grade<=99){
           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';
       }
      
   }
   /**
   * @return
   */
   public static ArrayList<Student> setStudents(){
       ArrayList<Student> students=new ArrayList<Student>();
       // The name of the file to open.
String fileName = "studentdata.txt";

// This will reference one line at a time
String line = null;

try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);

// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);

while((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
String lineArray[]=line.split(" ");
Student student=new Student();
student.setFirstName(lineArray[0]);
student.setLastName(lineArray[1]);
int grades[]=new int[4];
grades[0]=Integer.parseInt(lineArray[2]);
grades[1]=Integer.parseInt(lineArray[3]);
grades[2]=Integer.parseInt(lineArray[4]);
grades[3]=Integer.parseInt(lineArray[5]);
  
student.setGrades(grades);
students.add(student);
}   

// Always close files.
bufferedReader.close();   
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
  
      
       return students;
      
   }
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

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

   public static void main(String arg[]) {
       ArrayList<Student> students = setStudents();

       writeStudent(students);

   }

   /**
   * @param students
   */
   public static void writeStudent(ArrayList<Student> students) {

       try {

           File file = new File("studentgrades.txt");

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);

           Iterator<Student> it = students.iterator();
           while (it.hasNext()) {
               Student student = it.next();
               int grades[] = student.getGrades();
               String content = student.getFirstName() + " "
                       + student.getLastName() + " "
                       + caliculatGrade(grades[0]) + " "
                       + caliculatGrade(grades[1]) + " "
                       + caliculatGrade(grades[2]) + " "
                       + caliculatGrade(grades[3]) + " ";
               System.out.print(content);

               bw.write(content);
           }

           bw.close();

           // System.out.println("Done");

       } catch (IOException e) {
           e.printStackTrace();
       }
   }

   /**
   * @param grade
   * @return
   */
   public static char caliculatGrade(int grade) {

       /*
       * 90 – 99: A 80 – 89: B 70 – 79: C 60 – 69: D < 60 : F
       */

       if (grade >= 90 && grade <= 99) {
           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';
       }

   }

   /**
   * @return
   */
   public static ArrayList<Student> setStudents() {
       ArrayList<Student> students = new ArrayList<Student>();
       // The name of the file to open.
       String fileName = "studentdata.txt";

       // This will reference one line at a time
       String line = null;

       try {
           // FileReader reads text files in the default encoding.
           FileReader fileReader = new FileReader(fileName);

           // Always wrap FileReader in BufferedReader.
           BufferedReader bufferedReader = new BufferedReader(fileReader);

           while ((line = bufferedReader.readLine()) != null) {
               // System.out.println(line);
               String lineArray[] = line.split(" ");
               Student student = new Student();
               student.setFirstName(lineArray[0]);
               student.setLastName(lineArray[1]);
               int grades[] = new int[4];
               grades[0] = Integer.parseInt(lineArray[2]);
               grades[1] = Integer.parseInt(lineArray[3]);
               grades[2] = Integer.parseInt(lineArray[4]);
               grades[3] = Integer.parseInt(lineArray[5]);

               student.setGrades(grades);
               students.add(student);
           }

           // Always close files.
           bufferedReader.close();
       } catch (FileNotFoundException ex) {
           System.out.println("Unable to open file '" + fileName + "'");
       } catch (IOException ex) {
           System.out.println("Error reading file '" + fileName + "'");
           // Or we could just do this:
           // ex.printStackTrace();
       }

       return students;

   }
}

studentdata.txt:

srinivas palli 10 20 30 95
akshayasri palli 69 88 97 43
madhavi gudivada 89 45 67 54

studentgrades.txt

srinivas palli F F F A
akshayasri palli D B A F
madhavi gudivada B F D F

output :

srinivas palli F F F A
akshayasri palli D B A F
madhavi gudivada B F D F