Quizzes Assignments Puzzles Easy exercises Required knowledge |

Java Assignments Green = Easy, Blue = Normal, Red = Hard Select 01 02 03 04 05 06 07 08 09 10 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.

## Suggested Articles

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