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

You are given a text file called \'Students.txt\' that contains information on m

ID: 3577875 • Letter: Y

Question

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.

Students.txt file format (file information at bottom)

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:

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...

it is essential that you draw pictures of the objects involved in your program, so that you are always aware of the data type you are working with.

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)

Syntax for processing an array list of objects

the syntax for processing an array list of Student objects is exactly as you would expect. For example, here’s a method that prints all the Students with GPAs above the average

first, the method call in main(). Since we must do all printing from main(), we design aboveAverage() to return a new Students object:

System.out.printf(" GPAs above the average of %.2f ", gpa); ot.printf(" GPAs above the average of %.2f ", gpa); System.out.println(students.aboveAverage(gpa).toString()); ot.println(students.aboveAverage(gpa).toString());

since aboveAverage() deals with many students, it would be part of the Students class. It creates a new Students object in which to return many Student objects:

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;

}

see that it calls a method getGPA(), that must directly return the GPA of a student. So getGPA() would be part of the Student class

public double getGPA()

{

return gpa;

}

it also calls a method add(), that takes a Student object and adds it to a Students object. So add() would be part of the Students class:

public void add(Student s)

{

students.add(s);

}

Designing method return types

the natural unit of an object-oriented program is an object. So methods returning results tend to return entire objects. Some hypothetical examples to illustrate this:

e.g. bestStudent() returns a single student, so would return a Student object. Method header would be something like: public Student bestStudent(~~~~~~)

e.g. aboveAverage() returns many students, so would return a Students object. Method header would be something like: public Students aboveAverage(~~~~~~)

(of course methods can also return non-object data types if that is appropriate) e.g. averageGPA() returns the average GPA, so method header would be something like: public double averageGPA()

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.

Students.txt file is here below because I did not know how to attach a file.

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

Solution:

Student.java

package studentinfo;


public class Student {

private String name;
  
private double gpa;
  
private int age;
  
public Student(String name,int age,double gpa){
      
this.name = name;
      
this.gpa = gpa;
      
this.age = age;
  
}
  
  
//getter methods
  
public int getAge(){
      
return this.age;
  
}
  
  
public double getGpa(){
      
return this.gpa;
  
}
  
  
public String getName(){
      
return this.name;
  
}
  
public void setAge(int age){
  
this.age=age;
  
}
  
  
//setter methods
  
public void setGpa(double gpa){
      
this.gpa=gpa;
  
}
  
  
public void setName(String name){
      
this.name=name;;
  
}
  
  
  
//toString method
  
public String toString(){
      
return this.name +" "+this.age+" "+this.gpa;
  
}
}

Students.java

package studentinfo;


import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.Scanner;


public class Students {

  
private ArrayList<Student> students;
  
  
public Students(){
      
students = new ArrayList();
  
}
  
  
public ArrayList<Student> getList(){
      
return this.students;
  
}
  
  
//readFile and store in arraylist
   public void readFile() {
      
try{
      
Scanner scan = new Scanner(new File("Students.txt"));
      
while(scan.hasNextLine()){
          
String name = scan.next();
          
int age = scan.nextInt();
          
double gpa = scan.nextDouble();
          
//System.out.println(name+age+gpa);
          
Student stu = new Student(name,age,gpa);
          
this.students.add(stu);
      
}
      
}catch(Exception e){
          
System.out.println(e);
      
}
  
}
  
  
//return student with best GPA
  
public Student bestGpa(){
      
double maxGpa = 0;
      
Student stu = null;
      
for(Student s:this.students){
          
if(s.getGpa()>maxGpa){
              
maxGpa = s.getGpa();
              
stu = s;
          
}
      
}
      
return stu;
   }
  
  
//returns average GPA
  

public double avgGpa(){
      
double avg=0;
      
for(Student s:this.students){
          
avg = avg + s.getGpa();
      
}
      
avg = avg/this.students.size();
      
return avg;
   }
  
  
//return youngest student with below Average GPA
  
public Student youngBelowAvg(){
      
double avg = this.avgGpa();
      
Student stu = null;
      
int minAge = 1000;
      
      
for(Student s:this.students){
          
if(s.getGpa()<avg){
              
if(s.getAge()<minAge){
                  
stu = s;
              
}
          
}
      
}
      
return stu;
  
}
  
//return all names
  
public String allNames(){
      
String str = " ";
      
for(Student s:this.students){
          
str = str + s.getName()+" ";
      
}
      
return str;
  
}
  
  
public String toString(){
      
String str = "";
      
for(Student s:this.students){
      
str = str + s.getName()+" "+s.getAge()+" "+s.getGpa()+" ";
      
}
      
return str;
  
}

}

Tester.java

package coursehero;


import java.io.PrintWriter;

import java.util.ArrayList;


public class Tester
{

public static void main(String arg[])
{

Students student = new Students();
  
try{
  
PrintWriter writer = new PrintWriter("output.txt");

student.readFile();

System.out.println("Student with best GPA is: "+student.bestGpa());

writer.println("Student with best GPA is: "+student.bestGpa());
  
System.out.println("Average GPA is: "+student.avgGpa());
  
writer.println("Average GPA is: "+student.avgGpa());
  
System.out.println("Youngest student below average GPA is: "+student.youngBelowAvg());

writer.println("Youngest student below average GPA is: "+student.youngBelowAvg());

System.out.println(student.allNames());

writer.println(student.allNames());
  
writer.close();
}
catch(Exception e){
     
System.out.println(e);

}
}
}

Students.txt

Sam 22 3.5

Max 21 2.0

Simi 21 1.0

Mini 23 4.5

Output.txt

Student with best GPA is: Mini   23 4.5

Average GPA is: 2.75

Youngest student below average GPA is: Simi 21 1.0 Sam

Max

Simi

Mini

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