Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So, working on code and files I've been helped with and I'm struggling to figure out where I should be using System.out.printf( %.1f ); or

So, working on code and files I've been helped with and I'm struggling to figure out where I should be using System.out.printf( %.1f ); or such in order for the results to be printed out with only the first decimal rounded up. I can't mess with Main, DataSet, or CSVReader so please do not add anything there.

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

Note: the extra line between Set results is optional. All other spacing must be exact!

CSVReader.java

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

DataSet.java

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

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

AbstractData.java

import java.util.List; public abstract class AbstractDataCalc { protected DataSet dataSet; public AbstractDataCalc(DataSet set) { setAndRun(set); } public void setAndRun(DataSet set) { this.dataSet = set; if (set != null) { runCalculations(); } } private void runCalculations() { // Build an array (or list) of doubles with an item for each row in the dataset. // The item is the result returned from calcLine. } @Override public String toString() { StringBuilder result = new StringBuilder("Dataset Results (Method: " + getType() + ") "); for (int i = 0; i < dataSet.rowCount(); i++) { result.append("Row ").append(i + 1).append(": ").append(calcLine(dataSet.getRow(i))).append(" "); } return result.toString(); } public abstract String getType(); public abstract double calcLine(List line); }

AverageData.java

import java.util.List; public abstract class AbstractDataCalc { protected DataSet dataSet; public AbstractDataCalc(DataSet set) { setAndRun(set); } public void setAndRun(DataSet set) { this.dataSet = set; if (set != null) { runCalculations(); } } private void runCalculations() { // Build an array (or list) of doubles with an item for each row in the dataset. // The item is the result returned from calcLine. } @Override public String toString() { StringBuilder result = new StringBuilder("Dataset Results (Method: " + getType() + ") "); for (int i = 0; i < dataSet.rowCount(); i++) { result.append("Row ").append(i + 1).append(": ").append(calcLine(dataSet.getRow(i))).append(" "); } return result.toString(); } public abstract String getType(); public abstract double calcLine(List line); }

MaximumDataCalc.java

import java.util.List; public abstract class AbstractDataCalc { protected DataSet dataSet; public AbstractDataCalc(DataSet set) { setAndRun(set); } public void setAndRun(DataSet set) { this.dataSet = set; if (set != null) { runCalculations(); } } private void runCalculations() { // Build an array (or list) of doubles with an item for each row in the dataset. // The item is the result returned from calcLine. } @Override public String toString() { StringBuilder result = new StringBuilder("Dataset Results (Method: " + getType() + ") "); for (int i = 0; i < dataSet.rowCount(); i++) { result.append("Row ").append(i + 1).append(": ").append(calcLine(dataSet.getRow(i))).append(" "); } return result.toString(); } public abstract String getType(); public abstract double calcLine(List line); }

MinimumDataCalc.java

import java.util.List; public abstract class AbstractDataCalc { protected DataSet dataSet; public AbstractDataCalc(DataSet set) { setAndRun(set); } public void setAndRun(DataSet set) { this.dataSet = set; if (set != null) { runCalculations(); } } private void runCalculations() { // Build an array (or list) of doubles with an item for each row in the dataset. // The item is the result returned from calcLine. } @Override public String toString() { StringBuilder result = new StringBuilder("Dataset Results (Method: " + getType() + ") "); for (int i = 0; i < dataSet.rowCount(); i++) { result.append("Row ").append(i + 1).append(": ").append(calcLine(dataSet.getRow(i))).append(" "); } return result.toString(); } public abstract String getType(); public abstract double calcLine(List line); }

The output has been:

Dataset Results (Method: AVERAGE) Row 1: 4.0 Row 2: 35.0 Row 3: 12.766666666666666 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

I would like not only Row 3 of Average to round correctly, but all of them and I can't mess with Main, DataSet, or CSVReader so please do not add anything there

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

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions

Question

Exude confidence, not arrogance.

Answered: 1 week ago

Question

13. Let A be a zero-one matrix. Show that AVA A

Answered: 1 week ago