Java assignment 01: Calculate net salary

Java Assignments:  Green = Easy,  Blue = Normal,  Red = Hard
 A01   A02   A03   A04   A05   A06   A07   A08   By title 

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 found this difficult, you might consider reading my book Java quizmaster for beginners. It is easier and 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.




Sar Maroof is graduated from HBO Amsterdam “higher professional education” when he already had a bachelor of science degree in Physics.
He is a SUN certified JSP as well as EJB. He has experience with Java since 2001 and worked for several big as well as small companies and later as a freelancer.
He is the author of Java quizmaster and Build a Java application in 7 days.

Books by Sar Maroof

           

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

Share on FacebookTweet about this on TwitterShare on RedditShare on LinkedIn

Leave a Reply

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

Sarmaroof © 2017