For this lab, you will write the following files:
AbstractDataCalc
AverageDataCalc
MaximumDataCalc
MinimumDataCalc
MUST USE ALL 6 FILES PROVIDED
AbstractDataCalc is an abstract class, that AverageDataCalc, MaximumDataCalc and
MinimumDataCalc all inherit.
The following files are provided
CSVReader
To help read CSV Files.
DataSet
This file uses CSVReader to read the data into a List> type structure. Think of this as a Matrix
using ArrayLists. The important methods for you are rowCount() and getRow(int i) - Between
CSVReader and DataSet - all your data is loaded for you!
Main
This contains a public static void String[] args. You are very free to completely change this main
(and you should!). We don't test your main, but instead your methods directly. However, it will
give you an idea of how the following output is generated.
Sample Input / Output
Given the following CSV file
The output of the provided main is:
Note: the extra line between Set results is optional and not graded. All other spacing must be
exact!
Specifications
You will need to implement the following methods at a minimum. You are free to add additional
methods.
AbstractDataCalc
public AbstractDataCalc(DataSet set) - Your constructor that sets your dataset to an instance
variable, and runCalculations() based on the dataset if the passed in set is not null. (hint: call
setAndRun)
public void setAndRun(DataSet set) - sets the DataSet to an instance variable, and if the passed
in value is not null, runCalculations on that data
private void runCalculations() - as this is private, technically it is optional, but you are going to
want it (as compared to putting it all in setAndRun). This builds an array (or list) of doubles,
with an item for each row in the dataset. The item is the result returned from calcLine.
public String toString() - Override toString, so it generates the format seen above. Method is the
type returned from get type, row counting is the more human readable - starting at 1, instead of
0.
public abstract String getType() - see below
public abstract double calcLine(List line) - see below
AverageDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "AVERAGE"
public abstract double calcLine(List line) - runs through all items in the line and returns the
average value (sum / count).
MaximumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "MAX"
public abstract double calcLine(List line) - runs through all items, returning the largest item in
the list.
MinimumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "MIN"
public abstract double calcLine(List line) - runs through all items, returning the smallest item in
the list.
MaximumDataCalc.java------ write code .
For this lab, you will write the following filesAbstractDataCalc.pdf
1. For this lab, you will write the following files:
AbstractDataCalc
AverageDataCalc
MaximumDataCalc
MinimumDataCalc
MUST USE ALL 6 FILES PROVIDED
AbstractDataCalc is an abstract class, that AverageDataCalc, MaximumDataCalc and
MinimumDataCalc all inherit.
The following files are provided
CSVReader
To help read CSV Files.
DataSet
This file uses CSVReader to read the data into a List> type structure. Think of this as a Matrix
using ArrayLists. The important methods for you are rowCount() and getRow(int i) - Between
CSVReader and DataSet - all your data is loaded for you!
Main
This contains a public static void String[] args. You are very free to completely change this main
(and you should!). We don't test your main, but instead your methods directly. However, it will
give you an idea of how the following output is generated.
Sample Input / Output
Given the following CSV file
The output of the provided main is:
Note: the extra line between Set results is optional and not graded. All other spacing must be
exact!
Specifications
You will need to implement the following methods at a minimum. You are free to add additional
methods.
AbstractDataCalc
public AbstractDataCalc(DataSet set) - Your constructor that sets your dataset to an instance
variable, and runCalculations() based on the dataset if the passed in set is not null. (hint: call
setAndRun)
public void setAndRun(DataSet set) - sets the DataSet to an instance variable, and if the passed
in value is not null, runCalculations on that data
private void runCalculations() - as this is private, technically it is optional, but you are going to
want it (as compared to putting it all in setAndRun). This builds an array (or list) of doubles,
2. with an item for each row in the dataset. The item is the result returned from calcLine.
public String toString() - Override toString, so it generates the format seen above. Method is the
type returned from get type, row counting is the more human readable - starting at 1, instead of
0.
public abstract String getType() - see below
public abstract double calcLine(List line) - see below
AverageDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "AVERAGE"
public abstract double calcLine(List line) - runs through all items in the line and returns the
average value (sum / count).
MaximumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "MAX"
public abstract double calcLine(List line) - runs through all items, returning the largest item in
the list.
MinimumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
public abstract String getType() - The type returned is "MIN"
public abstract double calcLine(List line) - runs through all items, returning the smallest item in
the list.
MaximumDataCalc.java------ write code from scratch
Main.java
/*
* Copyright (c) 2020.
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike
4.0 International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box
1866, Mountain View, CA 94042, USA.
*/
/**
* @author Albert Lionelle
* lionelle@colostate.edu
3. * Computer Science Department
* Colorado State University
* @version 20200624
*/
public class Main {
public static void main(String[] args) {
String testFile = "sample.csv";
DataSet set = new DataSet(testFile);
AverageDataCalc averages = new AverageDataCalc(set);
System.out.println(averages);
MinimumDataCalc minimum = new MinimumDataCalc(set);
System.out.println(minimum);
MaximumDataCalc max = new MaximumDataCalc(set);
System.out.println(max);
}
}
MinumumDataCalc.java------ write code from scratch
AverageDataCalc.java------ write code from scratch
AbstractDataCalc.java------ write code from scratch
import java.util.ArrayList;
import java.util.List;
/**
* A simple container that holds a set of Data, by row/col (matrix)
* @version 20200627
*/
public class DataSet {
private final List> data = new ArrayList<>();
/**
4. * Reads the file, assuming it is a CSV file
* @param fileName name of file
*/
public DataSet(String fileName) {
this(new CSVReader(fileName, false));
}
/**
* Takes in a CSVReader to load the dataset
* @param csvReader a csvReader
*/
public DataSet(CSVReader csvReader) {
loadData(csvReader);
}
/**
* Returns the number of rows in the data set
* @return number of rows
*/
public int rowCount() {
return data.size();
}
/**
* Gets a specific row based on the index. Throws exception if out of bounds
* @param i the index of the row
* @return the row as a List of doubles
*/
public List getRow(int i) {
return data.get(i);
}
/**
* Loads the data from th CSV file
* @param file a CSVReader
*/
private void loadData(CSVReader file) {
while(file.hasNext()) {
List dbl = convertToDouble(file.getNext());
if(dbl.size()> 0) {
5. data.add(dbl);
}
}
}
/**
* Converts a List of strings to a list of doubles. Ignores non-doubles in the list
* @param next a List of strings
* @return a List of doubles
*/
private List convertToDouble(List next) {
List dblList = new ArrayList<>(next.size());
for(String item : next) {
try {
dblList.add(Double.parseDouble(item));
}catch (NumberFormatException ex) {
System.err.println("Number format!");
}
}
return dblList;
}
/**
* Simple way to view the data
* @return a String of the
*/
@Override
public String toString() {
return data.toString();
}
}
CSVReader.java
/*
* Copyright (c) 2020.
* This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike
4.0 International License. To view a copy of this license, visit
http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box
6. 1866, Mountain View, CA 94042, USA.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* This is a slightly more advanced CSV reader that can handle quoted tokens.
*/
public class CSVReader {
private static final char DELIMINATOR = ',';
private Scanner fileScanner;
/**
* Basic constructor that assumes the first line should be skipped.
* @param file name of file to read
*/
public CSVReader(String file) {
this(file, true);
}
/**
* A constructor that requires the name of the file to open
* @param file filename to read
* @param skipHeader true to skip header, false if it is to be read
*/
public CSVReader(String file, boolean skipHeader) {
try {
fileScanner = new Scanner(new File(file));
if(skipHeader) this.getNext();
}catch (IOException io) {
System.err.println(io.getMessage());
System.exit(1);
}
}
/**
* Reads a line (nextLine()) and splits it into a String array by the DELIMINATOR, if the line is
7. * empty it will also return null. This is the proper way to check for CSV files as compared
* to string.split - as it allows for "quoted" strings ,",",.
* @return a String List if a line exits or null if not
*/
public List getNext() {
if(hasNext()){
String toSplit = fileScanner.nextLine();
List result = new ArrayList<>();
int start = 0;
boolean inQuotes = false;
for (int current = 0; current < toSplit.length(); current++) {
if (toSplit.charAt(current) == '"') { // the char uses the '', but the " is a simple "
inQuotes = !inQuotes; // toggle state
}
boolean atLastChar = (current == toSplit.length() - 1);
if (atLastChar) {
result.add(toSplit.substring(start).replace(""", "")); // remove the quotes from the quoted item
} else {
if (toSplit.charAt(current) == DELIMINATOR && !inQuotes) {
result.add(toSplit.substring(start, current).replace(""", ""));
start = current + 1;
}
}
}
return result;
}
return null;
}
/**
* Checks to see if the fileScanner has more lines and returns the answer.
* @return true if the file scanner has more lines (hasNext())
*/
public boolean hasNext() {
return (fileScanner != null) && fileScanner.hasNext();
}
}