Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (ellecullen):

Homework Help: Exercise 7.31 and 7.32 in Java. Exercise 7.31* Write a method public WorkerList highOnes() in WorkerList: The executor returns a new WorkerList object containing only its Worker objects whose pay is above average. 7.32 is in comments

OpenStudy (ellecullen):

Exercise 7.32* Write a method public void addAll (WorkerList given) in WorkerList: The executor adds all the Worker values from a WorkerList parameter to its own list, stopping only if its array becomes full. Throw an Exception if given is null.

OpenStudy (ellecullen):

@woodrow73 :)

OpenStudy (ellecullen):

I'm having trouble on how to do 7.32. I finished 7.31 today. :)

OpenStudy (ellecullen):

Book WorkerList: public class WorkerList { 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 }

OpenStudy (woodrow73):

Sure, so what's tripping you up?

OpenStudy (ellecullen):

I tripped on the logic for the executor to ad all the Worker values from a WorkerList parameter to its own list.

OpenStudy (woodrow73):

Do you need to use Arrays for this assignment? ArrayList would make life easier

OpenStudy (ellecullen):

I started like this: ublic void addAll (WorkerList given) { Worker[] cool = new Worker[itsSize];

OpenStudy (ellecullen):

Yes, I do need arrays :)

OpenStudy (woodrow73):

Well you're going to need a loop; and to keep adding the parameter WorkerList array elements until either *this* instance's isItem[] array is full, or you run out of the parameter WorkerList array elements to add

OpenStudy (ellecullen):

for loop? for (int k = 0; k < itsSize; k++) {

OpenStudy (woodrow73):

you can probably take care of stopping the loop pretty easy with a for loop once the parameter WorkerList has looped through all it's elements. Remember, you're dealing with multiple instances of the class - itsSize will refer to the size of the current instance, to get the size of the parameter WorkerList, you'll have to reference it via the object, like: ``` for (int k = 0; k < given.itsSize; k++) ```

OpenStudy (ellecullen):

okay, I get that. Now, we can do an if statement for the next part.

OpenStudy (woodrow73):

Since you're adding to the array in this instance, it's probably safest to figure out which elements currently aren't holding a value - that way we know both what elements we can add to, and how many elements max we can add - something like this; (also it's good practice to reference by an array's size by the array directly, not a different data value) ``` public void addAll (WorkerList given) { ArrayList<Integer> freeElements = new ArrayList<Integer>(); for(int i = 0; i < itsItem.length; i++) { if(itsItem[i] == null) freeElements.add(i); } for (int k = 0; k < given.itsItem.length && k < freeElements.size(); k++) { itsItem[freeElements.get(i)] = given.itsItem[i]; } } ``` So now it will stop looping once this instance of WorkerList is full, or we run out of elements from the given object. The next step is ensuring that given.itsItem[i]; isn't null

OpenStudy (ellecullen):

okay, Can we do that in an if statement, as in if (given.itsItem[I] == null) then it goes out else

OpenStudy (woodrow73):

Your teacher wants an exception to be thrown if any of the given itsItem array elements are null I think, right?

OpenStudy (woodrow73):

wait.. after re-reading the question, she wants an exception to be thrown if given is null, not it's itsItem arrays

OpenStudy (woodrow73):

You'll need a try/catch block if you're familiar with those.

OpenStudy (ellecullen):

no, I'm not familiar with those .

OpenStudy (woodrow73):

So null is basically a lack of value.. it was never initialized -- so what do you think happens if a null usage like this happens: ``` WorkerList wl; System.out.println(String.valueOf(wl.itsItem.length)); ```

OpenStudy (ellecullen):

the program could exit out......

OpenStudy (woodrow73):

I'd recommend ``` public void addAll (WorkerList given) { ArrayList<Integer> freeElements = new ArrayList<Integer>(); for(int i = 0; i < itsItem.length; i++) { if(itsItem[i] == null) freeElements.add(i); } try{ for (int k = 0; k < given.itsItem.length && k < freeElements.size(); k++) { itsItem[freeElements.get(i)] = given.itsItem[i]; } }catch(NullPointerException e) { e.printStackTrace(); } } ```

OpenStudy (woodrow73):

if given is null, the code will immediately jump to what's in the catch statement when given.itsItem.length is called

OpenStudy (ellecullen):

I agree. Thanks for the help :)

OpenStudy (woodrow73):

And e.printStackTrace(); will print out a cohesive-esque error report to console -- and I know I already said it, but the homework code is an abomination.. bad coding practice. I'd almost rewrite it for perfection...

OpenStudy (woodrow73):

The stuff your teacher is giving you

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!