Java Lab File
Java Lab File
OUTPUT
EXPERIMENT – 04
OUTPUT
Example 1: Polymorphism
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}
OUTPUT
EXPERIMENT – 07
import java.io.*;
public class FileWriteReadExample {
public static void main(String[] args) {
String fileName = "example.txt";
try {
FileWriter writer = new FileWriter(fileName);
writer.write("Hello, this is a Java I/O example.\n");
writer.write("We are writing to a file using FileWriter.");
writer.close();
System.out.println("File written successfully.");
}
catch (IOException e) {
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
}
try {
FileReader reader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(reader);
bufferedReader.close();
reader.close();
}
catch (IOException e) {
System.out.println("An error occurred while reading the file.");
e.printStackTrace();
}
}
}
OUTPUT
EXPERIMENT – 03
1. Encapsulation:
Binding data and methods into a single unit (class).
Use of private access modifier and getters/setters to protect data.
Example:
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. Inheritance:
One class (child) inherits properties and behavior from another (parent).
Promotes code reusability.
Use of extends keyword.
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
3. Polymorphism:
Ability of an object to take many forms.
Compile-time polymorphism (method overloading).
Run-time polymorphism (method overriding).
Example:
class Shape {
void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a circle");
}
}
4. Abstraction:
Hiding implementation details and showing only the functionality.
Achieved using abstract classes or interfaces.
Example:
1. Java Syntax:
Java is case-sensitive.
Every application starts with a main method.
2. Data Types:
Primitive: int, char, float, boolean, etc.
Non-primitive: String, arrays, classes, interfaces.
3. Control Structures:
if, else, switch, for, while, do-while.
5. Access Modifiers:
public, private, protected, and default (no modifier).
6. Keywords:
Examples: class, static, void, new, return, this, super, etc.
EXPERIMENT – 06
package shapes;
public class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
}
import shapes.Rectangle;
public class AreaCalculator {
public static void main(String[] args) {
Rectangle rect = new Rectangle(5.0, 3.0);
double area = rect.getArea();
System.out.println("Area of the rectangle: " + area);
}
}
OUTPUT
EXPERIMENT – 05
Use Java compiler and eclipse platform to write and execute java
program.
OUTPUT