-3

So im trying to do a school project and im running into problem and im just really confused so im asking ehre Im really new and I'm not exactly sure if what im doing is correct or not but its saying that the variable compensation and SSS is not initialized why is that? and what should I do to fix? again im new so I might be doing something stupid probably not doing the if statements correctly?

import java.util.Scanner;

public class Netpay
{
    public static void main(String args[])

    {
        Scanner scanner = new Scanner(System.in);

        double Gross,SSS,Tax,Philhealth,TotalDeduction,taxableincome,compensation,percentage,Netpay;
        int pagibig = 50;

        System.out.println("Please enter the gross basic pay of the employee:" );
        Gross = scanner.nextDouble();
        

Philhealth = Gross*0.02;


        

if (Gross <= 4249.0)

SSS = 180.0/2.0;
if (Gross >= 4250.0 && Gross <= 4749.99)
SSS = 202.5/2.0;
if (Gross >= 4750.0 && Gross <= 5249.99)
SSS = 225.0;
if (Gross >= 5250.0 && Gross <= 5749.99)
SSS = 247.5/2.0;
if (Gross >= 5750.0 && Gross <= 6249.99f)
SSS = 270.0/2.0;
if (Gross >= 6250.0 && Gross <= 6749.99f)
SSS = 292.5/2.0;
if (Gross >= 6750.0 && Gross <= 7249.99f)
SSS = 315.0/2.0;
if (Gross >= 7250.0 && Gross <= 7749.99)
SSS = 337.5/2.0;
if (Gross >= 7750.0 && Gross <= 8249.99)
SSS = 360.0/2.0;
if (Gross >= 8250.0 && Gross <= 8749.99)
SSS = 382.5/2.0;
if (Gross >= 8750.0 && Gross <= 9249.99)
SSS = 405.0/2.0;
if (Gross >= 9250.0 && Gross <= 9749.99)
SSS = 427.5/2.0;
if (Gross >= 9750.0 && Gross <= 10429.99)
SSS = 450.0/2.0;
if (Gross >= 10250.0 && Gross <= 10749.99)
SSS = 472.5/2.0;
if (Gross >= 10750.0 && Gross <= 11249.99)
SSS = 495.0/2.0;
if (Gross >= 11250.0 && Gross <= 11749.99)
SSS = 517.5/2.0;
if (Gross >= 11750.0 && Gross <= 12249.99)
SSS = 540.0/2.0;
if (Gross >= 12250.0 && Gross <= 12749.99)
SSS = 562.5/2.0;
if (Gross >= 12750.0 && Gross <= 13249.99)
SSS = 585.0/2.0;
if (Gross >= 13250.0 && Gross <= 13749.99)
SSS = 607.5/2.0;
if (Gross >= 13750.0 && Gross <= 14249.99)
SSS = 630.0/2.0;
if (Gross >= 14250.0 && Gross <= 14749.99)
SSS = 652.5/2.0;
if (Gross >= 14750.0 && Gross <= 15249.99)
SSS = 675.0/2.0;
if (Gross >= 15250.0 && Gross <= 15749.99)
SSS = 697.5/2.0;
if (Gross >= 15750.0 && Gross <= 16249.99)
SSS = 720.0/2.0;
if (Gross >= 16250.0 && Gross <= 16749.99)
SSS = 742.5/2.0;
if (Gross >= 16750.0 && Gross <= 17249.99)
SSS = 765.0/2.0;
if (Gross >= 17250.0 && Gross <= 17749.99)
SSS = 787.5/2.0;
if (Gross >= 17750.0 && Gross <= 18249.99)
SSS = 810.0/2.0;
if (Gross >= 18250.0 && Gross <= 18749.99)
SSS = 832.5/2.0;
if (Gross >= 18750.0 && Gross <= 19249.99)
SSS = 855.0/2.0;
if (Gross >= 19250.0 && Gross <= 19749.99)
SSS = 877.5/2.0;
if (Gross >= 19750.0 && Gross <= 20249.99)
SSS = 900.0/2.0;
if (Gross >= 20250.0 && Gross <= 20749.99)
SSS = 900.0/2.0;
if (Gross >= 20750.0 && Gross <= 21249.99)
SSS = 900.0/2.0;
if (Gross >= 21250.0 && Gross <= 21749.99)
SSS = 900.0/2.0;
if (Gross >= 21750.0 && Gross <= 22249.99)
SSS = 900.0/2.0;
if (Gross >= 22250.0 && Gross <= 22749.99)
SSS = 900.0/2.0;
if (Gross >= 22750.0 && Gross <= 23249.99)
SSS = 900.0/2.0;
if (Gross >= 23250.0 && Gross <= 23749.99)
SSS = 900.0/2.0;
if (Gross >= 23750.0 && Gross <= 24249.99)
SSS = 900.0/2.0;
if (Gross >= 24250.0 && Gross <= 24749.99)
SSS = 900.0/2.0;
if (Gross >= 24750.0 && Gross <= 25249.99)
SSS = 900.0/2.0;
if (Gross >= 25250.0 && Gross <= 25749.99)
SSS = 900.0/2.0;
if (Gross >= 25750.0 && Gross <= 26249.99)
SSS = 900.0/2.0;
if (Gross >= 26250.0 && Gross <= 26749.99)
SSS = 900.0/2.0;
if (Gross >= 26750.0 && Gross <= 27249.99)
SSS = 900.0/2.0;
if (Gross >= 27250.0 && Gross <= 28249.99)
SSS = 900.0/2.0;
if (Gross >= 28250.0 && Gross <= 28749.99)
SSS = 900.0/2.0;
if (Gross >= 28750.0 && Gross <= 29249.99)
SSS = 900.0/2.0;
if (Gross >= 29250.0 && Gross <= 29749.99)
SSS = 900.0/2.0;
if (Gross >= 29750.0)
SSS = 900.0/2.0;




if (Gross <= 10417.0)
compensation = 0.0;
percentage = 0.0;
if (Gross > 10417.0 && Gross <=16666.0)
compensation = 10417.0;
percentage = 0.2;
if (Gross >=16667.0 && Gross <=33332.0)
compensation = 16667.0;
percentage = 0.25;
if (Gross >= 33333.0 && Gross <=83332.0)
compensation = 33333.0;
percentage = 0.3;
if (Gross > 83333.0 && Gross <=333332.0)
compensation = 83333.0;
percentage = 0.32;
if (Gross >= 333333.0)
compensation = 333333.0;
percentage = 0.35;

taxableincome = pagibig + Philhealth + SSS;
Tax = (taxableincome - compensation)*percentage;


TotalDeduction = Tax + taxableincome;
Netpay = Gross - TotalDeduction;
System.out.println("Your Employees Netpay is:" + Netpay);
    }

}
wraith
  • 1
  • 1
  • `double Gross,SSS,Tax,Philhealth,TotalDeduction,taxableincome,compensation,percentage,Netpay` you are not setting a value here, thus having the variables not initialized. Also, `percentage` will always be 0.35 – XtremeBaumer Mar 16 '23 at 15:44
  • 5
    What if `Gross` is `4249.5`? Your code is riddled with logical gaps like that. And you need to learn about `else`. – Elliott Frisch Mar 16 '23 at 15:44
  • 4
    [Traditional reminder: don't use floats for money.](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – teapot418 Mar 16 '23 at 15:46
  • Should `10429.99` be `10249.99` ? Should `SSS = 225.0;` be `SSS = 225.0/2.0`?; – Old Dog Programmer Mar 18 '23 at 18:44

3 Answers3

1

Consider one example from your code:

if (Gross >= 15250.0 && Gross <= 15749.99)
SSS = 697.5/2.0;
if (Gross >= 15750.0 && Gross <= 16249.99)
SSS = 720.0/2.0;

Given the imprecise nature of floating point numbers, this leaves a gap if Gross is larger than 15749.99 but less than 15750.0.

You would do better to change the conditions to something like:

if (Gross >= 15250.0 && Gross < 15750.0)
SSS = 697.5/2.0;
if (Gross >= 15750.0 && Gross < 16250.0)
SSS = 720.0/2.0;
Chris
  • 26,361
  • 5
  • 21
  • 42
0

The local variable should always be initialised before using it but only the instance variable can use without initialising it.

Check this article - Difference between Instance Variable and Local Variable

Change two things to make your code work

1- initialize all your variables

 double Gross, SSS, Tax, Philhealth, TotalDeduction, taxableincome, compensation, percentage, Netpay;
        Gross = SSS = Tax = Philhealth = TotalDeduction = taxableincome = compensation = percentage = Netpay = 0.0;

2- add Local.Root to Scanner, See Localized numbers

 Scanner scanner = new Scanner(System.in).useLocale(Locale.ROOT);
ARNR
  • 43
  • 4
  • 2
    I think it is important to note that it does _not_ need to be initialized on declaration, it just needs to be initialized in all possible branches before used. If he had an `else` statement go along with each `if` that set the values of `sss` and `compensation` (or a single chain of `if-else` that ends with an `else`), there would not be a compilation failure as they are guaranteed to have a value before use even if you do not declare it at the start. – Nexevis Mar 16 '23 at 15:52
0

Other answers and comments have explained the problem with the use of floating point variables in your code. Basically, the compiler asks "Is there any potential value of Gross that will cause all the if statements that control initialization of SSS to skip initializing SSS. At the risk of going off-topic, I'd like to give an answer showing alternatives.

That is a lot of if statements. When I see repeated code, I consider alternatives. In your case, the repeated code is of the form

 if (Gross >= a && Gross <= b)
     SSS = c/2.0;

where a, b, and c are replaced with different double literals.

What are the alternatives to a long sequence of these if statements? I'd like to draw your attention to two of them.

Alternative One: Use Arithmetic

The difference between successive values for Gross in the if statements is predictable. Given one value, the next can be calculated. The same is true for the values of SSS. This means that chain of if statements can be reduced to calculations using arithmetic. In this case, the difference between successive values of Gross is 500.00. The difference between successive values assigned to SSS, prior to dividing by 2.0 is 22.5. So, SSS can be calculated directly from Gross. There is maximum and minimum on SSS, but that can be fixed after some calculations.

 final double GROSS_BASE_SSS = 4250.0;
 final double GROSS_INCREMENT_SSS = 500.0;
 final double SSS_BASE = 180.0;
 final double SSS_INCREMENT = 22.5;

 double SSS = ((   Math.floor((  Gross - GROSS_BASE_SSS ) 
                               / GROSS_INCREMENT_SSS) + 1.0)
                 * SSS_INCREMENT + SSS_BASE) / 2.0;
SSS = Math.max(180.0 / 2.0, Math.min(SSS2, 900.0 / 2.0));
     
 

By the way, I recommend an additional restriction: Don't allow the total reductions from Gross to exceed Gross. It isn't nice to have a negative net pay.

Replacing the chain of if statements with a few lines of calculation was pretty straight forward. But, what if it is difficult to find formulae that will predict the next value in a sequence when one value is known? Consider the next alternative:

Alternative Two: Use tables

The values in the if statements and the result for SSS can be put into a table. Then, loops can be used to search the table, and the value looked up:

      final double [][] GROSS_SSS_TABLE = 
      { {     0.0,180.0},{ 4_250.0,202.5},{ 4_750.0,225.0},{ 5_250.0,247.5}
       ,{ 5_750.0,270.0},{ 6_250.0,292.5},{ 6_750.0,315.0},{ 7_250.0,337.5}
       ,{ 7_750.0,360.0},{ 8_250.0,382.5},{ 8_750.0,405.0},{ 9_250.0,427.5}
       ,{ 9_750.0,450.0},{10_250.0,472.5},{10_750.0,495.0},{11_250.0,517.5}
       ,{11_750.0,540.0},{12_250.0,562.5},{12_750.0,585.0},{13_250.0,607.5}
       ,{13_750.0,630.0},{14_250.0,652.5},{14_750.0,675.0},{15_250.0,697.5}
       ,{15_750.0,720.0},{16_250.0,742.5},{16_750.0,765.0},{17_250.0,787.5}
       ,{17_750.0,810.0},{18_250.0,832.5},{18_750.0,855.0},{19_250.0,877.5}
       ,{19_750.0,900.0},{20_250.0,900.0}
      };
      final int GROSS_VAL = 0;
      final int SSS_VAL = 1;
      ... 
      SSS = GROSS_SSS_TABLE[0][SSS_VAL];

      for (int i = GROSS_SSS_TABLE.length - 1; i > 0; --i) {
        if (Gross >= GROSS_SSS_TABLE[i][GROSS_VAL]) {
           SSS = GROSS_SSS_TABLE[i][SSS_VAL];
           break;
        }
      }
      SSS /= 2.0; 

(Here, I chose to not have the /2.0 part in the table.)

For the part of the code that calculates compensation and percentage, you could use either the formula alternative or table alternative. But, it looks like it might be harder to find formulae for that part, so I suspect the table alternative would be preferred. Note such a table for this part would use values in triplets instead of in pairs.

Note:

As previous comments and answer said, there is some lack of precision with floating point values. In selecting a data type to represent money and associated calculations, floating point is unsuitable. In Java, consider using the BigDecimal API for these.

You won't be able to use regular arithmetic and relational operators on BigDecimal Objects. You will have to use methods such as .add, .multiply, .compareTo and so on. But, it's worth it.

You will need more typing to create the table using BigDecimal. It might look something like this:

  final BigDecimal [][] GROSS_SSS_TABLE = 
      { {new BigDecimal(    "0.0",new BigDecimal ("180.0"}
       ,{new BigDecimal( "4250.0",new BigDecimal ("202.5"}
       ,{new BigDecimal( "4750.0",new BigDecimal ("225.0"}

and so on.

Imagine someone else wrote the code, but you have the job of maintaining the code when the taxes & deductions amounts are changed. Which would you rather deal with? A long chain of if statements, or a table? What if the number of rows needed changes? Would you rather add / delete if statements, or rows in a table?

Old Dog Programmer
  • 901
  • 1
  • 7
  • 9