• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Please help - Printing the Employee id

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write Payroll class that uses the following private arrays as fields
employeeId – An array of five integers to hold employee identification numbers
hours – An array of five integers to hold the number of hours worked by each employee
payRate – An array of five doubles to hold each employees hourly par rate  
The class should relate the data in each array through the index/subscripts. Use appropriate set methods to initialize the array and get methods to return array.
The class should have method searchEmployee() that accepts an employee’s identification number as an argument and return the gross pay (Gross pay=hours*payRate) for that employee. Uses the sort and binarySearch method from the Arrays class to search the employee and return the gross pay for that employee.
The class should have method getHighest() method that find the highest pay rate of employee and return it.
Write Test class to create object of Payroll class and ask user to enter employee identification, hours worked and payRate of each employee. It should then display each employee’s identification number, hours, payRate and gross pay as per follows. Calculate gross pay=hours*payRate and store it in grossPay array.
Input validation: do not accept negative values for hours , numbers less than 6.00 for pay rate. Handle proper error If employee id does not match in searchEmployee() method

Following is sample run:
User loop to store following value in each array;
Enter emp id = 1 -> five different vale
Enter number of hours worked = 7      -> five different value
Enter employee hourly rate = 10.5      -> five different value

After storing value in each array print information as per follows:


Enter empid of employee which you want to calculate gross pay? 3
Gross pay of employee=274.5

Highest pay rate of employee=50.5


Or

Enter empid of employee which you want to calculate gross pay? 15
EmployeeId not present in array Index -6 out of bounds for length 5


In testpayroll, the line



pr.setpayRate() is giving me an error. when i type "payrate" in pr.setpaybrackets it also gives me an error. what can i do?






 
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olu idowu wrote:. . . class that uses the following private arrays as fields . . . relate the data in each array through the index/subscripts. . . .

Where do they set assignments like that requiring poor class design? Or no hint of object‑oriented design?

Your setXXX() methods cannot work as written; you would have to take the employee's id or an array index as a parameter too.
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made a java code:





I want to print my code as:
Enter emp id = 1 -> five different vale
Enter number of hours worked = 7      -> five different value
Enter employee hourly rate = 10.5      -> five different value

After storing value in each array print information as per follows:



but the only problem is that when it SHOULD be printing all the employees numbers but it prints out the last employee 5 times:
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olu idowu wrote:After storing value in each array...



You didn't put any arrays in your program. So you didn't store the employee ID, or any other employee information in those missing arrays. So when you get to the end of the input process, there aren't any arrays containing the employee information that you need.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

olu idowu wrote:After storing value in each array...



You didn't put any arrays in your program. So you didn't store the employee ID, or any other employee information in those missing arrays. So when you get to the end of the input process, there aren't any arrays containing the employee information that you need.



Can you show me how to do it?
 
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like:
Your loop counter needs to start at zero though.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since this is posted in "Java General", do you know how classes work?

I'm asking because that employee data is best put into an Employee class.
Then you can have an array of Employee (Employee[] employees = new Employee[value]).

The other thing that leaps out, your display loop is inside your data input loop.
It ought to be more:

If nothing else it removes the need for the if (i == value) check.
And at that point it should become clear that they ought to be different methods.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's still giving me error
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olu idowu wrote:it's still giving me error


This doesn't quite help us help you. We'd need to see your code in its current state and we'd need to see the complete (cut and paste) error message.
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find it useful to Google: java array tutorial
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest having a look at the java tutorials here : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olu idowu wrote:I made a java code: . . . .

But that is different from what you said you had been instructed to do, in your other thread. However bad the instructions might be, you will have to follow them if you want good marks.
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I print something only once in a for loop?


 

I want to put the code above in the this code below and i want the above code to print once at the end of bottom of the code (inside the for loop).

 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The obvious answer is to put it outside of the  for() loop, either before or after. If that is not possible then you'd need an if() that would be true at one point in the loop iterations.
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If this question was in regards to your other post(s) then I suggest in the future you continue your questions in your already existing threads.
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The obvious answer is to put it outside of the  for() loop, either before or after.


Caution: Sometimes conditions exist where the body of a loop is never even entered, having the print inside the loop may not be what you want in this case.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:The obvious answer is to put it outside of the  for() loop, either before or after. If that is not possible then you'd need an if() that would be true at one point in the loop iterations.



It gives me error if i put it outside the loop
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, then fix that error.

In all seriousness, we cannot help you if you don't show us the code that is giving the error.  Also, tell us what EXACTLY the error says.  You should try and make is as easy as possible for people to help you if you want help.  
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olu idowu wrote:. . .  this code below  . . .

That code won't work. You were told to create multiple arrays, which is bad design, but you have to do what you are told if you want good marks. You are not showing any sign of using the arrays in that loop. You did have arrays in your earlier code. You are going to have to put those data into the arrays.
By the way: the standard form for a for loop is this:-The reason for using that form of loop as a default is that it works. In the instance I showed you, it always works; it matches the structure of an array exactly. You may need to use a different loop header in some circumstances, but I don't believe there are special circumstances here. Don't start from 1 and don't use <=.
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . continue your questions in your already existing threads.

Agree. I shall merge the three threads.
 
Campbell Ritchie
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the forum software, on my behalf wrote:. . . I have merged your topic into this topic. . . .

We can now remind ourselves of the requirements. You appear not to have read them. Your first attempt at code might be better than what you posted more recently, but you neither created the number of arrays specified, nor the size of arrays specified. I know that one of Murphy's Laws reads,

When all else fails, read the instructions,

but in the case of assignments you need to read the instructions first.
 
olu idowu
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:well, then fix that error.

In all seriousness, we cannot help you if you don't show us the code that is giving the error.  Also, tell us what EXACTLY the error says.  You should try and make is as easy as possible for people to help you if you want help.  




why I post my question if i don't know how to fix my code?
 
Carey Brown
Bartender
Posts: 10964
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guidelines:
  • Do not start new threads for each question on the same project.
  • When you are discussing code that you have changed since the last time you posted the code, then it is time to post the current version of the code so we can keep in sync.
  • When you have an error message, cut and paste the entire error message into your post.
  • Try to follow suggestions offered to you and post the new code with your attempts for comments and suggestions.

  • Based on numerous previous suggestions I would expect to see new code from you showing how you turned each of the Employee fields into arrays.
     
    olu idowu
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    PLEASE HELP!
    How do i do this part at the end of the main method:
    This is an example:


    I have changed the code and this code works:
    Main Method



    PayRoll Method



     
    olu idowu
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    olu idowu wrote:PLEASE HELP!
    How do i do this part at the end of the main method:
    This is an example:


    I have changed the code and this code works:
    Main Method



    PayRoll Method




    Nevermind I got it
     
    Carey Brown
    Bartender
    Posts: 10964
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    This is problematic from several points. You are correct that you can't do a binary search unless the IDs are sorted. However, your structure has several arrays that need to be kept in sync and by sorting IDs that array is no longer in sync with the other arrays. I'm afraid that you'll need to do a basic linear search, in which case it would be best that it return the array index and then that index can be used by other methods for calculations, printing, etc.. Your "search" method is doing more than searching, it is also "calculating" grossPay. You should split off the grossPay calculation into its own method.

    Seeing as how your search method will now be returning an index, several of your methods may benefit from having an index added as one of the parameters. Example:
     
    Campbell Ritchie
    Marshal
    Posts: 80083
    412
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:. . . basic linear search . . .

    Since the arrays are supposed to have length = 5, a linear search is hardly going to cause slow execution. It will be quick enough to implement, too.
    There is a problem with arrays and many implementations of ID numbers: how will we know that the numbers are unique? Most search algorithms report the location of the first found element and don't test whether there are are any other matching elements.
     
    Carey Brown
    Bartender
    Posts: 10964
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Marshal
    Posts: 8988
    652
    Mac OS X Spring VI Editor BSD Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OP

    You have such method in PayRoll class as:
    public double getHighest()

    Now, put yourself into the shoes of user of the class, and try to guess what that may mean. Get highest what?

    After looking to three fields of PayRoll class: [1]employeeId, [2]hours, [3]rate; I also would have hard times to guess what getHighest() mean.


    Try to come up with something much more descriptive, so it would be absolutely obvious and unambiguous reading method name what it would return.


    Also similar story with getHours(). Is it total hours in a day/week/month/year? Is it actually worked hours by an employee? Something else?

     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic