LPU Java Unit 2 Lecture 13 RB | LPUCOLAB

LPU Java Unit 2 Lecture 13 RB | LPUCOLAB

LPU_Java_Unit 2_Lecture 13_RB

1. Problem Statement

Create a program for Raj's Employee Information System that takes input for an employee's name, ID, and monthly salary. Utilize the Employee class with a constructor and the this keyword for attribute assignments.

Implement methods for calculating and displaying the annual salary. Collect Raj's input and display the employee details.

Input format: - The first line of input consists of a string, which represents the employee's name (a string containing characters and spaces). - The second line consists of an integer, representing the employee's ID. - The third line consists of a double-point number, which represents the monthly salary.

Output format: - The first line displays "Employee Name: " followed by the employee's name. - The second line displays "Employee ID: " followed by the employee's ID. - The third line displays "Monthly Salary: Rs. " followed by the monthly salary as a double, rounded to two decimal places. - The last line displays "Annual Salary: Rs. " followed by the calculated annual salary as a double, rounded to two decimal places.

Solution:

class Employee {
   String name;
   int id;
   double salary;

   public Employee(String name, int id, double salary) {
      this.name = name;
      this.id = id;
      this.salary = salary;
   }

   public double calculateAnnualSalary() {
      return this.salary * 12;
   }

   public void displayEmployeeDetails() {
      System.out.println("Employee Name: " + this.name);
      System.out.println("Employee ID: " + this.id);
      System.out.printf("Monthly Salary: Rs. %.2f\n", this.salary);
      System.out.printf("Annual Salary: Rs. %.2f\n", calculateAnnualSalary());
   }
}

2. Problem Statement

Bob is curious about palindrome numbers and wants a program to check if a given integer is a palindrome. The program should take an integer input, use a class with a parameterized constructor and an initializer block to initialize values, and implement a method to check and display whether the given number is a palindrome or not.

Input format: - The input consists of a single integer representing the input number.

Output format: - The output prints whether the given integer is a palindrome or not.

Solution:

class PalindromeChecker {
    private int number;

    public PalindromeChecker(int num) {
        this.number = num;
    }

    public boolean isPalindrome() {
        int originalNumber = number;
        int tempNumber = number;
        int reversedNumber = 0;

        while (tempNumber > 0) {
            int digit = tempNumber % 10;
            reversedNumber = reversedNumber * 10 + digit;
            tempNumber /= 10;
        }

        return originalNumber == reversedNumber;
    }

    public void displayPalindromeCheckResult() {
        if (isPalindrome()) {
            System.out.println(number + " is a Palindrome");
        } else {
            System.out.println(number + " is not a Palindrome");
        }
    }
}