### **Q#1.
Reading and Displaying Data from a File**
**Content of `[Link]` file:**
```
Alice, 21, A
Bob, 22, B
Charlie, 20, A
```
**Java Program:**
```java
import [Link].*;
public class StudentFileReader {
public static void main(String[] args) {
String fileName = "[Link]"; // File name
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
[Link]("Student Data:");
while ((line = [Link]()) != null) {
[Link](line);
}
} catch (IOException e) {
[Link]("Error reading the file: " + [Link]());
}
}
}
```
---
### **Q#2. What is Polymorphism?**
**Definition:**
Polymorphism in Java is the ability of an object to take on multiple forms. It
allows a single interface or method to work in different ways depending on the
object or arguments used.
**Types of Polymorphism:**
1. **Compile-Time Polymorphism (Method Overloading):**
Multiple methods with the same name but different parameter lists in the same
class.
**Example:**
```java
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]([Link](5, 3)); // Outputs 8
[Link]([Link](5.5, 3.3)); // Outputs 8.8
}
}
```
2. **Run-Time Polymorphism (Method Overriding):**
Subclass provides a specific implementation of a method already defined in the
superclass.
**Example:**
```java
class Animal {
public void sound() {
[Link]("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
public void sound() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
[Link](); // Outputs: Dog barks
}
}
```
---
### **Q#3. What is Inheritance?**
**Definition:**
Inheritance is a mechanism in object-oriented programming where a class
(child/subclass) inherits properties and methods from another class
(parent/superclass).
**Types of Inheritance in Java:**
1. **Single Inheritance:**
A child class inherits from one parent class.
**Example:**
```java
class Animal {
public void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
public void bark() {
[Link]("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
}
}
```
2. **Multilevel Inheritance:**
A child class inherits from a parent class, and another class inherits from the
child.
**Example:**
```java
class Animal {
public void eat() {
[Link]("This animal eats food.");
}
}
class Mammal extends Animal {
public void walk() {
[Link]("Mammals can walk.");
}
}
class Dog extends Mammal {
public void bark() {
[Link]("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
[Link]();
}
}
```
3. **Hierarchical Inheritance:**
Multiple child classes inherit from a single parent class.
**Example:**
```java
class Animal {
public void eat() {
[Link]("This animal eats food.");
}
}
class Dog extends Animal {
public void bark() {
[Link]("The dog barks.");
}
}
class Cat extends Animal {
public void meow() {
[Link]("The cat meows.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
[Link]();
[Link]();
Cat cat = new Cat();
[Link]();
[Link]();
}
}
```
---
### **Q#4. What is a Constructor?**
**Definition:**
A constructor is a special method used to initialize an object in Java. It has the
same name as the class and does not have a return type.
**Constructor Overloading:**
Constructor overloading allows a class to have multiple constructors with different
parameter lists.
**Example:**
```java
public class Car {
private String make;
private String model;
private int year;
// Default constructor
public Car() {
[Link] = "Unknown";
[Link] = "Unknown";
[Link] = 0;
}
// Parameterized constructor
public Car(String make, String model, int year) {
[Link] = make;
[Link] = model;
[Link] = year;
}
public void displayDetails() {
[Link]("Make: " + make + ", Model: " + model + ", Year: " +
year);
}
public static void main(String[] args) {
Car defaultCar = new Car();
Car parameterizedCar = new Car("Toyota", "Corolla", 2023);
[Link](); // Outputs default values
[Link](); // Outputs parameterized values
}
}
```
---
### **Q#5. Java Swing Application**
**Example Application:** Simple Login Form
```java
import [Link].*;
import [Link];
import [Link];
public class LoginForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Login Form");
JLabel userLabel = new JLabel("Username:");
[Link](20, 30, 80, 25);
[Link](userLabel);
JTextField userField = new JTextField();
[Link](100, 30, 150, 25);
[Link](userField);
JLabel passLabel = new JLabel("Password:");
[Link](20, 70, 80, 25);
[Link](passLabel);
JPasswordField passField = new JPasswordField();
[Link](100, 70, 150, 25);
[Link](passField);
JButton loginButton = new JButton("Login");
[Link](100, 110, 80, 25);
[Link](loginButton);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = [Link]();
String password = new String([Link]());
if ([Link]("admin") && [Link]("password")) {
[Link](frame, "Login Successful!");
} else {
[Link](frame, "Invalid Credentials!");
}
}
});
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
```
This application creates a simple GUI login form where users can input a username
and password. Let me know if more explanation is needed!