The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 4 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Microsoft Word - Lab_6.docxMicrosoft Word - Lab_6.docx
Files Lab Six in Java
Microsoft Word - Lab_6.docx
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 bestoredintoanarraylistofStudentobjects,intheStudentsclass. TheTesterclass 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;
Microsoft Word - Lab_6.docx
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
thestudentsarraylist. WillcallStudent'stoString()todothis. Forexample:
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, as demonstrated during the Week 13. Files lecture.
Writing the output file
Your program must use the PrintWriter class to save all its output to the output.txt file, as demonstrated during the Week 13. Files lecture. 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 aPrintWriterobject). TheStudentsobjectthencallsaseparateStudentsmethodto do each of the 6 different tasks below. You must design appropriate parameters and return values, in particular so that all program output to Terminal Window and output.txtisdonefrommain(). Inpseudocode:
+ 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
print the Student with the best GPA
calculate and print the average GPA
print the youngest Student who has a GPA below average print just the names of all the students
Hints
public static void main(String args[]) throws IOExceptionThis prevents a file handling syntax error: “unreported exception java.io.IOException; must be caught or declared to be thrown”
Microsoft Word - Lab_6.docx
Microsoft Word - Lab_6.docxSyntax 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
Microsoft Word - Lab_6.docx
System.out.printf("nGPAs above the average of %.2fn", gpa);
ot.printf("nGPAs above the average of %.2fn", gpa);
System.out.println(students.aboveAverage(gpa).toString());
ot.println(students.aboveAverage(gpa).toString());
Microsoft Word - Lab_6.docx
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));
}
Microsoft Word - Lab_6.docx
return aboveAverage; }
Microsoft Word - Lab_6.docx
public double getGPA()
{ return gpa; }
Microsoft Word - Lab_6.docx
public void add(Student s)
{
students.add(s);
}Microsoft Word - Lab_6.docx
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:
public Student bestStudent(~~~~~~)
public double averageGPA()