Homework help: Please help ASAP Exercise 7.24 on Arrays: Modify Listing 7.7 to print all Workers who are older than the average Worker. Listing is in comments.
Listing: public class PersonnelData { private Buffin itsFile = new Buffin ("workers.txt"); private static final int TIME_SPAN = 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 /** Read a file and return the name of the Worker that is * alphabetically first. */ public String findEarliestWorker() { Worker answerSoFar = new Worker (itsFile.readLine()); //1 if (answerSoFar.getName() == null) //2 return "no workers!"; //3 else //4 { Worker data = new Worker (itsFile.readLine()); //5 while (data.getName() != null) //6 { if (data.compareTo (answerSoFar) < 0) //7 answerSoFar = data; //8 data = new Worker (itsFile.readLine()); //9 } //10 return answerSoFar.getName() + " is first of all."; //11 } //12 } //====================== // 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_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 } //== 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; } // 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); } }
@Ashleyisakitty
I decided to start with calculating the average year born. Please check my logic, and you are welcome to add corrections.
My Logic with Code: public void printOlder() { //== Calculate average year born double age = YEAR; String o = ""; for(int k = 0; k < YEAR; k--) { if (item[k].countBirthYears() < YEAR) { o += item[k].toString() + "\n"; } } JOpotionPane.showMessageDialog (null, o); }
You still did not use the code quoting system. You need to use the ``` abive and below the code blocks or I can't copy it to test because it pastes on one line and the comments mess everything up.
public void printOlder() `````````````````` { //== Calculate average year born `````````````````````````````` double age = YEAR; ```````````````````````` String o = ""; ````````````````````` for(int k = 0; k < YEAR; k--) `````````````````````````````` { `````````````````````````````` if (item[k].countBirthYears() < YEAR) ``````````````````````````````````````` { ````````````````````````````````` o += item[k].toString() + "\n"; ```````````````````````````````````` } ``````````````` } ``````````````` JOptionPane.showMessageDialog (null, o); `````````````````````````````````````````` is this ok?
just put three tlides before and after the code \(```\\foo \\ ``\implies\) ``` foo ```
Assuming YEAR is positive; ``` for(int k = 0; k < YEAR; k--) ``` will cause an infinite loop
Yes, it will. I thought I might need it in my logic for the exercise.
I broke the exercise down into two parts, 1. calculate the average worker birth year 2. Print all workers who are older than the average Worker
@e.mccormick what do you think of my logic so far? I'm having trouble coming up with the formula for printing all workers who are older than the average Worker
Loop through them. Use an if statment to test. Inside the body of the loop do the print.
This is a poorly coded example, but relates to how: ``` for(i = 0; i < 10; i++){ if(i > 5){ print i; } }
got it :) Thanks to everyone :)
Join our real-time social learning platform and learn together with your friends!