Question

2. The data file studentData.txt contains a list of students name, major, grade points, and credit hours. The number of stud

UK Write a program that reads the data from the data file, calculates the GPA (grade points/ credit hours), then saves the st

please solve this Java program. I need some explain.

2. The data file studentData.txt contains a list of student's name, major, grade points, and credit hours. The number of students is unknown. The data file is on D2L. studentData.txt Major Grade Points Credit Hours Name Jacob Michael Joshua Matthew Samantha CS Math CS Art Business 42 80 48 39 100 12 25 12 10 34
UK Write a program that reads the data from the data file, calculates the GPA (grade points/ credit hours), then saves the student information and GPA in another file (studentResult.txt). The program should also calculate and save the following data: - Average GPA of CS majors and the number of CS majors studentResult.txt Name Major Grade Points Credit Hours GPA Jacob Michael Joshua Matthew Samantha Elizabeth CS Math Cs Art Business CS 4 2 80 48 39 100 98 12 25 12 10 3 4 25 3.50 3.20 4.00 3.90 2.94 3.92 Brandon Christian Dylan Samuel cs Art Art Math 48 39 100 98 12 10 34 25 4.00 3.90 2.94 3.92 Average GPA of Cs students: 3.43 Number of CS Students: 14 - Use printf method to format the output with alignment, spacing, and decimal places. - Use java File, File Writer, PrintWriter objects for File IO. Do not use other types of /O objects. Do not use try and catch to handle exceptions 5
0 0
Add a comment Improve this question Transcribed image text
Answer #1

############### Student.java ###############
/**
* The Class Student.
*/
public class Student {

   /** The name. */
   private String name;

   /** The major. */
   private String major;

   /** The grade point. */
   private int gradePoint;

   /** The credit hours. */
   private int creditHours;

   /**
   * Instantiates a new student.
   *
   * @param name the name
   * @param major the major
   * @param gradePoint the grade point
   * @param creditHours the credit hours
   */
   public Student(String name, String major, int gradePoint, int creditHours) {
       super();
       this.name = name;
       this.major = major;
       this.gradePoint = gradePoint;
       this.creditHours = creditHours;
   }

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

   /**
   * Gets the major.
   *
   * @return the major
   */
   public String getMajor() {
       return major;
   }

   /**
   * Gets the grade point.
   *
   * @return the grade point
   */
   public int getGradePoint() {
       return gradePoint;
   }

   /**
   * Gets the credit hours.
   *
   * @return the credit hours
   */
   public int getCreditHours() {
       return creditHours;
   }

}

##################### GPACalculator.java #############

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
* The Class GPACalculator.
*/
public class GPACalculator {

   /** The students. */
   private List<Student> students;

   /**
   * Instantiates a new GPA calculator.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
   public GPACalculator() throws IOException {
       this.students = new ArrayList<>();
       readFile();
       calculateGPA();
   }

   /**
   * Read file.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
   private void readFile() throws IOException {
       System.out.println("****** File reading started *********");
       FileReader fr = new FileReader("studentdata.txt");
       BufferedReader br = new BufferedReader(fr);
       String line = br.readLine();
       while ((line = br.readLine()) != null) {
           String[] data = line.split(" ");
           String name = data[0];
           String major = data[1];
           int gradePoint = Integer.parseInt(data[2]);
           int creditHours = Integer.parseInt(data[3]);
           students.add(new Student(name, major, gradePoint, creditHours));
       }
       br.close();
       System.out.println("****** File reading finished *********");
   }

   /**
   * Calculate GPA.
   *
   * @throws FileNotFoundException the file not found exception
   */
   public void calculateGPA() throws FileNotFoundException {
       System.out.println("****** GPA calculation started *********");
       PrintWriter writer = new PrintWriter("studentresults.txt");
       double csGpa = 0.0;
       int cs = 0;
       writer.write("Name Major Grade_points Credit_hours GPA");
       writer.println();
       for (Student s : students) {
           double gpa = s.getGradePoint() / s.getCreditHours();
           if (s.getMajor().equalsIgnoreCase("cs")) {
               csGpa += gpa;
               cs++;
           }
           writer.write(String.format("%s %s %d %d %.2f", s.getName(), s.getMajor(), s.getGradePoint(), s.getCreditHours(), gpa));
           writer.println();
       }
       writer.println();
       writer.println();
       writer.write(String.format("Average GPA of CS Students: %.2f", (csGpa / cs)));
       writer.println();
       writer.write(String.format("Number of CS Students: %d", cs));
       writer.println();
       writer.flush();
       System.out.println("****** GPA calculation finished *********");
       writer.close();
   }

   /**
   * The main method.
   *
   * @param args the arguments
   * @throws IOException Signals that an I/O exception has occurred.
   */
   public static void main(String[] args) throws IOException {
       new GPACalculator();
   }
}

############# studentdata.txt ###########
Name Major Grade_point Credit_hours
Jacob CS 42 12
Michael Math 80 25
Joshua CS 48 12
Matthew Art 39 10
Samantha Business 100 34
Elizabeth CS 98 25
Brandon CS 48 12
Dylan Art 39 10
Samuel Art 100 34
Christian Math 98 25

############ studentresults.txt ############
Name Major Grade_points Credit_hours GPA
Jacob CS 42 12 3.00
Michael Math 80 25 3.00
Joshua CS 48 12 4.00
Matthew Art 39 10 3.00
Samantha Business 100 34 2.00
Elizabeth CS 98 25 3.00
Brandon CS 48 12 4.00
Dylan Art 39 10 3.00
Samuel Art 100 34 2.00
Christian Math 98 25 3.00


Average GPA of CS Students: 3.50
Number of CS Students: 4

Add a comment
Know the answer?
Add Answer to:
2. The data file studentData.txt contains a list of student's name, major, grade points, and cred...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written...

    Must be written in JAVA Code Write a program that will read in a file of student academic credit data and create a list of students on academic warning. The list of students on warning will be written to a file. Each line of the input file will contain the student name (a single String with no spaces), the number of semester hours earned (an integer), the total quality points earned (a double). The following shows part of a typical...

  • 1 and 2 1) For the data listed below as the questions (a) to (d). DATA:...

    1 and 2 1) For the data listed below as the questions (a) to (d). DATA: 20 25 40 41 42 43 44 45 46 47 48 49 50 50 51 52 53 54 55 56 57 58 59 60 65 80 90 Questions Points a)Make a frequency diagram with 7 classes b) Make a histogram c) Give the five number summary d) Make a box-and-whisker diagram (show outliers.) 2) At a gathering of 190 college students their grade-group and...

  • Add additional information to the student class below such as name, address, major, and contact information...

    Add additional information to the student class below such as name, address, major, and contact information along with all the getters and setters (methods) needed to access all data. Submit your code along with a sample run of your program. Comment all your code. Cite any sources of information you use. Write a note on the process of completing the assignment in a Microsoft Word document. // ShowStudent.java // client to test the Student class class ShowStudent { public static...

  • Add additional information to the student class below such as name, address, major, and contact information...

    Add additional information to the student class below such as name, address, major, and contact information along with all the getters and setters (methods) needed to access all data. Submit your code along with a sample run of your program. Comment all your code. Cite any sources of information you use. Write a note on the process of completing the assignment in a Microsoft Word document. // ShowStudent.java // client to test the Student class class ShowStudent { public static...

  • 8. -/2 points ASWSBE13 3.E.016.MI. My Notes The grade point average for college students is based...

    8. -/2 points ASWSBE13 3.E.016.MI. My Notes The grade point average for college students is based on a weighted mean computation. For most colleges, the grades are given the following data value A (4), (3), (0). Alter 60 credit hours of course work, a student at State University and credit hours of A, 10 credit hours of B, 2 credit hours of C, and 4 credit hours of D. (a) Compute the student's grade point average Ask Your Teacher (2),...

  • Use C++ For this week’s lab you will write a program to read a data file...

    Use C++ For this week’s lab you will write a program to read a data file containing numerical values, one per line. The program should compute average of the numbers and also find the smallest and the largest value in the file. You may assume that the data file will have EXACTLY 100 integer values in it. Process all the values out of the data file. Show the average, smallest, largest, and the name of the data file in the...

  • The data from data421.dat contains information on 78 seventh-grade students. We want to know how well...

    The data from data421.dat contains information on 78 seventh-grade students. We want to know how well each of IQ score and self-concept score predicts GPA using least-squares regression. We also want to know which of these explanatory variables predicts GPA better. Give numerical measures that answer these questions. (Round your answers to three decimal places.) find:(Regressor: IQ) R 2 find: (Regressor: Self-Concept) R 2 obs gpa iq gender concept 1 7.94 103 2 54 2 8.292 111 2 73 3...

  • Pls both parts for UPVOTE Using Excel create four histograms, properly labeled, depicting the data from...

    Pls both parts for UPVOTE Using Excel create four histograms, properly labeled, depicting the data from the frequency distributions in #a and #b below. a) Using the variable Age, construct TWO frequency distributions to summarize the Ages of students born in the USA and those born outside the USA. Use 5 classes to construct your frequency distribution. For each frequency distribution, show your calculations for the class width and then list the: Lower limits and upper limits Class boundaries Class...

  • The data from data349.dat contains information on 78 seventh-grade students. We want to know how well each of IQ score a...

    The data from data349.dat contains information on 78 seventh-grade students. We want to know how well each of IQ score and self-concept score predicts GPA using least-squares regression. We also want to know which of these explanatory variables predicts GPA better. Give numerical measures that answer these questions. (Round your answers to three decimal places.) (Regressor: IQ) R 2 (Regressor: Self-Concept) R 2 Which variable is the better predictor? IQ Self Concept obs   gpa   iq   gender   concept 1   7.94   112  ...

  • File Ed Yew ranoform e Srepha Anaivza Custom Hours Work GPA Absences yar ed less than...

    File Ed Yew ranoform e Srepha Anaivza Custom Hours Work GPA Absences yar ed less than 1... 3.00 4.00 less than 1.. 2 4.00 2.00 less than 1... 2.50 3.00 less than 1.. 3.80 1.00 less than 1.. 2.70 2.00 less than 1. 3.20 5.00 7 less than 1. 4.00 6.00 less than 1.. 8 3.80 7.00 less than 1... 2.90 9.00 10 less than 1... 3.10 4.00 11 less than 1. 3.10 7.00 12 less than 1... 2.00 8.00...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT