Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need your assistance with my intro to programming homework assignment. Follow those steps to complete this project: Step 1. Create a Java project and

I need your assistance with my intro to programming homework assignment.

Follow those steps to complete this project:

Step 1. Create a Java project and add the given java files (see below) and data folder into the project.

The code should compile & run. Take a screenshot of the source code files and data folder on your local machine (screenshot #1). Take a second screenshot showing the results at this point (screenshot #2). Add both to your assignment report. The output should look exactly like this:

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

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

Number of students: 7

Step 2: Add the following code into the StudentRecordManager class to complete it. Read the comments.

  • displayRecords() method (ADD CODE #1): add code to print out all records like the output shown below. The exact space between columns doesnt matter, as long as the name column is left-aligned and number column right-aligned

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

StudentName AbsenceCount

Mary D. Brown 0

Tracy Smith 7

Muhammad Ali 4

Tracy A. Smith 12

Richard Young 0

Ben T. Johnson 1

Samuel Ebeling 8

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

  • Add an avgAbsencecount() method (ADD CODE #2) to calculate the average absence count of the class. Remember to uncomment the last if statement in main().

Once completed, take a screenshot of the execution result (screenshot #3) and add to your assignment report.

StudentRecordManager.java File:

// StudentRecordManager.java (incomplete)

// (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 recList = null; // declare an ArrayList obj

// 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); // load data into ArrayList obj

displayRecords(recList); // display data in table format

// simple statistics

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 records)

{

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 fileName name of input text file

* @return number of records loaded

* @throws IOException: any exception during file opening, reading, and closing

*/

private static ArrayList loadFromFile(String fileName)

{

Scanner fileIn = null; // scanner object to connect to file

ArrayList records = new ArrayList<>(); // initialize to empty list

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

Student.jave File:

// Student.java (complete)

// represents a student record

//

// Author: CHEGG

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

Text File:

StudentName, AbsenceCount

Mary D. Brown, 0

Tracy Smith, 7

Muhammad Ali, 4

Tracy A. Smith, 12

Richard Young, 0

Ben T. Johnson, 1

Samuel Ebeling, 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions

Question

A small segment of DNA is called a .

Answered: 1 week ago