Category: Java assignments

Java assignment 01: Calculate net salary based on tax rate

Choose an Assignment:    Green = Easy,   Blue = Normal,   Red = Hard
A01  |   A02  |   A03  |   A04  |  

Guys, here 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 is correct 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. Please, don’t check out the answer before trying it!

Java assignment 1: Calculate net salary based on tax rate

Level: Normal

public class CalculateNetSalary
{
  public double getNetSalary()
  {
    double taxRate = 0.30;
    double tax = 3200 * taxRate;
    double netSalary = 3200 - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    CalculateNetSalary cns = new CalculateNetSalary();
    System.out.println(cns.getNetSalary());
  }
}
Author: Sar Maroof

Answer explanation

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. In our example we pass the following tax rates to the method, 0.20, 0.25 & 0.40.

Try to pass other tax rates of your choice and execute the program to see the result!

  1. for taxRate 0.20 the net salary = 2560
  2. for taxRate 0.25 the net salary = 2400
  3. for taxRate 0.40 the net salry = 1920

 

 
public class CalculateNetSalary
{
  public double getNetSalary(double taxRate)
  {
    double tax = 3200 * taxRate;
    double netSalary = 3200 - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    CalculateNetSalary cns = new CalculateNetSalary();
    System.out.println(cns.getNetSalary(0.20));
    System.out.println(cns.getNetSalary(0.25));
    System.out.println(cns.getNetSalary(0.40));
  }
}

This program writes the following to the standard output if it is compiled and run.
2560.0
2400.0
1920.0


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

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.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Sarmaroof © 2017