maximum wage, minimum wage and average of all wages | Sololearn: Learn to code for FREE!

+1

maximum wage, minimum wage and average of all wages

how do i make my program to be able to calculate maximum wage, minimum wage and average of all wages? https://code.sololearn.com/c5h29RCDmM91/?ref=app

9/24/2021 12:44:19 PM

Bobby Dike

10 Answers

New Answer

+5

Bobby Dike, your code is suffering from the typical beginner mistake of being one huge monolithic slab of code. Not a bad first effort though. It is always better to break a problem down into manageable pieces though. Think about the individual steps that are required. 1. An employee is a distinct entity, an individual person, or object. Since Java is an object oriented language it makes sense to keep all of the data for an employee in a distinct class. 2. Break the operations down into distinct stages which will help you to identify methods and stages for your program such as: 2.a Getting the number of employees required. Since we will require multiple employee objects use a collection to store them all together e.g. an ArrayList. This is a List collection. 2.b Since multiple employees are required a method in the main program to add employees is a good idea. 2.c Displaying the list is another good candidate for a separate method. This method can be used during development to verify what we have entered and aid in debugging. 2.d Calculating the min, max, and total wages from the list are also good candidates for separate methods. This provides separation of concerns in our code. This means that we can edit code in one of the methods without messing up the code in another. This is very important in a multi stage problem such as this. 3. Since we are using a collection we can apply collection methods such as min and max to our collection. All that is necessary is to create a custom comparator class for our Employee class. With a complex object like an Employee it is preferable to use a custom Comparator class so that we can arrange collections in different ways (name, id, hours worked, highest rate, or highest pay). Here is a worked example for you to study... https://code.sololearn.com/cU6XFvEW5kTN Though I can see from your profile that you have only completed 13% of the Java course. So some of this may be beyond your level at the moment.

+2

You don't. Common way for traversing an array is by using for...loop.

+2

I'll be back later, I have to go again now. Please try to make that Employee class, so we can move on later ...

+2

Ipang I suggested and wrote class Employee before, you can use it https://www.sololearn.com/Discuss/2887429/?ref=app https://www.sololearn.com/Discuss/2888137/?ref=app

+2

Zemiak, Sorry about that, I forget a lot of things. I didn't even check his questions to notice ... #unsub

+2

Ipang I have already given solution as you explained in 1st reply. I think it is easy to do that. Bobby Dike You can use Java stream API to get min and max wages. I have done both things in my code. Now to get average you can get sum of all wages using Java Stream and after getting sum divide sum with length of employee list. Or you can use average method of stream class https://code.sololearn.com/cfXzaLDTMoCe/?ref=app

+2

Yes AJ, My bad I didn't check he had posted it earlier.

+1

I would create an Employee class and fill it with id, name, workHours etc. properties. Then I can create an array (or ArayList) of Employee class to contain the records. Next I would traverse the array to find the min & max wages, sum the wages and divide the result with number of array's elements to get the average.

0

how do i traverse the array using the if statement

0

okay, how do i write the statement. I've tried but there are too many errors. its in the program i posted