REFERENCE 16
First 211 Program written in Object Oriented Style by Albert Lewis
public class Person {
private char race;
private char gender;
private int age;
private int weight;
public Person() {
}
public void setRace(char r)
{race = r;}
public void setGender(char g)
{gender = g;}
public void setAge(int a)
{age = a;}
public void setWeight(int w)
{weight = w;}
public char getRace()
{return race;}
public char getGender()
{return gender;}
public int getAge()
{return age;}
public int getWeight()
{return weight;}
}
//*****************************************************
import java.io.RandomAccessFile;
import java.io.EOFException;
import java.io.IOException;
import java.util.Vector;
public class SortPeople {
private RandomAccessFile raf;
private final String fileToRead = "C:\\data1";
private Vector vector = new Vector();
public SortPeople(){
this.loadFile();
System.out.println(this);
}
private void loadFile(){ //reads from the file and builds objects
try{
raf = new RandomAccessFile(fileToRead,"r");
while(raf.getFilePointer() < raf.length())
{
Person person = new Person();
person.setRace(raf.readChar());
person.setGender(raf.readChar());
person.setAge(raf.readInt());
person.setWeight(raf.readInt());
vector.add(person); //adds built objects to storage area
}
}
catch(EOFException eof)
{System.err.println("End of file: "+eof);}
catch(IOException ioe)
{System.err.println("IOError occurred: " + ioe);}
}
private int getTotalAges(){
int total = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
total += p.getAge();
}
return total;
}
private int getTotalBlkMales(){
int total = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
if(p.getGender() == 'M' && p.getRace() == 'B')
{
total++;
}
}
return total;
}
private int getTotalWhtFemales(){
int total = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
if(p.getGender() == 'F' && p.getRace() == 'W')
{
total++;
}
}
return total;
}
private int getFemalesOver20(){
int total = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
if((p.getGender() == 'F') && (p.getAge() > 20))
{
total++;
}
}
return total;
}
private double getAverageOfOver125lbs(){
int age = 0;
int count = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
if(p.getWeight() > 125)
{
age += p.getAge();
count++;
}
}
return (age / count);
}
private double getAverageMaleWeight(){
double weight = 0.0;
int count = 0;
for(int a = 0; a < vector.size(); a++)
{
Person p = (Person)vector.get(a);
if(p.getGender() == 'M')
{
weight += p.getWeight();
count++;
}
}
return (weight / count);
}
private String printFile(){
String file = "\n\n";
try{
raf = new RandomAccessFile(fileToRead,"r"); //resets file (creates new instance)
while(raf.getFilePointer() < raf.length()){ //reads contents of file in (char,char int,int) order
file += (raf.readChar() + " "+ raf.readChar() + " "
+ raf.readInt() + " " + raf.readInt() +"\n");
}
}
catch(EOFException eof)
{System.err.println("End of file: "+eof);}
catch(IOException ioe)
{System.err.println("IOError occurred: " + ioe);}
return file;
}
public String toString(){
return "\n1 TOTAL AGE: "+this.getTotalAges()+ "\n2 NUMBER OF BLACK MALES: "+this.getTotalBlkMales()+
"\n3 NUMBER OF WHITE FEMALES: "+this.getTotalWhtFemales()+ "\n4 AVERAGE WEIGHT OF MALES: "+this.getAverageMaleWeight()+
"\n5 NUMBER OF FEMALES OVER 20 YEARS OLD: "+this.getFemalesOver20()+"\n6 AVERAGE AGE OF PEOPLE OVER 125 POUNDS: "+this.getAverageOfOver125lbs()+
"\n7 ITEMIZED LIST OF THE DATA: \n"+ this.printFile();
}
public static void main(String A[]){
new SortPeople();
}
}