0% found this document useful (0 votes)
6 views3 pages

Java Odd Value and Salary Checker

The document contains two Java programs: the first checks if a user-entered integer is odd or even, throwing a custom exception for odd values, while the second defines an Employee class and a derived Salary class to calculate and display employee salary details. Both programs utilize the Scanner class for user input and demonstrate exception handling and class inheritance in Java. The main methods in each program orchestrate the flow of execution, prompting user input and displaying results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Odd Value and Salary Checker

The document contains two Java programs: the first checks if a user-entered integer is odd or even, throwing a custom exception for odd values, while the second defines an Employee class and a derived Salary class to calculate and display employee salary details. Both programs utilize the Scanner class for user input and demonstrate exception handling and class inheritance in Java. The main methods in each program orchestrate the flow of execution, prompting user input and displaying results.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SOURCE CODE:

import [Link];

// User-defined exception
class OddValExp extends Exception {
public OddValExp(String message) {
super(message);
}
}

public class OddValueChecker {


// Method to check for odd value
static void checkValue(int num) throws OddValExp {
if (num % 2 != 0) {
throw new OddValExp("Error: The entered value " + num + " is odd!");
} else {
[Link]("The entered value " + num + " is even.");
}
}

public static void main(String[] args) {


Scanner sc = new Scanner([Link]);
try {
[Link]("Enter an integer: ");
int number = [Link]();

// Check if value is odd


checkValue(number);

} catch (OddValExp e) {
[Link]([Link]());
} catch (Exception e) {
[Link]("Invalid input! Please enter an integer.");
} finally {
[Link]();
}
}
}

Department Of Computer Science


SOURCE CODE:
import [Link];

// Base class: Employee


class Employee {
int id;
String name;
String designation;
String dept;

// Constructor
Employee(int id, String name, String designation, String dept) {
[Link] = id;
[Link] = name;
[Link] = designation;
[Link] = dept;
}

// Method to display employee details


void displayEmployeeInfo() {
[Link]("\nEmployee Details:");
[Link]("ID: " + id);
[Link]("Name: " + name);
[Link]("Designation: " + designation);
[Link]("Department: " + dept);
}
}

// Derived class: Salary


class Salary extends Employee {
double basic;
double hra;
double da;
double allowance;
double netSalary;

// Constructor
Salary(int id, String name, String designation, String dept, double basic) {
super(id, name, designation, dept);
[Link] = basic;
[Link] = 1250; // Fixed HRA
[Link] = 1.10 * basic; // 110% of basic
[Link] = 0.35 * basic; // 35% of basic
}

// Method to calculate net salary


void calculateNetSalary() {
netSalary = basic + hra + da + allowance;
}

Department Of Computer Science


// Method to display salary details
void displaySalaryInfo() {
[Link]("\nSalary Details:");
[Link]("Basic Salary: " + basic);
[Link]("HRA: " + hra);
[Link]("DA (110%): " + da);
[Link]("Allowance (35%): " + allowance);
[Link]("Net Salary: " + netSalary);
}
}

public class EmployeeSalaryDemo {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Reading employee details


[Link]("Enter Employee ID: ");
int id = [Link]();
[Link](); // consume newline

[Link]("Enter Name: ");


String name = [Link]();

[Link]("Enter Designation: ");


String designation = [Link]();

[Link]("Enter Department: ");


String dept = [Link]();

[Link]("Enter Basic Salary: ");


double basic = [Link]();

// Create Salary object


Salary emp = new Salary(id, name, designation, dept, basic);

// Compute and display


[Link]();
[Link]();
[Link]();

[Link]();
}
}

Department Of Computer Science

You might also like