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 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
* 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<>();
/**
* 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) {
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
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
* 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();
}
}

More Related Content

PDF
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
PPT
ASP.NET 08 - Data Binding And Representation
PDF
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
PDF
in c languageTo determine the maximum string length, we need to .pdf
PDF
Algorithms devised for a google interview
PDF
In this lab, we will write an application to store a deck of cards i.pdf
PDF
Create a JAVA program that performs file IO and database interaction.pdf
PPT
2 a stacks
Creat Shape classes from scratch DETAILS You will create 3 shape cla.pdf
ASP.NET 08 - Data Binding And Representation
Using Array Approach, Linked List approach, and Delete Byte Approach.pdf
in c languageTo determine the maximum string length, we need to .pdf
Algorithms devised for a google interview
In this lab, we will write an application to store a deck of cards i.pdf
Create a JAVA program that performs file IO and database interaction.pdf
2 a stacks

Similar to For this lab, you will write the following filesAbstractDataCalc.pdf (20)

PDF
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
PDF
spring-tutorial
PDF
Java 8 Workshop
PDF
Frequency .java Word frequency counter package frequ.pdf
PPT
Spring data ii
PPT
New features and enhancement
PPTX
Aggregate.pptx
PPT
Sqlapi0.1
PDF
Jdbc tutorial
PDF
Stata Programming Cheat Sheet
PPT
30 5 Database Jdbc
PPSX
ADO.NET
PPTX
File Input and output.pptx
PDF
Stata cheatsheet programming
PDF
File LinkedList.java Defines a doubly-l.pdf
PDF
Please the following is the currency class of perious one- class Curre.pdf
ODP
PDF
9 Python programming notes for ktu physics and computer application semester 4
PPT
Ado.net
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
spring-tutorial
Java 8 Workshop
Frequency .java Word frequency counter package frequ.pdf
Spring data ii
New features and enhancement
Aggregate.pptx
Sqlapi0.1
Jdbc tutorial
Stata Programming Cheat Sheet
30 5 Database Jdbc
ADO.NET
File Input and output.pptx
Stata cheatsheet programming
File LinkedList.java Defines a doubly-l.pdf
Please the following is the currency class of perious one- class Curre.pdf
9 Python programming notes for ktu physics and computer application semester 4
Ado.net
Ad

More from alokindustries1 (20)

PDF
Select the TRUE statement Group of answer choicesA. An arthropod�.pdf
PDF
Select one of the scenarios listed below and explain the best soluti.pdf
PDF
Select the combination of the following statements relating to sourc.pdf
PDF
select each of the following which relate to the term homozygous .pdf
PDF
Select the combination of the following statements regarding stakeho.pdf
PDF
select each of the following which relate to the term heterozygous.pdf
PDF
Select all that are true regarding Economic Value Added (EVA) a) .pdf
PDF
Select the case that would most likely be filed under disparate impa.pdf
PDF
Select all that are true about plasmid DNA.a.The DNA in plasmids.pdf
PDF
Select a current event (local or international) that is relevant to .pdf
PDF
Seleccione SOLO UNO de los siguientes Objetivos de Desarrollo Sosten.pdf
PDF
Seleccione todas las cualidades que pertenecen a los sistemas parlam.pdf
PDF
Seleccione la declaraci�n FALSA de las siguientes A. Trabajar con o.pdf
PDF
Seismic waves travel faster when the rock is less stiff.A) TrueB.pdf
PDF
Seg�n TCPS 2, �qu� es la investigaci�n de riesgo m�nimo (seleccione .pdf
PDF
Seg�n Juan Linz (1990), �c�mo los sistemas presidenciales crean disc.pdf
PDF
Seg�n Holton en la fuente 1, �cu�les fueron los motivos de los padre.pdf
PDF
Seg�n el texto, �qu� es un factor Persona a la que se le da autori.pdf
PDF
Security X has an actual rate of return of 11.8 and a beta of 0.72..pdf
PDF
Seg�n el C�digo y las Normas, �cu�l de las siguientes afirmaciones c.pdf
Select the TRUE statement Group of answer choicesA. An arthropod�.pdf
Select one of the scenarios listed below and explain the best soluti.pdf
Select the combination of the following statements relating to sourc.pdf
select each of the following which relate to the term homozygous .pdf
Select the combination of the following statements regarding stakeho.pdf
select each of the following which relate to the term heterozygous.pdf
Select all that are true regarding Economic Value Added (EVA) a) .pdf
Select the case that would most likely be filed under disparate impa.pdf
Select all that are true about plasmid DNA.a.The DNA in plasmids.pdf
Select a current event (local or international) that is relevant to .pdf
Seleccione SOLO UNO de los siguientes Objetivos de Desarrollo Sosten.pdf
Seleccione todas las cualidades que pertenecen a los sistemas parlam.pdf
Seleccione la declaraci�n FALSA de las siguientes A. Trabajar con o.pdf
Seismic waves travel faster when the rock is less stiff.A) TrueB.pdf
Seg�n TCPS 2, �qu� es la investigaci�n de riesgo m�nimo (seleccione .pdf
Seg�n Juan Linz (1990), �c�mo los sistemas presidenciales crean disc.pdf
Seg�n Holton en la fuente 1, �cu�les fueron los motivos de los padre.pdf
Seg�n el texto, �qu� es un factor Persona a la que se le da autori.pdf
Security X has an actual rate of return of 11.8 and a beta of 0.72..pdf
Seg�n el C�digo y las Normas, �cu�l de las siguientes afirmaciones c.pdf
Ad

Recently uploaded (20)

PDF
International_Financial_Reporting_Standa.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
IP : I ; Unit I : Preformulation Studies
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PPTX
Computer Architecture Input Output Memory.pptx
PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
FORM 1 BIOLOGY MIND MAPS and their schemes
PDF
Complications of Minimal Access-Surgery.pdf
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
Climate Change and Its Global Impact.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
CRP102_SAGALASSOS_Final_Projects_2025.pdf
PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
International_Financial_Reporting_Standa.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
IP : I ; Unit I : Preformulation Studies
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Computer Architecture Input Output Memory.pptx
Introduction to pro and eukaryotes and differences.pptx
FORM 1 BIOLOGY MIND MAPS and their schemes
Complications of Minimal Access-Surgery.pdf
Journal of Dental Science - UDMY (2020).pdf
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Climate Change and Its Global Impact.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
What’s under the hood: Parsing standardized learning content for AI
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
CRP102_SAGALASSOS_Final_Projects_2025.pdf
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...

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(); } }