2 Tasks 21 The main class The main class that runs the whole
2 Tasks
2.1 The “main” class
The main class that runs the whole code is “Main.java”. This class is responsible for reading the
input file, creating the object that maintains the three arrays, and then uses this object to print
information and filter students based on GPA.
The path to the input file must be provided as the value of the constant INPUT FILE PATH first.
The code for Main.java is incomplete, and your first task is complete it. After reading the data
from the input file, the program asks the user to provide a GPA value as a cutoff for enrollment.
Completing this is a part of your assignment. This is VERY important, because without this, the
next part of the program cannot be tested! The incomplete Main.java is shown next.
1
Main.java
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
public class Main {
private static final String INPUT_FILE_PATH = \"/path/to/input/file\";
public static void main(String[] args)
throws URISyntaxException, IOException, InputFileFormatException {
File inFile = new File(INPUT_FILE_PATH);
InputDataHandler handler = new InputDataHandler(inFile);
handler.printAllDetails();
System.out.print(\"Enter the GPA cutoff for the course: \");
// TODO: add your code to read user input
handler.filter(cutoff);
handler.printAllDetails();
}
}
Task Goals
• The cutoff GPA provided by the user should be deemed invalid if it is lower than 1.0 or higher
than 4.0. It should, of course, be deemed invalid if it anything other than a double.
• If the user enters an invalid cutoff GPA value, your code should print a meaningful error
message and ask for the user input again.
2.2 Processing input data and the “InputDataHandler” class
This is where all the data handling and processing happens. Your job here is to write
• the constructor for this class,
• the printAllDetails() method,
• the deleteStudent(int) method, and
• a Java documentation in the same style as shown in the provided code to explain what could
go wrong in the filter(double) method (i.e., why it may fail to remove students whose
GPA is below the cutoff provided by the user).
All three tasks must adhere to the documentation provided in the code.
InputDataHandler.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
class InputDataHandler {
private String[] nameArray;
private Double[] gpaArray;
private Integer[] idArray;
// TODO: write Javadoc explaining why this approach of filtering may fail
void filter(double cutoff) {
for (int i = 0; i < gpaArray.length; i++) {
if (gpaArray[i] < cutoff)
deleteStudent(i);
}
}
/**
* The only constructor for this class. It takes an input file as its argument,
* and populates the three arrays such that the n’th line’s data is stored in
* the (n-1)’th index of all three arrays. It throws an IOException
* if the inputFile is not a file (e.g., it’s a directory, symbolic link,
* etc.). It also throws an InputFileFormatException if the input file
* contains lines that are not properly formatted or if it contains a line with
* an invalid value for the GPA.
*
* @param inputFile the input file with 3 columns of tab-separated data in each line.
* @throws IOException if there is any problem reading the input file.
* @throws InputFileFormatException if there is any formatting problem in the
* input file.
*/
InputDataHandler(File inputFile) throws IOException, InputFileFormatException {
// TODO: add your code
}
/**
* Prints out the details of all the students in this class.
* This must be in the following format:
*
* Name : name of student
* GPA : GPA of student
* Student ID: ID of student
* There must also be a one-line gap between two students.
*/
void printAllDetails() {
// TODO: add your code
}
/**
* This method removes all the information about a student whose information
* is stored at the specified index (in all three arrays). It also ensures
* that after the removal, all three arrays in the InputDataHandler
* class have their size reduced by one, and subsequent student information
CSE 214 Homework I Submission Deadline: Feb 5, 2017
* is shifted to the left by one.
*
* @param index the specified index where data is removed.
private void deleteStudent(int index) {
// TODO: add your code
}
}
Solution
GPA.txt
James 6.7 101
Jack 5.8 103
Jane 2.4 104
Peter 8.2 105
Paul 3.4 107
InputDataHandler.java
package Feburary;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class InputDataHandler {
public String[] nameArray;
public Double[] gpaArray;
public Integer[] idArray;
File file;
FileReader fr;
BufferedReader br;
StringBuffer sb = new StringBuffer();
ArrayList al1,al2,al3;
public InputDataHandler(File file) throws IOException{
this.file = file;
fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
int i=0;
while((line=br.readLine())!=null){
sb.append(line).append(\"\ \");
i++;
}
nameArray = new String[i];
gpaArray = new Double[i];
idArray = new Integer[i];
int j=0;
String d[] = sb.toString().split(\"\ \");
while(d.length>j){
String dt[] = d[j].split(\"\\t\");
nameArray[j] = dt[0];
gpaArray[j] = Double.parseDouble(dt[1]);
idArray[j] = Integer.parseInt(dt[2]);
j++;
}
al1 = new ArrayList();
al2 = new ArrayList();
al3 = new ArrayList();
for(int k=0;k<nameArray.length;k++){
al1.add(nameArray[k]);
al2.add(gpaArray[k]);
al3.add(idArray[k]);
}
}
void filter(double cutoff) throws IOException{
if(cutoff <1.0 || cutoff>4.0){
System.out.println(\"Invalid Cutoff\");
}else {
for (int i = 0; i < gpaArray.length; i++) {
if (gpaArray[i] < cutoff)
deleteStudent(i);
}
}
}
public void deleteStudent(int index) throws IOException{
al1.remove(nameArray[index]);
al2.remove(gpaArray[index]);
al3.remove(idArray[index]);
}
public void printAllDetails() throws IOException{
System.out.println(\"The details are:\");
System.out.println(al1);
System.out.println(al2);
System.out.println(al3);
}
}
Main.java
class Main {
public static void main(String args[]) throws IOException {
Scanner s = new Scanner(System.in);
File file = new File(\"D:\\\\GPA.txt\");
InputDataHandler handler = new InputDataHandler(file);
handler.printAllDetails();
System.out.print(\"Enter the GPA cutoff for the course: \");
double cutoff;
cutoff = s.nextDouble();
handler.filter(cutoff);
handler.printAllDetails();
}
}
Output:
The details are:
[James, Jack, Jane, Peter, Paul]
[6.7, 5.8, 2.4, 8.2, 3.4]
[101, 103, 104, 105, 107]
Enter the GPA cutoff for the course: 3.9
The details are:
[James, Jack, Peter]
[6.7, 5.8, 8.2]
[101, 103, 105]



