Question: Lab 9: File Access This lab accompanies Chapter 10 of the text book. For this lab, students will work in pairs . Each pair will
Lab 9: File Access
This lab accompanies Chapter 10 of the text book.
For this lab, students will work in pairs. Each pair will use one computer, one monitor, one mouse, and one keyboard. If questions arise, try to answer them within the pair before turning to another pair for help. Ask the instructor for help as a last resort.
** Download this file from Canvas, type in all the answers, and turn in your lab report online using Canvas. Note that each student needs to submit the report individually in Canvas **
Name#1: Name#2: Date: 11/2/2018
Lab 9.1 File Access and Pseudocode
| Critical Review When a program needs to save data for later use, it writes the data to a file. The data can be read from the file at a later time. Three things must happen in order to work with a file. 1) Open a file. 2) Process the file. 3) Close the file. An internal file must be created for an output file or input file, such as: Declare OutputFile outFile // to write out Declare InputFile inFile // to read in A data file must also be created to store the output, such as: Open outFile thedata.txt New operations include the following: Open InternalName FileName Write InternalName String or Data Read InternalName Data Close InternalName Loops are used to process the data in a file. For example: For counter = 1 to 5 Display Enter a number: Input number Write outFile number End For When reading information from a file and it is unknown how many items there are, use the eof function. It is similar to a sentinel loop. For example: Read inFile number While NOT eof(inFile) Display number Read inFile number End While |
This lab examines how to work with a file by writing pseudocode. Read the following programming problem prior to completing the lab. The following program from Lab 8 will be used, with some modifications.
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 called bloodResults.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
Lab 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.
Code for the question above?
import java.util.*;
public class BloodPints {
public static int MAX_HOURS=7;
public static void getPints(double pints[], int hours)
{ Scanner scan = new Scanner(System.in);
System.out.println(" Enter data for the "+MAX_HOURS+" hours ");
for(int i=0;i
{
System.out.print(" Enter pints collected:");
pints[i] = scan.nextDouble();
} for(int i =0; i System.out.println(pints[i]); }
}
public static double getAverage(double pints[], int hours)
{
int i;
double totalPints=0;
double averagePints;
for(i=0;i
totalPints = totalPints + pints[i];
averagePints = totalPints/hours;
return averagePints;
}
public static double getHigh(double pints[], int hours)
{
int i;
double highest = pints[0];
for(i=1;i
if(pints[i] > highest)
highest = pints[i];
return highest;
}
public static double getLow(double pints[], int hours)
{
int i;
double lowest = pints[0];
for(i=1;i
if(pints[i] < lowest)
lowest = pints[i];
return lowest;
}
public static void displayInfo(double averagePints, double highPints, double lowPints)
{
System.out.printf(" Average Pints : %.1f",averagePints);
System.out.printf(" Highest Pints : %.1f",highPints);
System.out.printf(" Lowest Pints : %.1f",lowPints);
}
public static void main(String[] args) {
double pints[] = new double[MAX_HOURS];
double averagePints,lowPints,highPints ;
String again = "yes";
Scanner scan = new Scanner(System.in);
while(again.equalsIgnoreCase("yes"))
{
getPints(pints,MAX_HOURS);
averagePints = getAverage(pints,MAX_HOURS);
highPints = getHigh(pints,MAX_HOURS);
lowPints = getLow(pints,MAX_HOURS);
displayInfo(averagePints,highPints,lowPints);
System.out.print(" Do you want to run again (yes or no)? ");
again = scan.nextLine();
}
scan.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
