This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/5/3

bad operand types for binary operator

divinity divinity

2016/5/3

#
hi danpost i am practicing this array program where it is say calculate a payroll for a set of employees. I have decided to use a while loop for 3 employees instead of the for loop. in the program you have to prompt for the employees name etc but they have to declare it using arrarys. while doing the calculation part of the program, (i use the if else (selection construct) ) but i am stomp. i got some syntax errors such as, incomparable types: double and int bad operand for binary operator * (multiplication symbol) bad operand for binary operator > how can i fix that here is the codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
char gender[] = new char[m];
        String employee_firstname[] = new String[8];
        String employee_lastname[] = new String[8];
        double hrswrk[]= new double[3];
        double deductions[]= new double[5];
        double rate_ofpay[]= new double[3];
         
        while(i < 3)
        {
            i++;
             
            System.out.println("please enter gender or a to be terminated  ");
            gender[i] = reader.next().charAt(0);
             
            System.out.println("please enter employee first name");
            employee_firstname[i] = reader.next();
 
            System.out.println("please enter employee last name");
            employee_lastname[i] = reader.next();
 
            System.out.println("please enter hours of work");
            hrswrk[i] = reader.nextDouble();
 
            System.out.println("please enter deductions");
            deductions[i] = reader.nextDouble();
 
            System.out.println("please enter the rate of pay");
            rate_ofpay[i] = reader.nextDouble();
             
            if(hrswrk == 40)
            {  
                hrs = (hrswrk * rate_ofpay);
            }
            else if(hrswrk > 40)
            {
                double overtime = (1.5 * rate_ofpay)
            }
            else
            {
                net_salary = (gross_salary - deductions)
            }
danpost danpost

2016/5/3

#
Well, just about all the variables within the lines 30 through 41 refer to arrays; but, you are using them like double values instead. You need to specify an index value for them (hrswrk, rate_ofpay, and deductions) to use them in mathematical calculations.
divinity divinity

2016/5/3

#
hi danpost i got to fix the error and thank you for pointing that out to me, the things is it is now outputting zero, dont know what. i thinks i probably mis skewed the calculation,, can you help me out there and another think. the program has to be terminated by a string rogue value how do you terminated a program by using a string rogue value. ah try using A as the string rogue value but it is not working, cuz the progrm still continue to run even when ah enter the string value A
danpost danpost

2016/5/3

#
The gross_salary field used on line 40 is never assigned any value. The overtime variable is not used after assigned a value at line 36. The hrs field is only assign a new value (default is zero) if the hours worked was exactly 40. A consequence of this might show some other issues. Let us say that the first employee worked exactly 40 hours and the hrs field is assigned its proper value. Then the next employee, who did not work 40 hours, will not re-assign a new value to the field. The value will remain what it was for the first employee. Certainly, this cannot be what you would want. You should zero any fields that require it at the beginning of the loop (after the 'while' statement). I think the 'if-else if-else' set-up should be avoided here. You can calculate the gross pay in a straight-forward manner. Assuming that no employee will ever work at least 80 hours, then you can use int division to result in a 0 or 1 when dividing 40 divides into the hours worked . Multiply that by the number of hours worked minus 40 and then divide that in half (and a half). Add that to hours worked times rate of pay (time). The result is:
1
payBeforeDeductions = (hrs*payrate)+(hrs/40)*(hrs-40)*payrate/2;
This might be considered gross salary (although hourly pay is not salaried). We can actually skip that step however and go right to net salary (net pay):
1
netPay = (hrs*payrate)+(hrs/40)*(hrs-40)*payrate/2-deductions;
I did not code like to be placed directly into your code or replace your code as is. I did not take into account that some of these fields are contained within arrays, for one thing. Also, some field names have been changed.
You need to login to post a reply.