Write a program which will fill an array of a student class

Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the student. You will use PrintWriter to produce a well formatted report containing the Student name, student Id, the student average (2 decimal places) and the students letter grade.
Your report should have the appropriate headings and all of the columns should line up. Remember your program must compile or you will receive an automatic zero. I will give partial credit. I expect you to use the String.formatter in your program. Your class should contain all of the private data listed as well as methods to return the student average and the student letter grade.
The letter grades will be assigned as follows:
90-100 A80-89 B70-79 C60-69 Dbelow 60 fails. This is java.
Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the student. You will use PrintWriter to produce a well formatted report containing the Student name, student Id, the student average (2 decimal places) and the students letter grade.
Your report should have the appropriate headings and all of the columns should line up. Remember your program must compile or you will receive an automatic zero. I will give partial credit. I expect you to use the String.formatter in your program. Your class should contain all of the private data listed as well as methods to return the student average and the student letter grade.
The letter grades will be assigned as follows:
90-100 A80-89 B70-79 C60-69 Dbelow 60 fails. This is java.
Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the student. You will use PrintWriter to produce a well formatted report containing the Student name, student Id, the student average (2 decimal places) and the students letter grade.
Your report should have the appropriate headings and all of the columns should line up. Remember your program must compile or you will receive an automatic zero. I will give partial credit. I expect you to use the String.formatter in your program. Your class should contain all of the private data listed as well as methods to return the student average and the student letter grade.
The letter grades will be assigned as follows:
90-100 A80-89 B70-79 C60-69 Dbelow 60 fails. This is java.

Solution

import java.text.DecimalFormat;

public class Student {

   private String name;
   private int id;
   private int grades[] = new int[3];

   public Student(String name, int id, int[] grades) {
       super();
       this.name = name;
       this.id = id;
       this.grades = grades;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the id
   */
   public int getId() {
       return id;
   }

   /**
   * @param id
   * the id to set
   */
   public void setId(int id) {
       this.id = id;
   }

   /**
   * @return the grades
   */
   public int[] getGrades() {
       return grades;
   }

   /**
   * @param grades
   * the grades to set
   */
   public void setGrades(int[] grades) {
       this.grades = grades;
   }

   public double getAverage() {

       return (grades[0] + grades[1] + grades[2]) / (double) 3;

   }

   public char getLetterGrade() {
       double avg = getAverage();
       if (avg <= 100 && avg >= 90)
           return \'A\';
       else if (avg <= 89 && avg >= 80)
           return \'B\';
       else if (avg <= 79 && avg >= 70)
           return \'C\';
       else if (avg <= 69 && avg >= 60)
           return \'D\';
       else
           return \'F\';
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       DecimalFormat format = new DecimalFormat(\"#.00\");
       return name + \"\\t\" + id + \"\\t\" + format.format(getAverage()) + \"\\t\"
               + getLetterGrade();
   }

}

import java.io.File;
import java.util.Scanner;

public class TestStudent {
   public static void main(String[] args) {
       Scanner scanner = null;
       try {

           Student[] studentArr = new Student[3];
           scanner = new Scanner(new File(\"student.txt\"));

           int count = 0;
           while (scanner.hasNext()) {
               String name = scanner.next();
               int id = scanner.nextInt();
               int grades[] = new int[3];
               grades[0] = scanner.nextInt();
               grades[1] = scanner.nextInt();
               grades[2] = scanner.nextInt();
               studentArr[count++] = new Student(name, id, grades);

           }
           System.out.println(\"Name\\tID\\tAverage\\tGrade\");
           for (int i = 0; i < studentArr.length; i++) {
               System.out.println(studentArr[i]);
           }

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }
}

student.txt

srinu 223 60 70 80
rajesh 224 50 80 90
pavan 225 80 80 90

OUTPUT:

Name   ID   Average   Grade
srinu   223   70.00   C
rajesh   224   73.33   C
pavan   225   83.33   B

 Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the stude
 Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the stude
 Write a program which will fill an array of a student class from a file. The file will contain the student name, student id and three test grades for the stude

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site