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

2016/2/4

why is this program is outputting a zero output

divinity divinity

2016/2/4

#
I am doing this program as practice and who know i might just get this as a exam. i am wondering why i am getting a zero output. I have turn it around and looking thru the codes to see where and why but cannot say for sure. here is my codes. it is just a simple program. any help wud be appreciate.
double hrs, hrlyrate,taxrate=0,total_deduction, gross_salary=0, net_salary=0;
        double NIS=0, health_surcharge = 0, rateof_pay =0;
        
        System.out.println("please enter first name");
        String firstname = userinput.next();
        
        System.out.println("please enter your last name");
        String lastname = userinput.next();
        
        System.out.println("please enter hours worked");
        double hrswrk = userinput.nextDouble();
        
        System.out.println("please enter hourly rate ");
        hrlyrate = userinput.nextDouble();
        
        hrs = (hrswrk * hrlyrate);
        hrs = (hrswrk * rateof_pay);
        taxrate = (gross_salary * 0.25);
        total_deduction =(NIS + health_surcharge);
        net_salary = (gross_salary - total_deduction);
        
        System.out.println("the total deduction is "+total_deduction);
        System.out.println("the gross salary is "+gross_salary);
        System.out.println("the net salary is "+net_salary);
    }
danpost danpost

2016/2/4

#
Lines 1 and 2 declare most of your variables -- and all start with a value of zero. A new variable, 'hrswrk' is declared and assigned some value at line 11 and line 14 assigns some value to 'hrlyrate'. Line 16 multiplies these two "some" values and assigns the product to the 'hrs' variable; but, then line 17 assigns a different product to the variable -- one whose value is zero (the value of the 'rateof_pay' field is still zero). Line 18 assigns zero to 'taxrate' because 'gross_salary' is still zero. Line 19 assigns zero to 'total_deduction' as both 'NIS' and 'health_surcharge' are still zero. Finally, line 20 assigns zero to 'net_salary' as both variables within the assigned expression have values of zero.
danpost danpost

2016/2/4

#
Of note: the 'hrs' variable, after being assigned a zero value at line 17, is never used again. It is suspicious that both lines 16 and 17 are trying to assign different values to the same variable -- one right after another. I really do not think that 'hrs' really describes either of the expressed values assigned to it.
divinity divinity

2016/2/4

#
so what do i do to fix it. do i have to delete anything or fix or add
danpost danpost

2016/2/4

#
Again, half of the variables you are using are not assigned anything at all ( 'gross_salary', 'NIS', 'health_surcharge' and 'rateof_pay' ). Then, with unassigned values (set to zero), some are used to assign values to other variables ( 'taxrate' and 'total_deduction' ) evaluated to zero. Finally, 'net_salary' is evaluated from a combination of these variables to zero.
divinity wrote...
so what do i do to fix it. do i have to delete anything or fix or add
You need to have some non-zero values set to your variables -- starting with 'NIS', 'health_surcharge' and 'rateof_pay'. Then 'gross_salary' needs to be assigned to an expression that evaluates to something.
davmac davmac

2016/2/4

#
It looks to me like 'rateof_pay' and 'hrlyrate' are conflated - do you really need two separate variables? What are each of the variables (including all the other variables) supposed to represent, and how should they be calculated? If you can answer the above, you can fix your problem by:
  • removing any variables that are not needed or which are redundant
  • ensuring that the variable assignments in the code reflect the calculations required for each of the variables.
You need to login to post a reply.