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

Purpose Purpose is to practice using file input and output, and array list of ob

ID: 3842012 • Letter: P

Question

Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'students txt' that contains information on many students. Your program reads the file, creating 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: 22 1.2 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:

Explanation / Answer

CODE:

Student.java


public class Student {
   private String name;
   private int age;
   private double gpa;
  
   public Student(String name, int age, double gpa) {
       super();
       this.name = name;
       this.age = age;
       this.gpa = gpa;
   }
  
   @Override
   public String toString(){
       String retval = name + " " + age + " " + gpa;
       return retval;
   }

   public String getName() {
       return name;
   }

   public int getAge() {
       return age;
   }

   public double getGpa() {
       return gpa;
   }
  
}

----------------------------------------------------------------------------------------------------------

Students.java

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

public class Students {
   public ArrayList<Student> students;
  
   public Students() {
       students = new ArrayList<>();
   }
  
   public void readFile() throws FileNotFoundException{
       Scanner sc = new Scanner(new File("H:\eclipse_workspace\Test\StudentProject\src\Students.txt"));
       while (sc.hasNextLine()) {
           String name = sc.nextLine();
           int age = Integer.parseInt(sc.nextLine());
           double gpa = Double.parseDouble(sc.nextLine());
           System.out.println(name + " " + age + " " + gpa);
           Student stdnt = new Student(name, age, gpa);
           students.add(stdnt);
       }      
       sc.close();
   }

   @Override
   public String toString() {
       int numOfStudents = students.size();
       String retVal = null;
      
       for(int i=0; i<numOfStudents; i++){
           Student temp = students.get(i);
           retVal += temp.toString() + " ";
       }
       return retVal;
   }
  
   public Student bestStudent(){
       int numOfStudents = students.size();
       Student retVal = null;
       int gpa = -1;
       for(int i=0; i<numOfStudents; i++){
           Student temp = students.get(i);
           if(temp.getGpa() > gpa){
               retVal = temp;
           }
       }
       return retVal;
   }
  
   public void add(Student s){
       students.add(s);
   }
  
  
   public Students aboveAverage(double avgGPA){
       Students aboveAverage = new Students();
      
       for(int i=0;i<students.size();i++){
           if(students.get(i).getGpa() > avgGPA){
               aboveAverage.add(students.get(i));
           }
       }
       return aboveAverage;
   }
  
   public double getAverageGPA(){
       double avgGPA = 0;
       double totalGPA = 0;
       for(int i=0;i<students.size();i++){
           totalGPA += students.get(i).getGpa();
       }
      
       avgGPA = totalGPA/students.size();
       return avgGPA;
   }
}

------------------------------------------------------------------

Tester.java

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class Tester {

   public static void main(String[] args) throws IOException {
       Students sdnts = new Students();
       PrintWriter writer = new PrintWriter(System.out);
       PrintWriter writer1 = new PrintWriter(new File("output.txt"));
      
       sdnts.readFile();
       writer1.flush();
       //writer.write(sdnts.toString());
       writer1.write(sdnts.toString());
       writer1.write(" ");
       writer1.write("Student with Best GPA");
       writer1.write(sdnts.bestStudent().getName());
      
       writer1.close();
       writer.close();
   }

}

-----

Please include all the files in the same path. And do change the path for the input file in the Students class code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote