Java assignment 02: Calculate net salary

Java Assignments   Green = Easy,  Blue = Normal,  Red = Hard
 A01   A02   A03   A04   A05   A06   A07   A08   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 Mc
{
  public double getNetS()
  {
    double taxRate = 0.30;
    double tax = 3200 * taxRate;
    double netSalary = 3200 - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    Mc m = new Mc();
    System.out.println(m.getNetS());
  }
}
Author: Sar Maroof

Answer explanation

To answer this assignment you need to pass the tax rate and the monthly income as parameters of the type of double 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!

if this program is compiled and run, it will write the following to the standard output

2240.0
1760.0
2100.0

public class Mc
{
  public double getNetS(double taxRate, double income)
  {
    double tax = income * taxRate;
    double netSalary = income - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    Mc m = new Mc();
    System.out.println(m.getNetS(0.30, 3200));
    System.out.println(m.getNetS(0.20, 2200));
    System.out.println(m.getNetS(0.50, 4200));
  }
}

 

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.




Sar Maroof is graduated from HBO Amsterdam "higher professional education". He is a professional computer programming teacher, gives master classes and publishes technical articles. His experience with developing software started from 2001 and he has worked as a software developer 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!

Leave a Reply

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

Sarmaroof © 2017