2
\$\begingroup\$

I'm learning the ropes of Java. So far everything is going good. Can you please review my code and tell me what I can do to make it cleaner - or what I need to improve on?. I'll appreciate it so much.

This program:

The user inputs are the gross income and number of dependents. The program calculates the income tax based on the inputs and the tax law and then displays the income tax.

Here's a screen shot of the code in the IDE and a flowchart.

(IDE)

IDE

(Flowchart)

Flowchart

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

        System.out.println("Enter gross income: ");
        double grossIncome;
        grossIncome = input.nextDouble();

        System.out.println("Enter the number of dependents: ");
        double dependents;
        dependents = input.nextDouble();

        final double TAX_RATE = 0.2;
        final double STANDARD_DEDUCTION = 10000.00;
        final double DEPENDENT_DEDUCTION = 2000.0;
        final double grossIncomeafterdeduction = grossIncome - (STANDARD_DEDUCTION + DEPENDENT_DEDUCTION * dependents);

        double incomeTax;
        incomeTax = grossIncomeafterdeduction * TAX_RATE;

        double grossincometax;
        grossincometax = grossIncome - incomeTax;

        System.out.println("The income tax is $ " + incomeTax);

        System.out.println("The gross income after tax $ " + grossIncometax);
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ The title of your code review should summarize what the code does. Requests for improvement are implied in code reviews. \$\endgroup\$ – Greg Burghardt Feb 9 '17 at 18:40
  • 1
    \$\begingroup\$ Please explain what you're code is doing in a small paragraph. The format of the code is quite buggy in your question. Make sure it reflects the format you have in your IDE / Editor. \$\endgroup\$ – Marc-Andre Feb 9 '17 at 18:48
  • \$\begingroup\$ Ah, sorry about that. I updated the post. I tried pasting the code correctly but the ending brackets don't fit inside the code box for some reason. Here's how it looks in the IDE i.imgur.com/Cjl1xGT.png \$\endgroup\$ – user130591 Feb 9 '17 at 19:12
  • 1
    \$\begingroup\$ Welcome to Code Review! Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$ – Simon Forsberg Feb 9 '17 at 19:25
  • \$\begingroup\$ Ahh, you learn something new everyday. My mistake for not reading that part. Thank you. \$\endgroup\$ – user130591 Feb 9 '17 at 19:43
3
\$\begingroup\$
  1. Only import what you need from java util not the whole package.
  2. Move constants to member level and make static, or pass in these parameters to your program.
  3. Use a money package for calculations involving money.
\$\endgroup\$
  • \$\begingroup\$ I believe, I fixed #1, working on the next 2. \$\endgroup\$ – user130591 Feb 9 '17 at 19:12
  • \$\begingroup\$ So basically remove final right? Hopefully I did this right. imgur.com/a/YT4lr \$\endgroup\$ – user130591 Feb 9 '17 at 19:41
2
\$\begingroup\$

This code is pretty short and clear, so I can only suggest minor stylistic improvements.

Join declaration and assignment for local variables

Change variable declarations like this:

double grossIncome;
grossIncome = input.nextDouble();

to:

double grossIncome = input.nextDouble();

everywhere you can to get rid of the extra line.

Define constants on a class level

Move declarations of constants:

final double TAX_RATE = 0.2;

outside of the method:

public class lab4 {
    private static final double TAX_RATE = 0.2;

    public static void main(String[] args) {
        //...

Class level constants are more visible and you can access them from other methods if you need to.

Name variables consistently

Use camel-case for all local variables. E.g. change grossIncomeafterdeduction to grossIncomeAfterDeduction.

Further improvements

Currently, logic is intertwined with I/O operations. Consider moving tax calculation into separate methods or creating a separate class TaxCalculator. After that tax calculation can be tested in isolation. This might not be necessary for this particular assignment, though.

\$\endgroup\$
  • \$\begingroup\$ I still need to learn how to make a separate class, I'll look up a video tutorial on that right now, but I changed the code - tell me what you think, please. imgur.com/a/YT4lr \$\endgroup\$ – user130591 Feb 9 '17 at 19:40
  • \$\begingroup\$ I feel like I can maybe make grossIncomeAfterDeduction cleaner but I'm not sure how to go about it with too many math operations included. \$\endgroup\$ – user130591 Feb 9 '17 at 19:46
  • \$\begingroup\$ @AJM you joined declaration and assignment for grossIncome. You can do the same for dependents, incomeTax and others. Also, you can move _DEDUCTION constants to class body. \$\endgroup\$ – default locale Feb 9 '17 at 19:48
  • \$\begingroup\$ @AJM you can introduce an intermediate variable: double totalDeduction = STANDARD_DEDUCTION + DEPENDENT_DEDUCTION * dependents; (I'm not sure about the naming). \$\endgroup\$ – default locale Feb 9 '17 at 19:51
  • 1
    \$\begingroup\$ "BigDecimal is the first one I used but I'm using eclipse and it doesn't seem to recognize it," show the line of code that eclipse refuses to compile. \$\endgroup\$ – Timothy Truckle Feb 10 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy