Question
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class bloodDrive { public static String bloodDrive = (C:/Users/Angel/Desktop/Project/bloodDrive.txt); public static int MAX_HOURS =24;
import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner;
public class bloodDrive { public static String bloodDrive = ("C:/Users/Angel/Desktop/Project/bloodDrive.txt");
public static int MAX_HOURS =24;
public static void main(String[] args) throws IOException { int pints[]; int hours = 4; int highPints,lowPints,avePints; pints = getPints(new File("bloodDrive.txt")); avePints = getAverage(pints, hours);
highPints = getHigh(pints, hours);
lowPints = getLow(pints, hours); displayInfo(hours,avePints,highPints,lowPints); }
public static int getAverage(int[] pints, int hours) { int avg =0; for (int i = 0; i < hours; i++) { avg+=pints[i]; } return avg/hours; }
public static int getHigh(int[] pints, int hours) { int high =pints[0]; for (int i = 0; i < hours; i++) { if (high < pints[i]) { high = pints[i]; } } return high; }
public static int getLow(int[] pints, int hours) { int low =pints[0]; for (int i = 0; i < hours; i++) { if (low > pints[i]) { low = pints[i]; } } return low; } public static void displayInfo(Integer hours, int averagePints, int highPints, int lowPints) throws FileNotFoundException { File bloodOutFile = new File("/Users/Angel/Desktop/Project/bloodDrive.txt"); int valueOfHours = 24;
PrintWriter pw = new PrintWriter(bloodOutFile); pw.write("Report generated by [Angel Sanchez] "); pw.write("Blood drive period: "); pw.write("value of hours" + valueOfHours); pw.write(" Average Pints: "); pw.write("" + averagePints); pw.write(" Highest pints:" + highPints); pw.write("Lowest pints: "); pw.write(lowPints); pw.close(); }
public static int[] getPints(File inFile) throws IOException { if (!inFile.exists()) { System.out.println("Cannot open File"); return null; } Scanner sc = new Scanner(inFile); int count[] = new int[100]; int i = 0; while (sc.hasNextLine()) {//While not end-of-file int val = Integer.parseInt(sc.nextLine());//get value count[i++] = val; } sc.close(); return count; } }
I have this code but doesn't seem to give me the necessary output in cmd. The following are instructions i followed in order to create it.
The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, up to twenty-four hour drive period, from an input file, bloodDrive.txt. The average pints, highest pints, and lowest pints donated during that period should be calculated and written to a file calledbloodResults.txt. We will only run the program once so a yes/no loop is not needed.
Step 1: Note that the getHigh, getLow, and getAverage functions do not change. We need to modify modules getPints and displayInfo to meet the new requirements. Complete the pseudocode below for the two modules according to instructions:
In the getPints Module (notice there is a new pass-by-reference parameter count)
Declare an input file with the name bloodInFile.
Open the internal file (bloodInFile) and a text file named bloodDrive.txt.
If cannot open file then display an error message
Set count = 0
Input one value from the file
While not end-of-file, store value in the array[count], add 1 to count, and input another value
Close the file.
Module getPints(Real pints[], Integer Ref count)
End Module
In the displayInfo Module
Declare an output file with the name bloodOutFile.
Open the internal file (bloodOutFile) and a text file named bloodResults.txt.
Write the string Report generated by [YourName] to the file.
Write the string Blood drive period: to the file.
Write the value of hours to the file.
Write the string Average Pints: to the file.
Write the value of averagePints to the file.
Do the same thing for highest pints and lowest pints
Close the bloodOutFile.
Module displayInfo(Integer hours, Real averagePints,
Real highPints, Real lowPints,)
End Module
Pseudocode for the main module (MAX_HOURS is a global named constant)
Module main()
//Declare local variables
Declare Real pints[MAX_HOURS]
Declare Integer hours
Declare Real avePints, highPints, lowPints
Call getPints(pints, hours)
Display hours: , hours
Set avePints = getAverage(pints, hours)
Set highPints = getHigh(pints, hours)
Set lowPints = getLow(pints, hours)
Call displayInfo(hours, avePints, highPints, lowPints)
End Module
Page BreakLab 9.2 Java Code
Convert the blood drive program from Lab 9.1 to Java code with the help of Java Companions Ch. 10. Create a text file name bloodDrive.txt in the project folder with the following values.
43
25
64
35
19
37
46
Run your program and check against the following output file, bloodResults.txt. If there are errors, go back through the steps to troubleshoot. You might want to output relevant information such as input data to the screen to see what is going on.
Report generated by [YourName]
Blood drive period: 7
Average pints: 38.4
Highest pints: 64.0
Lowest pints: 19.0
You might want to try another test case with fewer or more than 7 values to make sure your program can handle it. Dont forget to format the results properly.? When your code is complete and runs properly, print both the source code and the output file for the test case above.?
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