0% found this document useful (0 votes)
15 views4 pages

Day5 Lab

This document discusses a Java program to calculate tax for employees. It defines exception classes, implements a TaxCalculator class to calculate tax based on salary and nationality, and provides a CalculatorSimulator class to test different cases. It also includes examples to demonstrate ArrayIndexOutOfBoundsException.

Uploaded by

Subhajit Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Day5 Lab

This document discusses a Java program to calculate tax for employees. It defines exception classes, implements a TaxCalculator class to calculate tax based on salary and nationality, and provides a CalculatorSimulator class to test different cases. It also includes examples to demonstrate ArrayIndexOutOfBoundsException.

Uploaded by

Subhajit Kundu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/*

java application to calculate the tax for a particular employee based on his
salary. Develop a class named “TaxCalculator“ with a method named calculateTax with
the following method parameters, Variable Name Data Type empName String isIndian
boolean empSal double This method should return a double taxAmount. The business
logic for calculating the tax is as follows, this has to be implemented inside the
method, If the employee is not a Indian The calculator should throw a
CountryNotValidException If the employee name is null or empty The calculator
should throw a EmployeeNameInvalidException If empSal is greater than one lakh and
isIndian true taxAmount =empSal *8/100 Otherwise If empSal is between 50K and 1lakh
and isIndian true taxAmount =empSal *6/100 Otherwise If empSal is between 30 and 50
Thousand and isIndian true taxAmount =empSal *5/100 Otherwise If empSal is between
10 and 30 Thousand and isIndian true taxAmount =empSal *4/100 Otherwise The
calculator should throw a TaxNotEligibleException. Develop a main class
CalculatorSimulator , implement the following logic in main method 1. Execute the
calculateTax Method and print the tax amount as “Tax amount is “ 2. In case the
calculateTaxMethod throws exceptions, this method needs to catch the appropriate
exception print the stack trace and display the following messages, a. Country not
valid: “The employee should be an Indian citizen for calculating tax” b. Employee
name not valid: “The employee name cannot be empty” c. Not eligible for Tax
calculation: “The employee does not need to pay tax” The following test cases to be
executed, change the data in main method and run it and verify the output messages
Test Cases Employee Name Employee Salary Is Indian Message Expected Test Case 1 Ron
34000 False The employee should be an Indian citizen for calculating tax. Test Case
2 Tim 1000 True The employee does not need to pay tax Test Case 3 Jack 55000 True
Tax amount is 3300 Test Case 4 30000 True The employee name cannot be empty.
*/
/*Here's the Java code implementing the TaxCalculator and CalculatorSimulator
classes as per the given requirements:

java*/

// Exception class for country not valid


class CountryNotValidException extends Exception {
public CountryNotValidException(String message) {
super(message);
}
}

// Exception class for employee name not valid


class EmployeeNameInvalidException extends Exception {
public EmployeeNameInvalidException(String message) {
super(message);
}
}

// Exception class for not eligible for tax calculation


class TaxNotEligibleException extends Exception {
public TaxNotEligibleException(String message) {
super(message);
}
}

// TaxCalculator class
class TaxCalculator {
// Method to calculate tax
public double calculateTax(String empName, boolean isIndian, double empSal)
throws CountryNotValidException, EmployeeNameInvalidException,
TaxNotEligibleException {
if (!isIndian) {
throw new CountryNotValidException("The employee should be an Indian
citizen for calculating tax");
}
if (empName == null || empName.isEmpty()) {
throw new EmployeeNameInvalidException("The employee name cannot be
empty");
}
double taxAmount;
if (empSal > 100000) {
taxAmount = empSal * 8 / 100;
} else if (empSal >= 50000 && empSal <= 100000) {
taxAmount = empSal * 6 / 100;
} else if (empSal >= 30000 && empSal < 50000) {
taxAmount = empSal * 5 / 100;
} else if (empSal >= 10000 && empSal < 30000) {
taxAmount = empSal * 4 / 100;
} else {
throw new TaxNotEligibleException("The employee does not need to pay
tax");
}
return taxAmount;
}
}

// CalculatorSimulator class
public class CalculatorSimulator {
public static void main(String[] args) {
// Test cases
TaxCalculator calculator = new TaxCalculator();

// Test Case 1
try {
double tax1 = calculator.calculateTax("Ron", false, 34000);
System.out.println("Tax amount is " + tax1);
} catch (CountryNotValidException e) {
System.out.println(e.getMessage());
} catch (EmployeeNameInvalidException e) {
System.out.println(e.getMessage());
} catch (TaxNotEligibleException e) {
System.out.println(e.getMessage());
}

// Test Case 2
try {
double tax2 = calculator.calculateTax("Tim", true, 1000);
System.out.println("Tax amount is " + tax2);
} catch (CountryNotValidException e) {
System.out.println(e.getMessage());
} catch (EmployeeNameInvalidException e) {
System.out.println(e.getMessage());
} catch (TaxNotEligibleException e) {
System.out.println(e.getMessage());
}

// Test Case 3
try {
double tax3 = calculator.calculateTax("Jack", true, 55000);
System.out.println("Tax amount is " + tax3);
} catch (CountryNotValidException e) {
System.out.println(e.getMessage());
} catch (EmployeeNameInvalidException e) {
System.out.println(e.getMessage());
} catch (TaxNotEligibleException e) {
System.out.println(e.getMessage());
}

// Test Case 4
try {
double tax4 = calculator.calculateTax("", true, 30000);
System.out.println("Tax amount is " + tax4);
} catch (CountryNotValidException e) {
System.out.println(e.getMessage());
} catch (EmployeeNameInvalidException e) {
System.out.println(e.getMessage());
} catch (TaxNotEligibleException e) {
System.out.println(e.getMessage());
}
}
}
/* This program defines three custom exception classes for handling specific
scenarios, implements the TaxCalculator class to calculate tax based on salary and
nationality, and provides a CalculatorSimulator class to test different test cases.
*/

/* Create a program with a logic that throws the ArrayIndexOutOfBoundsException


while accessing elements in an array.[Hint: Use array and loop and try to access
the element beyond the last index]

*/

public class ArrayIndexOutOfBoundsExceptionExample {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};

try {
// Accessing elements beyond the last index of the array
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds!");
}
}
}
/*In this program, an array of integers is defined, and then a loop attempts to
access elements beyond the last index of the array. When the loop tries to access
an index that doesn't exist in the array, it throws an
ArrayIndexOutOfBoundsException, which is caught by the try-catch block, and an
appropriate message is displayed.
*/
/*Create a program with a logic that throws the ArrayIndexOutOfBoundsException
while accessing elements in an array.
*/
public class ArrayAccessExample {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
try {
// Trying to access an element at an index that is out of bounds
int element = arr[5];
System.out.println("Element at index 5: " + element);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index is out of bounds!");
}
}
}
/* An array arr of size 3 is declared and initialized with values 1, 2, and 3.
Inside the try block, we attempt to access an element at index 5, which is
beyond the bounds of the array.
Since index 5 does not exist in the array, it throws an
ArrayIndexOutOfBoundsException.
This exception is caught by the catch block, and a message "Array index is out
of bounds!" is printed to the console.
*/
/*Write an program to print the array element from 1 to 100*/

public class PrintArrayElements {


public static void main(String[] args) {
// Create an array to store integers from 1 to 100
int[] array = new int[100];

// Initialize the array with values from 1 to 100


for (int i = 0; i < 100; i++) {
array[i] = i + 1;
}

// Print the array elements


System.out.println("Array elements from 1 to 100:");
for (int i = 0; i < 100; i++) {
System.out.print(array[i] + " ");
}
}
}
/*
Create a program with a logic that throws the ArrayIndexOutOfBoundsException while
accessing elements in an array.[Hint: Use array and loop and try to access the
element beyond the last index]*/
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] array = {1, 2, 3}; // Array with 3 elements

// Access elements beyond the last index of the array


for (int i = 0; i <= array.length; i++) {
try {
System.out.println(array[i]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds exception
occurred!");
}
}
}
}

You might also like