Question
Modify the StudentRecordManager class provided for Unit 1 project to take advantage of composition design.For grading purposes, use the StudentRecordManager.java file provided to you, not
Modify the StudentRecordManager class provided for Unit 1 project to take advantage of composition design.For grading purposes, use the StudentRecordManager.java file provided to you, not the one with your own work. Do not fill in #ADD portions.
You still need other provided files for this project i.e. Student class and the data folder.
After modification, the StudentRecordManager class should look like this (red means private):
- recListis a private instance data member now;
- The constructor should just initializerecListwithloadFromFile()result;
- displayRecords()is a public instance method (not private, not static) now and doesn't take any parameters as the data it needs is in instance data memberrecList;
- size()should just report # of records as now users of this class don't have access to the arraylist.
- Do not touchloadFromFile().
- Put thismain()directly inStudentRecordManagerclass. This project should compile and work the same as before (the given project portion in Unit 1).Include a screenshot of your program outputin assignment report.
public static void main(String[] args) { // path and file name of data file String fileName = "data/cs219.txt"; StudentRecordManager roster = new StudentRecordManager(fileName); roster.displayRecords(); // display data in table format // simple statistics System.out.println("Number of students: " + roster.size()); } // end main
// Student.java (complete)
// CS219 HW1 Project
// represents a student record
//
// Author: CS219
public class Student
{
// instance data members
private String name;
private int absenceCount = 0;
// constructor
public Student(String name, int absenceCount)
{
this.name = name;
this.absenceCount = absenceCount;
}
// accessors
public String getName() { return name; }
public int getAbsenceCount() { return absenceCount; }
} // end class Student
// StudentRecordManager.java (incomplete)
// CS219 HW1 Project
// (Entry point of the project. Need to use with Student class)
//
// Manages student records using an ArrayList of Student.
// - Load student record data from file in constructor
// - display raw records and some statistics
// java.io classes, used for file i/o
import java.io.File;
import java.io.IOException;
import java.util.Scanner; // I/O methods
import java.util.ArrayList;
public class StudentRecordManager
{
public static void main(String[] args)
{
ArrayList
// path and file name of data file
String fileName = "data/cs219.txt";
// This "cs219.txt" file should already exist in a folder named "data".
// If using an IDE and source code file is put in a default "src" folder, folder "data" should be at the same location as the "src" folder;
// otherwise, "data" folder should be at the same location as your .java file
recList = loadFromFile(fileName);
displayRecords(recList);
System.out.println("Number of students: " + recList.size());
//if (recList.size() > 0)
//System.out.printf("Avg AbsenceCount: %.1f%n", avgAbsenceCount(recList));
} // end main
// display student records on screen
private static void displayRecords(ArrayList
{
if (records == null || records.size() == 0) // null or empty
{
System.out.println("Empty class. No data to show");
return; // exit method now
}
// has some records
System.out.println("-----------------------------------");
// ADD #1
// END ADD #1
System.out.println("-----------------------------------");
} // end displayRecords
// calculate and return average absence count of all students
// ADD #2
// END ADD #2
// load student data from text files
/**
* Fills student record ArrayList with data from text file.
* columns are separated with comma ,
*
* @param fileNamename of input text file
* @returnnumber of records loaded
* @throws IOException: any exception during file opening, reading, and closing
*/
private static ArrayList
{
Scanner fileIn = null;// scanner object to connect to file
ArrayList
try
{
// open input file
fileIn = new Scanner(new File(fileName));
// skip first line (headings row)
fileIn.nextLine(); // read one line, but do not use
// loop to read multiple records
while (fileIn.hasNext()) // more lines to read?
{
// 1. read one line containing all columns of a record
String line = fileIn.nextLine();
if (line.isEmpty())// empty line: go read next line
continue;
// 2. extract name (1st column) and absence count (2nd column)
// 1st col is name, 2nd col is absence count: xxxx, dd
String name;
int absenceCount;
int toIndex = line.indexOf(',');// locate first comma ,
name = line.substring(0, toIndex); // from beginning to the char right before first comma ,
String absenceCountStr = line.substring(toIndex+1);// +1 to skip comma ,
absenceCount = Integer.parseInt(absenceCountStr.trim()); // trim() leading/trailing whitespace
// 3. create Student object and add to ArrayList records
records.add(new Student(name, absenceCount));
// end one record
}// end while: reading all records
}
catch (IOException ioe)
{
System.out.println("Error reading \"" + fileName + "\" file: " + ioe);
}
finally // close file
{
if ( fileIn != null)
{// close if was connected to a file
fileIn.close();
}
}
// end file input
return records;
}// end loadFromFile
} // end class StudentRecordManager
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started