Write code that: Below is a \'Students.txt\' file that contains information on m
ID: 3576109 • Letter: W
Question
Write code that:
Below is a 'Students.txt' file that contains information on many students. Your programmust read the file, create many Student objects, all of which will be stored into an array list of Student objects, in the Students class. The Tester class controls everything, calling many Students class methods to produce lots of different outputs. The program must write the output to an output file and to the Terminal Window.
File and class specifications
Students.txt file format
Information for each student is stored in the file as 3 lines of text:
name
age
GPA
e.g. the following shows data for two students:
Name0
22
1.2
Name1
22
2.71
Student class
The Student class has instance variables and methods to represent one single student. Your Student class must have exactly and only the following instance variables: private
String name;
private int age;
private double gpa;
Design appropriate constructors and other methods for your Student class, including:
+ toString() – returns a String containing the 3 instance variables e.g.
Name0
22
1.2
Students class
Very importantly, the Students class is used to store and process many Student objects. It will have an instance variable to store many Student objects. Methods intended to process many Student objects belong in this Students class.
Your Students class must have exactly and only the following instance variable:
private ArrayList<Student> students;
students here is an array list of Student objects, in which all of the Student objects are stored.
Students must have appropriate constructors and methods, including the following:
+ readFile() – opens the data file, reads the data, creates Student objects, and adds them to the students array list
+ toString() – returns a String containing a line of information for each Student in the students array list. Must call the Student class’s toString() as it builds the big String. Example of output:
Name0 22 1.2
Name1 22 2.71
. . .
Many other methods for processing a Students object. Most of the code you write will be in this class.
Reading the data file
Your program will use the Scanner class to read from the data file.
Writing the output file
Your program must use the PrintWriter class to save all its output to the output.txt file. It will also send the same output to the BlueJ Terminal Window, as usual.
Tester class
The Tester class controls everything. Tester does not have any instance variables. You have to write the Tester class.
Tester contains only a main() method, which first creates a single Students object and a PrintWriter object. The Students object then calls a separate Students method to do each of the 5 different tasks below. You must design appropriate parameters and return values, in particular so that all program output to Terminal Window and output.txt is done from main(). In pseudocode:
+ main()
create an empty Students object create a new PrintWriter object to create the ‘output.txt’ output file
read data file into Students
print all Student objects from Students (must call the Students class toString() method to do this)
print the Student with the best GPA
calculate and print the average GPA
print the youngest Student who has a GPA below average
Hints
the Students.txt data file is attached below.. You must copy it into your Lab 6 BlueJ project folder
you will need to add throws IOException to your Tester class main() method header and the Students class readFile() method header. For example, in Tester:
public static void main(String args[]) throws IOException
This prevents a file handling syntax error: “unreported exception java.io.IOException; must be caught or declared to be thrown”
it is important in this lab always to keep in mind that each element in the Students class array list is itself an entire Student object...
the 5 different actions above will each be implemented as a separate public Students method, called from main() by the Students object, using appropriate parameters and return types
(by the way, there is no inheritance in this lab. So no standard formatting for inheritance in the toString() methods)
Required
your program must work for a file containing any number of students
you are required this time to use PrintWriter to create your output.txt output file. Cannot just save the Terminal Window output as usual
your program must clearly label each part of the output e.g. "Student with best GPA is:", "Average GPA is: ", "Youngest student below average GPA is:", etc
use good programming practices regarding encapsulation of class instance variables i.e. all must be declared private as shown above
every class and method is required to have a clear, meaningful Javadoc comment. You will lose points otherwise.
I do not think I am able to link a file, so here is the students.txt file information:
Name0
22
1.2
Name1
22
2.71
Name2
19
3.51
Name3
18
3.91
Name4
20
1.6
Name5
19
1.03
Name6
18
3.78
Name7
19
3.19
Name8
18
2.37
Name9
21
1.01
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Student{
private String name;
private int age;
private double gpa;
Student(String name,int age, double gpa){
this.name = name;
this.age = age;
this.gpa = gpa;
}
public String toString(){
System.out.println(this.name + " " + this.age + " " + this.gpa);
}
}
class Students{
private ArrayList<Student> students;
Students(){
students = new ArrayList<Student>();
}
public void readFile() throws IOException
{
File data_file = new File("Absolute_File_Path_To_Students_File/Students.txt");
Scanner sc = new Scanner(data_file);
while(sc.hasNextLine()){
String studentName = sc.next();
int studentAge = sc.nextInt();
double studentGPA = sc.nextDouble();
students.add(Student(studentName,studentAge,studentGPA));
}
}
public String toString(){
Iterator<Student> itr = students.iterator();
String resultString = "";
while(itr.hasNext()){
Student s = itr.next();
resultString += s.toString() + ' ';
}
return resultString;
}
public String bestGPA(){
Iterator<Student> itr = students.iterator();
double bestGPA = 0;
Student bestGPAStudent;
while(itr.hasNext()){
Student s = itr.next();
if (s.gpa > bestGPA){
bestGPA = s.gpa;
bestGPAStudent = s;
}
}
return bestGPAStudent.toString();
}
public double AverageGPA(){
Iterator<Student> itr = students.iterator();
double SumOfGPA = 0;
while(itr.hasNext()){
Student s = itr.next();
SumOfGPA += s.gpa;
}
return SumOfGPA/students.size();
}
public String youngest_student_below_AverageGPA(){
Iterator<Student> itr = students.iterator();
double SumOfGPA = 0;
while(itr.hasNext()){
Student s = itr.next();
SumOfGPA += s.gpa;
}
double avgGPA = SumOfGPA/students.size();
int youngAge = 999;//random value
Student youngStudentBelowAvgGPA;
while(itr.hasNext()){
Student s = itr.next();
if(s.age < youngAge && s.gpa < avgGPA){
youngAge = s.age;
youngStudentBelowAvgGPA = s;
}
}
return youngStudentBelowAvgGPA.toString();
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Tester
{
public static void main (String[] args) throws IOException
{
Students s = new Students();
s.readFile();
File outputFile = new File("Absolute_Path_To_Create_Output_File/output.txt");
PrintWriter printObj = new PrintWriter(outputFile);
printObj.println(s.toString());
printObj.println("Student with best GPA is:" + s.bestGPA());
printObj.println("Average GPA is: "+ s.AverageGPA());
printObj.println("Youngest student below average GPA is:"+ s.youngest_student_below_AverageGPA());
System.out.println(s.toString());
System.out.println("Student with best GPA is:", s.bestGPA());
System.out.println("Average GPA is: ",s.AverageGPA());
System.out.println("Youngest student below average GPA is:",s.youngest_student_below_AverageGPA());
printObj.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.