Using basic knowledge of Java complete the following: Attached is a data file co
ID: 3712015 • Letter: U
Question
Using basic knowledge of Java complete the following:
Attached is a data file containing student information named Students.txt. The info includes a first name, phone number, email and gpa (separated by comma). You are supposed to write a program, that:
1.) creates a Student class to store first name, phone number, email and gpa.
2.) reads all of the lines in “Students.txt”; creates a Student object for each line
3.) saves all the Student objects in a Student Array
4.) loop through the Student array to display how many students have a 3.0 gpa or higher
Hints:
1) Each line read from Students.txt can be spitted by using:
String[] values = line.split(",") ;
2) The following shows how to convert from String to double
double gpa = Double.parseDouble(gpaString);
There is a lot of data in the text file that is formatted as such:
Which is Name,StudentID,Email,GPA
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class StudentFileProcess {
class Student {
String firstName;
String phoneNumber;
String email;
double gpa;
/**
* @param firstName
* @param phoneNumber
* @param email
* @param gpa
*/
public Student(String firstName, String phoneNumber, String email, double gpa) {
super();
this.firstName = firstName;
this.phoneNumber = phoneNumber;
this.email = email;
this.gpa = gpa;
}
}
public static void main(String args[]) {
Student[] studentArray = new Student[10000];
int countStud = 0;
Scanner fileReader = new Scanner(new File("Students.txt"));
while (fileReader.hasNextLine()) {
String student = fileReader.nextLine();
String[] studArray = student.split(",");
double gpa = Double.parseDouble(studArray[3]);
Student stud = new StudentFileProcess().new Student(studArray[0], studArray[1], studArray[2], gpa);
studentArray[countStud++] = stud;
}
int countGpaGr3 = 0;
for (int i = 0; i < countStud; i++) {
if (studentArray[i].gpa >= 3.0) {
countGpaGr3++;
}
}
String output = String.format("There are totally %d students having GSP greater than %.2f", countGpaGr3, 3.0);
System.out.println(output);
}
}
Output:
Program tested with 10 records in input file. Program can handle even 10000 records
There are totally 5 students having GSP greater than 3.00
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.