'Working Java project and i keep getting errors im stuck [closed]
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
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);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
