Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Working Java project and i keep getting errors im stuck

Here’s the question for the project.

Find solutions for your homework

Search
home / study / engineering / computer science / computer science questions and answers / in java code a program that tabulates contributions collected by an organization. the organization …
Your question has been answered
Let us know if you got a helpful answer. Rate this answer
Question: In Java code a program that tabulates contributions collected by an organization. The organization…
In Java code a program that tabulates contributions collected by an organization. The organization wishes to accept contributions until a total of $10,000,000 is met. Once this total is hit, no further contributions should be accepted. The organization wants the program to read data from an input file (input.in). The following data should be written to a file called results.out

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a.The total number of contributions needed to meet the goal of 10 million dollars

b.The amount of the largest and smallest contribution accepted

c.The average contribution size

d.The final total of the contributions accepted

3.Implement (code) and test your program with a variety of input data. It is helpful to set a smaller contribution goal when testing your program. Consider both the scenario where the input file does not contain enough contributions to meet the goal and the scenario where the input file contains more data than needed. The output file should match the formatting shown in the example:

The maximum contribution received was $53,246.00.

The minimum contribution received was $7.00.

The average contribution amount was $4, 982.58.

A total of $10,000,34.00 was collected.

Here’s my code:

public class Contribution {

    public void fileOperation(String inputFile, String outputFile) {
        double max_contro = Double.MIN_VALUE;
        double min_contro = Double.MAX_VALUE;
        int total = 0;
        int goal = 1000;
        double current = 0.0;

//         Reading from the file
        try {
            File file = new File(inputFile);
            Scanner sc = new Scanner(file);
            while (sc.hasNextLine()) {
                double amt = Double.parseDouble(sc.nextLine());
                current += amt;
                total++;
                if (amt < min_contro) {
                    min_contro = amt;
                }
                if (amt > max_contro) {
                    max_contro = amt;
                }
                if (current > goal) {
                    break;
                }
            }
        }
        catch (Exception e) {
            System.out.println("Error processing the file!");
        }

        double average_contro = Math.round((double) current / total);
//         Writing to the file
        try {
            FileWriter writer = new FileWriter(outputFile, true);
            BufferedWriter out = new BufferedWriter(writer);
            out.write("It took " + total
                    + " contributions to reach the goal.\nThe maximum contribution"
                    + " received was $" + max_contro + ".\nThe minimum contribution"
                    + " received was $" + min_contro + "\nThe average contribution amount was $"
                    + average_contro + ".\nA total of $" + current + " was collected.");
            out.newLine();
            out.close();
        }
        catch (Exception e) {
            System.out.println("Error processing the file!");
        }
    }

//     Driver method to test the code
    public static void main(String[] args) {
        String inputFile = "input.in";
        String outputFile = "output.out";

        Contribution obj = new Contribution();
        obj.fileOperation(inputFile, outputFile);
    }
}

>Solution :

your code is working fine:
you just need to import below :

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

your whole and correct code will be:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
public class Contribution {


    public void fileOperation(String inputFile, String outputFile) {
        double max_contro = Double.MIN_VALUE;
        double min_contro = Double.MAX_VALUE;
        int total = 0;
        int goal = 1000;
        double current = 0.0;

//          Reading from the file
        try {
            File file = new File(inputFile);
            Scanner sc = new Scanner(file);
            while (sc.hasNextLine()) {

                double amt = Double.parseDouble(sc.nextLine());
                current += amt;
                total++;
                if (amt < min_contro) {
                    min_contro = amt;
                }
                if (amt > max_contro) {
                    max_contro = amt;
                }
                if (current > goal) {
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println("Error processing the file!");
        }

        double average_contro = Math.round((double) current / total);
//          Writing to the file
        try {
            FileWriter writer = new FileWriter(outputFile, true);
            BufferedWriter out = new BufferedWriter(writer);
            out.write("It took " + total + " contributions to reach the goal.\nThe maximum contribution"
                    + " received was $" + max_contro + ".\nThe minimum contribution"
                    + " received was $" + min_contro + "\nThe average contribution amount was $"
                    + average_contro + ".\nA total of $" + current + " was collected.");
            out.newLine();
            out.close();
        } catch (Exception e) {
            System.out.println("Error processing the file!");
        }

    }

    //      Driver method to test the code
    public static void main(String[] args) {

        String inputFile = "input.in";
        String outputFile = "output.out";

        Contribution obj = new Contribution();
        obj.fileOperation(inputFile, outputFile);

    }
}

Output:
output image

in output window lastly it goes to catch block and prints the catch block statement.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading