Jump to content


I Need Help!


  • Please log in to reply
3 replies to this topic

#1 Murphy93

Murphy93

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 19 April 2012 - 03:53 PM

Hi,
I missed 2 classes in college on programming and im a bit behind. we have an assignment due next week and im stumped, i really dont know where to go  from where i am.

The assignment is to create a program that the user can input data about 10 separate workers hours and get their net pay. (£9.25/h)
This is what i have so far


import java.util.*;
class Assignment2{
public static void main (String[]args){
Scanner in = new Scanner(System.in);

int f[]=new int[10];

String employees[]= {"S.Patrick: " ,"A.Hogan: ", "B.Murphy: ", "I.Honan: ", "A.Brosnan: ", "P.Clancy: ", "S.Slater: ", "K.Ryan: ",          "E.Curtin: ", "J.Griffin: "};

System.out.print("Enter Hours Worked: ");
for(int j = 0; j<f.length; j=j+1){
f[j]=in.nextInt();
}

for(int j =0; j<f.length; j=j+1){
System.out.print(employees[j]);
System.out.println(f[j]);
}
  

}
}


but i still need to add 1) take home pay
                        2) total part-time hours worked that week by all employees
                        3) Sum of payroll for employees that week

the final output screen needs to be neat too.
any help would be really appreciated  

Thanks.

#2 nyccanuck

nyccanuck

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts

Posted 19 April 2012 - 09:50 PM

View PostMurphy93, on 19 April 2012 - 03:53 PM, said:

The assignment is to create a program that the user can input data about 10 separate workers hours and get their net pay. (£9.25/h)

This is what i have so far ...
... but i still need to add 1) take home pay
                            2) total part-time hours worked that week by all employees
                            3) Sum of payroll for employees that week

Thanks.

Hello,

I will not provide you with any code, just some guidance.

1. Use object / variable names that are useful.  Do not use i, j, or anything of the like.  Change your ‘i’ to ‘index’ or ‘person’.  Use names that are meaningful and understandable by someone reading your code.
2. When you are asking for the hours worked by the person, use their name.  “Enter hours worked by “ + employee name (print the name out of your employees array).  Your first for loop asks for input with no user name but does properly put it into the correct location of your ‘f’ array (again a proper name wouldn’t hurt).
3. Your next for loop prints out the name and the hours worked.  Think about how you would get the hours multiplied by the wage (if your wage is a constant, set it up as one in your declarations)
4. What do you mean by part-time hours? Less than 20 in a week per employee?  Again set-up a constant if this is defined and will not change.  If you just want a sum of the total hours worked, set up a variable and have it incremented every time you add an employee’s hours.
5. Sum of payroll. Again, set-up a variable and increment it.
6. Also, in you for loops you are using ‘j = j+1’, you can use just ‘j++’.  This will increment your j variable by 1.

Hopefully that will give you a little help to get you on your way.  Let me know how it works out and if you need any more help.

#3 Murphy93

Murphy93

    Newbie

  • Members
  • Pip
  • 2 posts

Posted 21 April 2012 - 02:34 PM

View Postnyccanuck, on 19 April 2012 - 09:50 PM, said:

Hello,

I will not provide you with any code, just some guidance.

1. Use object / variable names that are useful.  Do not use i, j, or anything of the like.  Change your ‘i’ to ‘index’ or ‘person’.  Use names that are meaningful and understandable by someone reading your code.
2. When you are asking for the hours worked by the person, use their name.  “Enter hours worked by “ + employee name (print the name out of your employees array).  Your first for loop asks for input with no user name but does properly put it into the correct location of your ‘f’ array (again a proper name wouldn’t hurt).
3. Your next for loop prints out the name and the hours worked.  Think about how you would get the hours multiplied by the wage (if your wage is a constant, set it up as one in your declarations)
4. What do you mean by part-time hours? Less than 20 in a week per employee?  Again set-up a constant if this is defined and will not change.  If you just want a sum of the total hours worked, set up a variable and have it incremented every time you add an employee’s hours.
5. Sum of payroll. Again, set-up a variable and increment it.
6. Also, in you for loops you are using ‘j = j+1’, you can use just ‘j++’.  This will increment your j variable by 1.

Hopefully that will give you a little help to get you on your way.  Let me know how it works out and if you need any more help.


Thanks for your reply it was very helpful in most ways but, i don't know how to set up a variable and have it incremented. have u any tips on where i could get an explanation or can u help me yourself
again thanks for the swift reply i really appreciate it

#4 nyccanuck

nyccanuck

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts

Posted 21 April 2012 - 05:35 PM

View PostMurphy93, on 21 April 2012 - 02:34 PM, said:

Thanks for your reply it was very helpful in most ways but, i don't know how to set up a variable and have it incremented. have u any tips on where i could get an explanation or can u help me yourself
again thanks for the swift reply i really appreciate it

You have already done both in your code in your for loop
(int j = 0; j<f.length; j=j+1)
Your j is your variable and your j=+1 is your increment.

Your variable you need will need to be created and initialized outside of your for loop - best at the top of your method (variables created inside you loops are local to that loop)
int totalHours = 0;

in your loop, when you add the hours of the employee, also add:
totalHours += f[j];

Your updated totalHours will be available at any time.  You can print it after each user has been entered if you like as well as any other time in your program.
System.out.println("The total hours worked by all employees is: " + totalHours);

Let me know if that helps.