0% found this document useful (0 votes)
21 views8 pages

Java Lab Exercise 1-4

The document contains Java programs demonstrating various concepts including Fibonacci series generation using both recursive and non-recursive methods, method and constructor overloading, employee detail display using the Scanner class, and palindrome checking. Each program is accompanied by an aim, code implementation, and a result statement confirming successful execution. The examples illustrate fundamental programming techniques in Java.

Uploaded by

ddharshini984
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)
21 views8 pages

Java Lab Exercise 1-4

The document contains Java programs demonstrating various concepts including Fibonacci series generation using both recursive and non-recursive methods, method and constructor overloading, employee detail display using the Scanner class, and palindrome checking. Each program is accompanied by an aim, code implementation, and a result statement confirming successful execution. The examples illustrate fundamental programming techniques in Java.

Uploaded by

ddharshini984
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

Ex No.

1 Fibonacci series using recursive and non-recursive functions


Aim:
To write a java program to find the Fibonacci series using recursive and non-recursive
functions

Program:
// Recursive function to find the nth Fibonacci number
public class Fibonacci {
public static int fibonacciRecursive(int n) {
if (n <= 1) {
return n;
}
return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}
public static void main(String[] args) {
int n = 10; // Change 'n' to get different Fibonacci numbers
[Link]("Fibonacci series using recursive function:");
for (int i = 0; i < n; i++) {
[Link](fibonacciRecursive(i) + " ");
}
}
}
// Non-recursive function to find the nth Fibonacci number
public class Fibonacci {
public static int fibonacciNonRecursive(int n) {
if (n <= 1) {
return n;
}
int fibPrev = 0;
int fibCurr = 1;
for (int i = 2; i <= n; i++) {
int temp = fibCurr;
fibCurr = fibPrev + fibCurr;
fibPrev = temp;
}
return fibCurr;
}
public static void main(String[] args) {
int n = 10; // Change 'n' to get different Fibonacci numbers
[Link]("Fibonacci series using non-recursive function:");
for (int i = 0; i < n; i++) {
[Link](fibonacciNonRecursive(i) + " ");
}
}
}
Output:

Result:
Thus the program to find the Fibonacci series using recursive and non-recursive functions are
executed successfully.
[Link].2. Method overloading and Constructor overloading
Aim:
To write a java program for Method overloading and Constructor overloading.
Program:
class Calculator {
// Method Overloading
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
// Constructor Overloading
public Calculator() {
[Link]("Default Constructor");
}
public Calculator(int num) {
[Link]("Constructor with one integer parameter: " + num);
}
public Calculator(double num1, double num2) {
[Link]("Constructor with two double parameters: " + num1 + " and " +
num2);
}
}
public class Overloading {
public static void main(String[] args) {
Calculator calculator = new Calculator();
// Method overloading examples
int result1 = [Link](5, 10);
[Link]("Result 1: " + result1);
double result2 = [Link](2.5, 3.5);
[Link]("Result 2: " + result2);
// Constructor overloading examples
Calculator calculator1 = new Calculator();
Calculator calculator2 = new Calculator(5);
Calculator calculator3 = new Calculator(2.5, 3.5);
}
}

Output:

Result:
Thus the program for Method overloading and Constructor overloading is executed
successfully
[Link].3. Display the employee details using Scanner class
Aim:
To write a java program to display the employee details using Scanner class.
Program:
import [Link];
class Employee {
private String name;
private int age;
private String designation;
private double salary;
public Employee(String name, int age, String designation, double salary) {
[Link] = name;
[Link] = age;
[Link] = designation;
[Link] = salary;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getDesignation() {
return designation;
}
public double getSalary() {
return salary;
}
}
public class EmployeeDetails {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter employee name: ");
String name = [Link]();
[Link]("Enter employee age: ");
int age = [Link]();
[Link](); // Consume the newline character left by nextInt()
[Link]("Enter employee designation: ");
String designation = [Link]();
[Link]("Enter employee salary: ");
double salary = [Link]();
// Creating the Employee object with user input
Employee employee = new Employee(name, age, designation, salary);
// Displaying employee details
[Link]("\nEmployee Details:");
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
[Link]("Designation: " + [Link]());
[Link]("Salary: " + [Link]());
[Link]();
}
}

Output:

Result:
Thus a program to display the employee details using Scanner class is implemented and
executed successfully
Ex. No.4. Palindrome
Aim:
To write a java program that checks whether a given string is palindrome or not.
Program:
public class PalindromeChecker {
public static void main(String[] args) {
String input = "madam"; // Replace this with your input
string if (isPalindrome(input)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
public static boolean isPalindrome(String str) {
// Remove spaces and convert to lowercase for case-insensitive
comparison str = [Link]("\\s+", "").toLowerCase();
int left = 0;
int right =
[Link]() - 1;
while (left < right)
{
if ([Link](left) !=
[Link](right)) { return false;
}
left++; right--;
}
return true;
}
}
Output:

Result:
Thus a java program that checks whether a given string is palindrome or not is executed
successfully

You might also like