import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.EOFException; import java.io.IOException; /** * CS 251L - Intermediate Programming * University of New Mexico - Los Alamos * I/O Lab. * * Class representing information about a precinct. */ public class PrecinctSolution { private int registeredVoters; private int uhuraVotes; private int mccoyVotes; private float femaleFraction; private short id; public Precinct() { } public Precinct(short id, int registeredVoters, int uhuraVotes, int mccoyVotes, float femaleFraction) { this.id = id; this.registeredVoters = registeredVoters; this.uhuraVotes = uhuraVotes; this.mccoyVotes = mccoyVotes; this.femaleFraction = femaleFraction; } public int getRegisteredVoters() { return this.registeredVoters; } public int getUhuraVotes() { return this.uhuraVotes; } public int getMccoyVotes() { return this.mccoyVotes; } public float getFemaleFraction() { return this.femaleFraction; } public short getId() { return this.id; } public boolean hasFraud() { return this.uhuraVotes + this.mccoyVotes > this.registeredVoters; } public static Precinct readObject(DataInputStream dis) throws IOException { try { Precinct p = new Precinct(); p.read(dis); return p; } catch (EOFException eofe) { return null; } } public void read(DataInputStream dis) throws IOException { this.registeredVoters = dis.readInt(); this.uhuraVotes = dis.readInt(); this.mccoyVotes = dis.readInt(); this.femaleFraction = dis.readFloat(); this.id = dis.readShort(); } public void write(DataOutputStream dos) throws IOException { dos.writeInt(this.registeredVoters); dos.writeInt(this.uhuraVotes); dos.writeInt(this.mccoyVotes); dos.writeFloat(this.femaleFraction); dos.writeShort(this.id); } public String toString() { return "id=" + this.id + ",registeredVoters=" + this.registeredVoters + ",uhuraVotes=" + this.uhuraVotes + ",mccoyVotes=" + this.mccoyVotes + ",femaleFraction=" + this.femaleFraction; } public static void main(String[] args) throws IOException { // Used to generate the data /* Precinct[] precincts = new Precinct[] { new Precinct((short)1, 1510, 315, 310, 0.52f), new Precinct((short)2, 704, 110, 125, 0.44f), new Precinct((short)3, 3714, 996, 506, 0.50f), new Precinct((short)4, 115, 96, 106, 0.36f), new Precinct((short)5, 19112, 6155, 7098, 0.47f), new Precinct((short)6, 39433, 13633, 8253, 0.56f), new Precinct((short)7, 2633, 933, 704, 0.55f), new Precinct((short)8, 1011, 254, 306, 0.49f), new Precinct((short)9, 257, 29, 78, 0.58f), }; FileOutputStream fos = new FileOutputStream("Precinct.ser"); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); for (Precinct p : precincts) p.write(dos); dos.flush(); dos.close(); */ int uhuraTotal = 0, mccoyTotal = 0, femaleTotal = 0, registeredVoters = 0; FileInputStream fis = new FileInputStream("Precinct.ser"); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream(bis); Precinct p = readObject(dis); while (p != null) { System.out.println(p); if (!p.hasFraud()) { uhuraTotal += p.getUhuraVotes(); mccoyTotal += p.getMccoyVotes(); registeredVoters += p.getRegisteredVoters(); femaleTotal += (int)((p.getUhuraVotes() + p.getMccoyVotes()) * p.getFemaleFraction()); } p = readObject(dis); } dis.close(); p = new Precinct((short)-1, registeredVoters, uhuraTotal, mccoyTotal, (float)femaleTotal / (uhuraTotal + mccoyTotal)); System.out.println(p); FileOutputStream fos = new FileOutputStream("PrecinctResults.ser"); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos); p.write(dos); dos.flush(); dos.close(); } }