please help, quick question

Okay my problem is writing a code for payrole. I have to write the code to be 1.5 basepay for every overtime hour between 41 and 60, and be 2 times basepay for every hour over 60. I have done that, which isnt the problem....

I am using a for loop to allow the user to select the number of employees they want to do at one time. However I am a bit perplexed. After the for loop is over I need to be able to add all the salarys together and come up with a total payrole salary, and I just do not understand how to do that, I will post my java code, sorry if its a little rough, I am just beginning.

import java.util.Scanner;

public class FigureWages {

public static void main(String[] args){

Scanner input =new Scanner(System.in);

System.out.print("Enter Number of Employees= ");

double NumbEmploy = input.nextDouble();

for (int i = 0; i < NumbEmploy; i++)

{

System.out.print("Enter Employee Number= ");

double EmployNumb = input.nextDouble();

System.out.print("Enter Base Pay= ");

double basepay = input.nextDouble();

System.out.print("Enter Hours Worked= ");

double hours = input.nextDouble();

if (hours < 60 )

{

double overtime1= ((hours-40)*(1.5*basepay)) + (40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

}

} System.out.println("Hours Worked= "+hours);

System.out.println("Pay= " + overtime1);

}

else if (hours > 60)

{

double overtime1= ((hours-60)*(2*basepay))+(20*(1.5*basepay))+(40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= "+ overtime1);

}

}

[1855 byte] By [Mediocrerevolutiona] at [2007-11-26 22:35:41]
# 1
Create a sum variable at the beginning of the main method. Whenever you calculate a salaray, add it to the total. After you've finished processing each employee, you'll have the sum of all the salaries.
hunter9000a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 2
um.. i think i undestand what you mean.... but.. im not really sure how to do that...... this class is frustrating the heck out of me
Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 3

> um.. i think i undestand what you mean.... but.. im

> not really sure how to do that...... this class is

> frustrating the heck out of me

If you think you understand, then try to implement it. When you get stuck, post your renewed code here. Please use code-tags while posting code:

http://forum.java.sun.com/help.jspa?sec=formatting

prometheuzza at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 4

import java.util.Scanner;

public class FigureWages {

public static void main(String[] args){

// create a sum variable here to keep track of the running total

Scanner input =new Scanner(System.in);

System.out.print("Enter Number of Employees= ");

double NumbEmploy = input.nextDouble();

for (int i = 0; i < NumbEmploy; i++)

{

System.out.print("Enter Employee Number= ");

double EmployNumb = input.nextDouble();

System.out.print("Enter Base Pay= ");

double basepay = input.nextDouble();

System.out.print("Enter Hours Worked= ");

double hours = input.nextDouble();

if (hours < 60 )

{

double overtime1= ((hours-40)*(1.5*basepay)) + (40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

// you've got some errors between here...

}

}

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= " + overtime1);

}

// ...and here. The else if has to come right after the if

else if (hours > 60)

{

double overtime1= ((hours-60)*(2*basepay))+(20*(1.5*basepay))+(40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= "+ overtime1);

}

// now that you've calculated the total salary for this employee, add that salary to the sum. Once you finish the loop, the sum will contain the total salary for all employees.

}

hunter9000a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 5

yes i realized i had a posting error in the lower part, the code works properly and complies without a problem, i messed it up when i copy and pasted it, and thank you for the help, i understand that i need to create a sum variable, not absolutely sure how to implement it, but hopefully im on the right track now, thank you

Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 6

i feel really stupid, im sorry to keep asking for help, i just cant understand how to make the variable sum read and add together my pay outputs

i really hope its not something completely obvious, although im sure it is, im sorry that the wonderful help all have already given me isn't enough to make things click in my head, im still just learning :-\

Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 7

Theres nothing special about it, it's just a double that you initialize to 0. Something like

int double = 0;

Just use the += operator to add the salary to the sum variable.

Changed int to double

Message was edited by:

hunter9000

hunter9000a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 8

okay i understand how to declare a variable

so does that i mean i need to change my code throughout my program?

like inside the part of the code where I am figuring salary, that way it identifies that it is what is needed to be added together to be the payroll?

I am just having a hard time wrapping my mind around how the programed variable can be read in the loop to understand the end result and add all the end results together

Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 9

> Theres nothing special about it, it's just a double

> that you initialize to 0. Something like

> int double = 0;

> Just use the += operator to add the salary to the sum

> variable.

>

> Changed int to double

> Message was edited by:

> hunter9000

hunter9000 meant to say:double sum = 0;

If you now do// ...

sum += 1.5; // the same as: sum = sum + 1.5;

sum += 2.5; // the same as: sum = sum + 2.5;

sum += 2.0; // the same as: sum = sum + 2.0;

// now 'sum' equals 6.0

prometheuzza at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 10

> > Theres nothing special about it, it's just a

> double

> > that you initialize to 0. Something like

> > int double = 0;

> > Just use the += operator to add the salary to the

> sum

> > variable.

> >

> > Changed int to double

> > Message was edited by:

> > hunter9000

>

> hunter9000 meant to say:double sum = 0;

Thanks prometheuzz. When I reread the code, I edited my reply to change the type from int to double so the OP wouldn't be confused, but managed to make it worse. Yay me!

hunter9000a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 11

//Created by Chase LeBlanc

import java.util.Scanner;

public class FigureWages {

public static void main(String[] args){

double sum;

Scanner input =new Scanner(System.in);

System.out.print("Enter Number of Employees= ");

double NumbEmploy = input.nextDouble();

for (int i = 0; i < NumbEmploy; i++)

{

System.out.print("Enter Employee Number= ");

double EmployNumb = input.nextDouble();

System.out.print("Enter Base Pay= ");

double basepay = input.nextDouble();

System.out.print("Enter Hours Worked= ");

double hours = input.nextDouble();

if (hours < 60 )

{

double overtime1= ((hours-40)*(1.5*basepay)) + (40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= " + overtime1);

}

else if (hours > 60)

{

double overtime1= ((hours-60)*(2*basepay))+(20*(1.5*basepay))+(40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= "+ overtime1);

}

}

System.out.println("Total Payrole= ");

System.out.println("Payrole by Chase LeBlanc");

}

}

okay ive made those changes, but i know inside for loop brackets things are only ture inside the brackets and no outside

i know i need to add sums, but i do not know how to make the program read the pay value out of the for loops

Message was edited by:

Mediocrerevolution

Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 12
Hello, why don磘 you try declaring yor variables as global instead of locals?.
sophie_04a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 13
> Hello, why don磘 you try declaring yor variables as> global instead of locals?.even if i declared the variable outside of the loop how would i instruct the program to add the results of the product of each repeated loop though?
Mediocrerevolutiona at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 14
You should insert a variable called sum in each if block, obviously this variable should be declared as global so it doesn磘 matter which block accesses the program this variable always would be incremented.
sophie_04a at 2007-7-10 11:45:10 > top of Java-index,Java Essentials,Training...
# 15
What if an employee has exactly 60 hours. I mean, how do you treat that condition?
sophie_04a at 2007-7-21 19:09:02 > top of Java-index,Java Essentials,Training...
# 16

> You should insert a variable called sum in each if

> block, obviously this variable should be declared as

> global so it doesnt matter which block accesses the

> program this variable always would be incremented.

okay so i can implement a sum variable assigned to the paysum

but then how would i write the code to add each pay sum from each empolyee?

Mediocrerevolutiona at 2007-7-21 19:09:02 > top of Java-index,Java Essentials,Training...
# 17

the code is able to iterpret 40 hours or less which equals regular pay, 41 to 60 with 1.5 times pay, and 60 plus with 2 times pay for just the extra hours over 60 and 1.5 for those between 41 and 60

done with these equations

double overtime1= ((hours-40)*(1.5*basepay)) + (40*basepay);

double overtime1= ((hours-60)*(2*basepay))+(20*(1.5*basepay))+(40*basepay);

the problem is i have it set where i could do anywhere from 1 to a million employees and i have to be able to add the result of each employee's salary together for the total payroll

Mediocrerevolutiona at 2007-7-21 19:09:03 > top of Java-index,Java Essentials,Training...
# 18

> ...

> the problem is i have it set where i could do

> anywhere from 1 to a million employees and i have to

> be able to add the result of each employee's salary

> together for the total payroll

Perhaps I am missing something, but what about this:

public class FigureWages {

public static void main(String[] args) {

double sum, overtime1;

// ...

for (int i = 0; i < NumbEmploy; i++) {

// ...

if (hours < 60 ) {

overtime1 = ...

}

else if (hours > 60) {

overtime1 = ...

}

sum = sum + overtime1; // <- !!!!!

}

}

}

prometheuzza at 2007-7-21 19:09:03 > top of Java-index,Java Essentials,Training...
# 19

Hi, I realized that you just want to know how much money you are going to pay according with the total worked hours, right?, so why dont you try declaring an array that stores thet worked hours for each employee?, lets see

Scanner sc=new Scanner(System.in);

int numberOfEmployees,workedHours;

double payment,totalHours;

int[] workedHours;

//here you can insert a question about the number of employees

System.out.print("Number of employees: ");

numberOfEmployees=sc.nextInt();

//according with this information you can define your array

workedHours==new int[numberOfEmployees];

//and after this you should process each element (employee) as you came doing, i mean

for(int j=0;j<numberOfEmployees;j++){

System.out.print("Number of worked hours by employee "+j);

workedHours=sc.nextInt();

}

for(int j=0;j<numberOfEmployees;j++){

// Here is your validation code, with something like this

payment+=your comprobation on each if block

//and finally, you insert the previously declared totalHours variable

totalHours+=payment;

}

System.out.print("The total money payroll is: "+totalHours);

I hope been enough clear, greetings>

sophie_04a at 2007-7-21 19:09:03 > top of Java-index,Java Essentials,Training...
# 20

Look at the lines of code marked by:

/**/

Hope this helps.

//Created by Chase LeBlanc

import java.util.Scanner;

public class FigureWages {

public static void main(String[] args){

/**/double sum = 0;

Scanner input =new Scanner(System.in);

System.out.print("Enter Number of Employees= ");

double NumbEmploy = input.nextDouble();

for (int i = 0; i < NumbEmploy; i++)

{

System.out.print("Enter Employee Number= ");

double EmployNumb = input.nextDouble();

System.out.print("Enter Base Pay= ");

double basepay = input.nextDouble();

System.out.print("Enter Hours Worked= ");

double hours = input.nextDouble();

if (hours < 60 )

{

double overtime1= ((hours-40)*(1.5*basepay)) + (40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= " + overtime1);

/**/sum += overtime1;

}

else if (hours > 60)

{

double overtime1= ((hours-60)*(2*basepay))+(20*(1.5*basepay))+(40*basepay);

System.out.println("Employee Number= "+EmployNumb);

System.out.println("Base Pay= "+ basepay);

System.out.println("Hours Worked= "+hours);

System.out.println("Pay= "+ overtime1);

/**/sum += overtime1;

}

}

/**/System.out.println("Total Payrole= " + sum);

System.out.println("Payrole by Chase LeBlanc");

}

}

Message was edited by:

mswitzer

mswitzera at 2007-7-21 19:09:03 > top of Java-index,Java Essentials,Training...