Program t1
Program t1
// Default constructor
public Student() {
this.name = "John Doe";
this.age = 20;
this.department = "Computer Science";
}
System.out.println("Student 3 details:");
student3.displayDetails();
}
}
In this program:
Default constructor: Sets default values for name, age, and department.
Constructor with name and age parameters: Allows setting name and age
while keeping the department as default.
Constructor with all parameters: Allows setting name, age, and
department.
The displayDetails() method prints the details of a student.
import java.util.*;
return sortedMap;
}
}
When you run this program, it will output:
class Bank {
public double getInterestRate() {
return 0.0;
}
}
Bank is the base class with a method getInterestRate() which returns 0.0
by default.
SBI, ICICI, and AXIS are subclasses of Bank which override the
getInterestRate() method to return their specific interest rates.
In the main() method, we create objects of type Bank but initialize them
with instances of SBI, ICICI, and AXIS. When we call getInterestRate()
method on these objects, it invokes the overridden method specific to
each bank, demonstrating runtime polymorphism.
When you run this program, it will output:
return reversedString.toString().trim();
}
}
In this program:
import java.util.HashSet;
Unique elements:
10
12
50
20
80
70
This demonstrates how to remove duplicate elements from an array and
print the unique elements in Java.
/ Employee class
class Employee {
public double getSalary() {
return 50000.0; // Default salary for Employee
}
}
// Main class
public class Main {
public static void main(String[] args) {
Employee employee = new Employee();
HREmployee hrEmployee = new HREmployee();
7. Write a java program use array,find the min and Max value
public class MinMaxArray {
public static void main(String[] args) {
int[] array = {10, 5, 8, 20, 3, 15};
In this program:
We define two methods findMin and findMax to find the minimum and maximum
values in the array respectively.
The findMin method initializes the min variable with the first element of
the array and then iterates through the array to find the minimum value.
The findMax method initializes the max variable with the first element of
the array and then iterates through the array to find the maximum value.
In the main method, we create an array and call the findMin and findMax
methods to find the minimum and maximum values respectively.
Finally, we print the minimum and maximum values.
When you run this program with the provided array, it will output:
Minimum value: 3
Maximum value: 20
This demonstrates how to find the minimum and maximum values in an array
using Java.
8. In a given array arrange all the even numbers first and remaining
next?
A[] = {24,57, 5, 3, 28, 2, 7, 9, 10}
o\p = 2, 28, 2, 10, 57, 5, 3, 7, 9
evenIndex++;
}
}
}
}
In this program:
Arranged Array:
24 28 2 10 57 5 3 7 9
This demonstrates how to arrange even numbers first in an array in Java.
9.In a given sentence remove the extra white spaces and print the result
"Telstra is my destiny"?
10. Write a program to print the string in reverse order without using
any string functions.
public class ReverseStringWithoutStringFunctions {
public static void main(String[] args) {
String str = "Hello, World!";
String reversed = reverseString(str);
System.out.println("Original string: " + str);
System.out.println("Reversed string: " + reversed);
}