In: Computer Science
Hello, I need an expert answer for one of my JAVA homework assignments. I will be providing the instructions for this homework below:
For this lab, you will write the following files:
As you can expect, AbstractDataCalc is an abstract class, that AverageDataCalc, MaximumDataCalc and MinimumDataCalc all inherit.
We will provide the following files to you:
Sample Input / Output
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Given the following CSV file
1,2,3,4,5,6,7 10,20,30,40,50,60 10.1,12.2,13.3,11.1,14.4,15.5
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
The output of the provided main is:
Dataset Results (Method: AVERAGE) Row 1: 4.0 Row 2: 35.0 Row 3: 12.8 Dataset Results (Method: MIN) Row 1: 1.0 Row 2: 10.0 Row 3: 10.1 Dataset Results (Method: MAX) Row 1: 7.0 Row 2: 60.0 Row 3: 15.5
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Specifications
You will need to implement the following methods at a minimum. You are free to add additional methods.
AbstractDataCalc
AverageDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
MaximumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
MinimumDataCalc
Extends AbstractDataCalc. Will implement the required constructor and abstract methods only.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CSVReader.java
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CSVReader {
private static final char DELIMINATOR =
',';
private Scanner fileScanner;
public CSVReader(String file) {
this(file, true);
}
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);
}
}
public List<String> getNext() {
if(hasNext()){
String toSplit = fileScanner.nextLine();
List<String> 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;
}
public boolean hasNext() {
return (fileScanner !=
null) && fileScanner.hasNext();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DataSet.java
import java.util.ArrayList;
import java.util.List;
public class DataSet {
private final List<List<Double>>
data = new ArrayList<>();
public DataSet(String fileName) {
this(new
CSVReader(fileName, false));
}
public DataSet(CSVReader csvReader) {
loadData(csvReader);
}
public int rowCount() {
return
data.size();
}
public List<Double> getRow(int i) {
return
data.get(i);
}
private void loadData(CSVReader file) {
while(file.hasNext())
{
List<Double> dbl = convertToDouble(file.getNext());
if(dbl.size()> 0) {
data.add(dbl);
}
}
}
private List<Double>
convertToDouble(List<String> next) {
List<Double>
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;
}
public String toString() {
return
data.toString();
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Main.java
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);
}
}
import java.util.ArrayList;
import java.util.List;
public abstract class AbstractDataCalc {
private DataSet set; // the dataset
private List<Double> resultsList; // the list
built by runCalculations()
// calls setAndRun to set dataset to an instance
variable
public AbstractDataCalc(DataSet set) {
setAndRun(set);
}
// sets the dataSet to an instance variable, and if
the passed in value is not null, runCalculations on that data
public void setAndRun(DataSet set) {
this.set = set;
if(set != null)
runCalculations();
}
// builds a list of doubles, with an item for each row
in the dataset. The item is the result returned from calcLine
private void runCalculations() {
resultsList = new
ArrayList<Double>();
for(int i=0; i<set.rowCount();
i++)
resultsList.add(calcLine(set.getRow(i)));
}
// generates results in a tabular form (the given
format)
@Override
public String toString() {
String result = "\nDataset Results
(Method: " + getType() + ")";
for(int i=0;
i<resultsList.size(); i++)
result += "\nRow
" + (i+1) + ": " + String.format("%.1f",resultsList.get(i));
return result;
}
// methods to be implemented by the subclasses
public abstract String getType();
public abstract double calcLine(List<Double>
line) ;
}
__________________________________
import java.util.List;
public class AverageDataCalc extends AbstractDataCalc{
public AverageDataCalc(DataSet set) {
super(set); // AbstractDataCalc
constructor
}
// - The type returned is "AVERAGE"
@Override
public String getType() {
return "AVERAGE";
}
// - runs through all items in the line and returns
the average value (sum / count).
@Override
public double calcLine(List<Double> line)
{
int count = line.size();
double sum = 0;
for(double item : line)
sum +=
item; // accumulate the sum
return sum / count; // compute and
return the average
}
}
__________________________________
import java.util.List;
public class MaximumDataCalc extends AbstractDataCalc{
public MaximumDataCalc(DataSet set) {
super(set); // AbstractDataCalc
constructor
}
// - The type returned is "MAX"
@Override
public String getType() {
return "MAX";
}
// - runs through all items, returning the largest
item in the list.
@Override
public double calcLine(List<Double> line)
{
double max = line.get(0);
for(double item: line)
if(item >
max)
max = item;
return max;
}
}
_____________________________________
import java.util.List;
public class MinimumDataCalc extends AbstractDataCalc{
public MinimumDataCalc(DataSet set) {
super(set); // AbstractDataCalc
constructor
}
// - The type returned is "MIN"
@Override
public String getType() {
return "MIN";
}
// - runs through all items, returning the smallest
item in the list.
@Override
public double calcLine(List<Double> line)
{
double min = line.get(0);
for(double item: line)
if(item <
min)
min = item;
return min;
}
}
_________________________________________
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);
}
}
_________________________________
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CSVReader {
private static final char DELIMINATOR = ',';
private Scanner fileScanner;
public CSVReader(String file) {
this(file, true);
}
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);
}
}
public List<String> getNext() {
if(hasNext()){
String toSplit = fileScanner.nextLine();
List<String> 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;
}
public boolean hasNext() {
return (fileScanner != null) &&
fileScanner.hasNext();
}
}
_____________________________________
import java.util.ArrayList;
import java.util.List;
public class DataSet {
private final List<List<Double>> data = new
ArrayList<>();
public DataSet(String fileName) {
this(new CSVReader(fileName, false));
}
public DataSet(CSVReader csvReader) {
loadData(csvReader);
}
public int rowCount() {
return data.size();
}
public List<Double> getRow(int i) {
return data.get(i);
}
private void loadData(CSVReader file) {
while(file.hasNext()) {
List<Double> dbl = convertToDouble(file.getNext());
if(dbl.size()> 0) {
data.add(dbl);
}
}
}
private List<Double> convertToDouble(List<String> next)
{
List<Double> 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;
}
public String toString() {
return data.toString();
}
}
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~