Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help fixing the error I am getting on line 34 in the code that I created. Main.java // Below is main class public
I need help fixing the error I am getting on line 34 in the code that I created.
Main.java
// Below is main class public class Main { // Below is main method public static void main(String[] args) { // Below create File object for input file File inputFile = inputFile = new File(args[0]); // Below checking if the file exist or not if (inputFile.exists()) System.out.println("Input file appropriate"); else { System.out.println("In appropriate input filename"); return; } // Below creating File object for output file File outputFile = new File(args[1]); // Check if file exist or not if (outputFile.exists()) System.out.println("Output file is appropriate"); else { System.out.println("Output input filename"); return; } // Below creating reference of PrintWriter class PrintWriter out = null; try { // Below crating object of PrintWriter class out = new PrintWriter(outputFile); // Read the data from the file and stored in an ArrayList of Strings ArrayList lines = readData(inputFile); // Below creating ArrayList of Cake objects ArrayList cakeFlavors = buildObjects(lines); // Below is to get the average favorable double avgValue = calcAvg(cakeFlavors, "favorable"); // Below will display the avgValue to the output file display("The average favorable is:", avgValue, out); // Below get the highest protein value double highestValue = calcLagrgestVlaue(cakeFlavors, "protein"); // Below will display the highestValue to the output file display("The highest protein value is:", highestValue, out); // Below get the Cake object that closest to the average Cake cakeFlavor = findAvgCakeFlavor(cakeFlavors, favorable, avgValue); // Below will display the cakeFlavor to the output file display("The cake flavor closest to the average is:", cakeFalvor, out); // Below get the list of Cake objects that are above the average value for // favorable ArrayList cakes = findHighestCakeFlavors(cakeFalvors, avgValue, "favorable"); } catch (FileNotFoundException e) { System.out.println(e); } // Closing the file out.close(); } }
Cake.java
// Below is a cake class public class Cake { // Below are private attributes private String name; private double favorable; private double protein; private double price; // Below is default constructor public Cake() { name = ""; favorable = 0.0; protein = 0.0; price = 0.0; } // Below is parameterized constructor public Cake(String name, double favorable, double protein, double price) { this.name = name; this.favorable = favorable; this.protein = protein; this.price = price; } // Below is first setter method public void setName(String name) { this.name = name; } // Below is first getter method public String getName() { return name; } // Below is second setter method public void setFavorable(double favorable) { this.favorable = favorable; } // Below is second getter method public double getFavorable() { return favorable; } // Below is third setter method public void setProtein(double protein) { this.protein = protein; } // Below is third getter method public double getCalories() { return protein; } // Below is fourth setter method public void setPrice(double price) { this.price = price; } // Below is fourth getter method public double getPrice() { return price; } // Below is an Cake object that will return as String @Override public String toString() { return String.format("%s %.1f %.1f %.1f", name, favorable, protein, price); } }
Cake2.Java
import java.util.Scanner; import java.util.ArrayList; import java.io.*; // Below is a Cake2 class public class Cake2 { // Below method to read the data from the file and stored in an ArrayList of // Strings public static ArrayList readData(File inputFile) throws FileNotFoundException { // Below code open the file Scanner file = new Scanner(inputFile); // Blow code create -ArrayList of Strings ArrayList list = new ArrayList<>(); // Below code read the file while (file.hasNextLine()) { // Below code read a line from the file String line = file.nextLine(); // Below code add to the list list.add(line); } // Below code return the ArrayList of Strings return list; } // Below method create -ArrayLiss of Cake objects public static ArrayList buildObjects(ArrayList lines) { // Below code create -ArrayList of Cake objects ArrayList list = new ArrayList<>(); String name; double favorable, protein, price; cake; for(int i=0; i cakeFlavors, String attribute) { double sum = 0, avg = 0; if (attribute.equals("favorable")) { for (int i = 0; i < cakeFlavors.size(); i++) { sum += cakeFlavors.get(i).getFavorable(); } avg = sum / cakeFlavors.size(); } else if (attribute.equals("protein")) { for (int i = 0; i < cakeFlavors.size(); i++) { sum += cakeFlavors.get(i).getProtein(); } avg = sum / cakeFlavors.size(); } else if (attribute.equals("price")) { for (int i = 0; i < cakeFlavors.size(); i++) { sum += cakeFlavors.get(i).getPrice(); } avg = sum / cakeFlavors.size(); } return avg; } // Below method calculates the highest value for the given attribute public static double calcLargestValue(ArrayList cakeFlavors, String attribute) { double highest = 0; if (attribute.equals("favorable")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (highest < cakeFlavors.get(i).getFavorable()) highest = cakeFlavors.get(i).getFavorable(); } } else if (attribute.equals("protein")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (highest < cakeFlavors.get(i).getProtein()) highest = cakeFlavors.get(i).getProtein(); } } else if (attribute.equals("price")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (highest < cakeFlavors.get(i).getPrice()) highest = cakeFlavors.get(i).getPrice(); } } return highest; } // Below method finds the cake flavor which is closest to the given attribute public static Cake findAvgCakeFlavor(ArrayList cakeFlavors, String attribute, double avgValue) { cake = null; double min = Double.MAX_VALUE; double absValue = 0; if (attribute.equals("favorable")) { for (int i = 0; i < cakeFlavors.size(); i++) { absValue = Math.abs(avgValue - cakeFlavors.get(i).getFavorable()); if (absValue < min) { min = absValue; cake = cakeFlavors.get(i); } } } else if (attribute.equals("protein")) { for (int i = 0; i < cakeFlavors.size(); i++) { absValue = Math.abs(avgValue - cakeFlavors.get(i).getProtein()); if (absValue < min) { min = absValue; cake = cakeFlavors.get(i); } } } else if (attribute.equals("price")) { for (int i = 0; i < cakeFlavors.size(); i++) { absValue = Math.abs(avgValue - cakeFlavors.get(i).getPrice()); if (absValue < min) { min = absValue; cake = cakeFlavors.get(i); } } } return cake; } // Below method finds the cake flavors which are above the value passed for the // given attribute public static ArrayList findHighestCakeFlavors(ArrayList cakeFlavors, double value, String attribute) { ArrayList list = new ArrayList<>(); if (attribute.equals("favorable")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (value < cakeFlavors.get(i).getFavorable()) { list.add(cakeFlavors.get(i)); } } } else if (attribute.equals("protein")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (value < cakeFlavors.get(i).getProtein()) { list.add(cakeFlavors.get(i)); } } } else if (attribute.equals("price")) { for (int i = 0; i < cakeFlavors.size(); i++) { if (value < cakeFlavors.get(i).getPrice()) { list.add(cakeFlavors.get(i)); } } } return list; } // Below method print an ArrayList of values public static void display(String outputMessage, ArrayList cakeFlavors, PrintWriter out) throws FileNotFoundException { out.write(outputMessage + " "); for (int i = 0; i < cakeFlavors.size(); i++) { out.write(cakeFlavors.get(i).toString()); } } // Below method print one Cake object public static void display(String outputMessage, Cake cakeFlavor, PrintWriter out) throws FileNotFoundException { out.write(outputMessage + " "); out.write(cakeFlavor.toString()); } // Below method print double value public static void display(String outputMessage, double value, PrintWriter out) throws FileNotFoundException { out.write(outputMessage); out.write(String.format("%.1f ", value)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started