Timed HW help, Need to write methods. I'll post them and the stuff my hw is based on.
Basis: public class Worker implements Comparable { public static final double WEEK = 40.0; // regular work week public static final int NUM_DAYS = 5; // days in 1 work week //////////////////////////////////// private String itsFirstName = null; private String itsLastName = null; private int itsBirthYear = 0; private double itsHourlyRate = 0; private double [] itsHoursPaid = {8, 8, 8, 8, 8}; // 1 per day /** Return the first name plus last name of the Worker. * But return null if it does not represent a real Worker. */ public String getName() { return (itsLastName == null) ? null : itsFirstName + " " + itsLastName; } //====================== /** Return a String value containing most or all of the * Worker's data, suitable for printing in a report. */ public String toString() { return itsLastName + ", " + itsFirstName + "; born " + itsBirthYear + ". rate = " + itsHourlyRate; } //====================== /** Record the hours worked in the most recent day. */ public void addHoursWorked (double hoursWorked) { for (int k = NUM_DAYS - 1; k > 0; k--) itsHoursPaid[k] = itsHoursPaid[k - 1]; itsHoursPaid[0] = hoursWorked; } //====================== /** Return the Worker's gross pay for the week. */ public double seeWeeksPay() { double sum = 0.0; for (int k = 0; k < NUM_DAYS; k++) sum += itsHoursPaid[k]; return sum <= WEEK ? itsHourlyRate * sum : itsHourlyRate * (sum * 1.5 - WEEK / 2); } //====================== public boolean equals (Worker par) { return par != null && this.itsLastName != null && this.itsLastName.equals (par.itsLastName) && this.itsFirstName.equals (par.itsFirstName); } //====================== public int getBirthYear() { return itsBirthYear; } //====================== /** Compare two workers based on their last names, * except based on their first names if the same last name. * Precondition: ob.getName() returns a non-null value. */ public int compareTo (Object ob) { Worker that = (Worker) ob; int comp = this.itsLastName.compareTo (that.itsLastName); return comp != 0 ? comp : this.itsFirstName.compareTo (that.itsFirstName); } //====================== /** Create a Worker from an input String, a single line with * first name, last name, year, and rate in that order. * If the String value is bad, the name is made null. */ public Worker (String par) { StringInfo si = new StringInfo (par); si.trimFront (0); if (si.toString().length() > 0) { itsFirstName = si.firstWord(); si.trimFront (itsFirstName.length()); itsLastName = si.firstWord(); si.trimFront (itsLastName.length()); itsBirthYear = (int) si.parseDouble (-1); si.trimFront (si.firstWord().length()); itsHourlyRate = si.parseDouble (-1); } if (itsBirthYear < 0 || itsHourlyRate < 0) itsLastName = null; // in case of bad input } //====================== } public class PersonnelDataV2 { private Buffin itsFile = new Buffin ("workers.txt"); ?private static final int TIME = 10; // part of listing 7.4 private static final int YEAR = 1960; // part of listing 7.4 public static final int MAX_WORKERS = 5000; // part of listing 7.7 // from listing 7.4 /** Read a file and return the string representation of the * number of Workers born in each of 1960 to 1969. */ public String countBirthYears() { int[ ] count = new int [TIME]; // all zeros //1 //== READ THE WORKER DATA AND UPDATE THE COUNTS Worker data = new Worker (itsFile.readLine()); //2 while (data.getName() != null) //3 { int lastDigit = data.getBirthYear() - YEAR; //4 count[lastDigit]++; // error check left as exercise//5 data = new Worker (itsFile.readLine()); //6 } //== CONSTRUCT THE STRING OF ANSWERS String s = ""; //8 for (int k = 0; k < TIME; k++) //9 s += (YEAR + k) + " : " + count[k] + " workers\n"; //10 return s; } // from listing 7.7 ?/** Read a file of up to 5000 Workers and display those who * make more than fifty percent above the average pay. */ public void printHighlyPaid() { //== INITIALIZE VARIABLES Worker[ ] item = new Worker[MAX_WORKERS]; //1 int size = 0; // number of workers in the array //2 //== READ THE WORKER DATA AND ADD EACH TO THE ARRAY Worker data = new Worker (itsFile.readLine()); //3 while (data.getName() != null && size < item.length) //4 { item[size] = data; //5 size++; //6 data = new Worker (itsFile.readLine()); //7 } //8 //== CALCULATE THE AVERAGE WEEK'S PAY double totalPay = 0; //9 for (int k = 0; k < size; k++) //10 totalPay += item[k].seeWeeksPay(); //11 double average = (size == 0) ? 0.0 : totalPay / size; //12 //== PRINT THOSE MAKING MORE THAN 50% OVER THE AVERAGE double highlyPaid = 1.5 * average; //13 String s = ""; //14 for (int k = 0; k < size; k++) //15 { if (item[k].seeWeeksPay() > highlyPaid) //16 s += item[k].toString() + "\n"; //17 } //18 JOptionPane.showMessageDialog (null, s); // Comparing age ( I did this myself) JOptionPane.showMessageDialog(null, "I need to comparethe age of our workers"); // calculations int wholeAge = 0; for (int k = 0; k < size; k++) { // using +=, getbirth year method wholeAge += item[k].getBirthYear(); double avgAge = (size == 0)?0.0 : wholeAge / size; } // printing and using the toString function ( I wrote this one myself) // go is a snappy name for my stringname String go =""; for (int k = 0; k < size; k++) { go += item[k].toString() + "\n"; } // like the example, using JOptionPane JOptionPane.showMessageDialog (null, go); } 7.29 public class WorkerList extends PersonnelDataV2 { private Worker[ ] itsItem; private int itsSize = 0; /** Create an empty list capable of holding max Workers. */ public WorkerList (int max) { itsItem = (max > 5) ? new Worker[max] : new Worker[5]; //1 } //====================== /** Add all Worker values in the non-null file to this list, * except no more than there is room for in the list. */ public void addFromFile (Buffin file) { Worker data = new Worker (file.readLine()); //2 while (data.getName() != null && itsSize < itsItem.length) { itsItem[itsSize] = data; //4 itsSize++; //5 data = new Worker (file.readLine()); //6 } //7 } //====================== /** Return the average pay for all workers in the list. * Return zero if the list is empty. */ public double getAverage() { double totalPay = 0; //8 for (int k = 0; k < itsSize; k++) //9 totalPay += itsItem[k].seeWeeksPay(); //10 return itsSize == 0 ? 0.0 : totalPay / itsSize; //11 } //====================== /** Return names of workers making more than the cutoff. */ public String thosePaidOver (double cutoff) { String s = ""; //12 for (int k = 0; k < itsSize; k++) //13 { if (itsItem[k].seeWeeksPay() > cutoff) //14 s += itsItem[k].toString() + "\n"; //15 } //16 return s; //17 } //====================== public Worker oldestWorker() { // returning oldest worker //String w = ""; //String o = ""; Worker older = itsItem[0], shark; // reference to itsItem (I didn't think of that last time) for(int k = 0; k < itsSize; k++) { shark =itsItem[k]; // using item[k], the important element if ((shark.getBirthYear() < older.getBirthYear())||k == 0) older = shark; } // returning the size andthe oldest value, using : ? return itsSize == 0 ? null : older; } 7.30 public boolean equals (WorkerList given) { for (int k = 0; k < itsSize; k++) { if (this.item[k].equals (given.item[k])) // if worker list { return true; JOptionPane.showMessageDialog (null, "Given has the same workers as WorkerList."); } } return given != null && this.item[k] != null && this.item[k].equals (given.iten[k]); } 7.31 public WorkerList highOnes() { // return a new WorkerList object containing its Worker objects: their pay is above average double avgPay = getAverage(); double average = getAverage(); Worker[] ana = new Worker[itsSize]; int k = 0; for (int k = 0; k < itsSize; k++) { // (this.seeWeeksPay() > this.getAverage()) if (item[k].seeWeeksPay() > average) // redid my if statement { return new WorkerList (ana); } }
Listing: Listing 7.4 A PersonnelData method using an array of counters public class PersonnelData // continued { private Buffin itsFile = new Buffin ("workers.txt"); private static final int TIME_SPAN = 10; private static final int YEAR = 1960; /** Read a file and return the string representation of the * number of Workers born in each of 1960 to 1969. */ public String countBirthYears() { int[ ] count = new int [TIME_SPAN]; // all zeros //1 //== READ THE WORKER DATA AND UPDATE THE COUNTS Worker data = new Worker (itsFile.readLine()); //2 while (data.getName() != null) //3 { int lastDigit = data.getBirthYear() - YEAR; //4 count[lastDigit]++; // error check left as exercise//5 data = new Worker (itsFile.readLine()); //6 } //7 //== CONSTRUCT THE STRING OF ANSWERS String s = ""; //8 for (int k = 0; k < TIME_SPAN; k++) //9 s += (YEAR + k) + " : " + count[k] + " workers\n"; //10 return s; //11 } //====================== }
1. Write an instance method for the Worker class to report the largest number of hours worked on any one of the past five days. Use the values stored in the instance variable double[] itsHoursPaid, an array of length 5.
0_0 Yup, my middle-schooled mind won't help here. . .
@perl
Join our real-time social learning platform and learn together with your friends!