Java assignment 01: Calculate net salary

  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 

Here below is a Java assignment for beginner programmers. When you start to work by a company, you often need to write Java methods and classes!

The method getNetSalary calculates the net salary of employees who have a monthly income of $3200. This method can be applied only if the tax rate is equal to 0.30.
Change the code of the method so that it can calculate the net salary for every desirable tax rates.

Java assignment 1: Calculate net salary based on tax rate

Level: Normal


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

To answer this assignment you need to pass the tax rate as a double type parameter to the method getNetSalary.
By doing so, you can calculate the net salary for every tax rates. The following program allows the user to enter a tax rate. The program calculates the net salary.

When this program is compiled and run, it asks the user to insert a tax rate and press the enter button. The program calculates the tax rate. Here below the user entered a tax rate of 0.40. Try to test the program by inserting more tax rates.

Enter a tax rate: 0.40
1920.0

 


import java.util.Scanner;

public class Employee
{
  public double getNetS(double taxRate)
  {
    double tax = 3200 * taxRate;
    double netSalary = 3200 - 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();
    Employee emp = new Employee();
    System.out.println(emp.getNetS(taxRate));
    input.close();
  }
}

Author: Sar Maroof

The second edition of Learn Java and Master Writing Code is easy to understand, organized to learn Java voor beginners and it guides readers to master Java code by solving more than 100 exercises and assignments. This book explains each chapter properly before starting with the exercises and the assignments. Available on Amazon. See inside the book here!


Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

Leave a Reply

Your email address will not be published. Required fields are marked *