Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


250


importjava.io.*; // File types

public classIncomes
{
public static voidmain(String[] args) throwsIOException
{
Gender female; // Females
Gender male; // Males
String inLine; // A line from the file
chargenderCode; // Indicates gender
doublesalary; // Salary amount
BufferedReader inFile; // Input data file
PrintWriter outFile; // Output data file

// Prepare files for reading and writing
inFile = newBufferedReader(newFileReader("gender.dat"));
outFile = newPrintWriter(newFileWriter("results.dat"));

// Initialize process
female = newGender();
male = newGender();

while((inLine = inFile.readLine()) != null)
{
// Update process
// Extract gender code and amount from input line
// Gender is the first character
genderCode = inLine.charAt( 0 );
// Amount begins in the second position
salary = Double.parseDouble(inLine.substring( 1 , inLine.length()));
// Process amount based on gender code
if(genderCode == 'F')
// Female
female.update(salary);
else
// Male
male.update(salary);
}
inFile.close(); // Done reading
// Write results
outFile.println("For "+ female.knowCount() + " females, the average income is "
+ female.knowAverage() + ".");
outFile.println("For "+ male.knowCount() + " males, the average income is "+
male.knowAverage() + ".");
outFile.close();
}
}

T


E


A


M


F


L


Y


Team-Fly®

Free download pdf