Java Assignments Green = Easy, Blue = Normal, Red = Hard 01 02 03 04 05 06 07 08 09 By title |

You can solve this assignment easily if you have learned from the first one.

The method getNetSalary calculates the net salary of employees.

The method applies only if the tax rate is equal to 0.20 and the monthly income is $4000.

Change the code of the method so that it can calculate the

net salary for every desirable combinations of tax rates and monthly incomes.

### Java assignment 2: Calculate net salary based on tax rate and income

Level: Normal

public class Employee { public double getNetS() { double taxRate = 0.20; double tax = 4000 * taxRate; double netSalary = 4000 - tax; return netSalary; } public static void main(String[] args) { Employee emp = new Employee(); System.out.println(emp.getNetS()); } }

###### Author: Sar Maroof

### Answer explanation

To answer this assignment you need to pass the tax rate and the monthly income as a double parameters to the method getNetSalary. By doing so, you can calculate the net salary for every tax rates and monthly incomes.

Choose some combinations of tax rates and monthly incomes and pass them to the method as follows!

The following program allows users to insert the tax rate and the monthly income, then it calculates the net salary.

import java.util.Scanner; public class Employee { public double getNetS(double taxRate, double income) { double tax = income * taxRate; double netSalary = income - tax; return netSalary; } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a tax rate: "); double taxRate = input.nextDouble(); System.out.print("Enter a gross salary: "); double income = input.nextDouble(); Employee emp = new Employee(); System.out.println(emp.getNetS(taxRate, income)); input.close(); } }

### Extra assignment

Add your own code to the program to inform the user to insert a number if the user enters symbols or letters.

If you found this difficult, you might consider reading my book Java quizmaster for beginners. It is easy to understand, organized to learn Java in 17 days and it guides you to master Java code by solving 105 quizzes and 117 assignments. This book explains each chapter properly before starting with exercises and assignments. It is available on Amazon. See inside the book here!

Please, leave your questions, feedback and suggestions in the comments below!

SarMaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.

## Suggested Articles

## Books by Sar Maroof |

Subscribe to Sar Maroof's RSS feeds! Mail this page to a friend! Bookmark this page! |