0% found this document useful (0 votes)
7 views28 pages

Java (4th Semester)

Uploaded by

groot987654
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)
7 views28 pages

Java (4th Semester)

Uploaded by

groot987654
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

Java Programming

Lab
INDEX

S.No. Content Page No.

1. Write a program declaring a class


Rectangle with data members length and
breadth and member functions Input,
Output and CalcArea.
2. Write a program to demonstrate use of
method overloading to calculate area of
square, rectangle and triangle.
3. Write a program to demonstrate the use
of static variable, static method and static
block.
4. Write a program to demonstrate concept
of ``this``.
5. Write a program to demonstrate multi-
level and hierarchical inheritance
6. Write a program to use super() to invoke
base class constructor.
7. Write a program to demonstrate run-time
polymorphism
8. Write a program to demonstrate the
concept of aggregation.
9. Write a program to demonstrate the
concept of abstract class with constructor
and ``final`` method.
10. Write a program to demonstrate the
concept of interface when two interfaces
have unique methods and same data
members
11. Write a program to demonstrate checked
exception during file handling.
12. Write a program to demonstrate
unchecked exception.
13. Write a program to demonstrate creation
of multiple child threads.
14. Write a program to use Byte stream class
to read from a text file and display the
content on the output screen.
15. Write a program to demonstrate any
event handling.
PRACTICAL 1: Write a program declaring a class
Rectangle with data members length and
breadth and member functions Input, Output
and CalcArea.

CODE:
import java.util.Scanner;
class Rectangle {
double length;
double breadth;
void Input() {
Scanner scanner = new
Scanner(System.in);
System.out.print("Enter the
length of the rectangle: ");
length = scanner.nextDouble();
System.out.print("Enter the
breadth of the rectangle: ");
breadth = scanner.nextDouble(); }
void Output() {
System.out.println("Length: "
+ length);
System.out.println("Breadth: "
+ breadth); }
void CalcArea() {
double area = length * breadth;
System.out.println("Area: "
+ area); }
public static void main(String[] args) {
Rectangle rectangle = new
Rectangle();
rectangle.Input();
rectangle.Output();
rectangle.CalcArea(); }}

OUTPUT:
PRACTICAL 2: Write a program to demonstrate
use of method overloading to calculate area of
square, rectangle and triangle.

CODE:
public class AreaCalculator {
public static double calculateArea(double side) {
return side * side; }
public static double calculateArea(double length, double width) {
return length * width; }
public static double calculateArea(double base, double height) {
return (base * height) / 2; }
public static void main(String[] args) {
double side = 5.0;
double length = 6.0;
double width = 4.0;
double base = 7.0;
double height = 3.0;
double squareArea = calculateArea(side);
System.out.println("Area of square: " + squareArea);
double rectangleArea = calculateArea(length, width);
System.out.println("Area of rectangle: " + rectangleArea);
double triangleArea = calculateArea(base, height);
System.out.println("Area of triangle: " + triangleArea); }
}

OUTPUT:
PRACTICAL 3: Write a program to demonstrate
the use of static variable, static method and
static block.

CODE:
public class StaticExample {
static int count = 0;
static {
System.out.println("Inside static block");
count = 10; }
static void displayCount() {
System.out.println("Count: " + count); }
public static void main(String[] args) {
System.out.println("Count before increment: " + count);
displayCount();
count++;
displayCount(); }
}

OUTPUT:
PRACTICAL 4: Write a program to demonstrate
concept of ``this``.

CODE:
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age; }
public void displayDetails() {
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age); }
public static void main(String[] args) {
Student student = new Student("John", 20);
student.displayDetails(); }
}

OUTPUT:
PRACTICAL 5: Write a program to demonstrate
multi-level and hierarchical inheritance.

CODE:
class Animal {
void eat() {
System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() {
System.out.println("Barking..."); }
}
class Cat extends Animal {
void meow() {
System.out.println("Meowing..."); }
}
class Labrador extends Dog {
void display() {
System.out.println("Labrador dog"); }
}
public class InheritanceExample {
public static void main(String[] args) {
Labrador labrador = new Labrador();
labrador.display();
labrador.bark();
labrador.eat();
System.out.println();
Dog dog = new Dog();
dog.bark();
dog.eat();
System.out.println();
Cat cat = new Cat();
cat.meow();
cat.eat(); }
}

OUTPUT:
PRACTICAL 6: Write a program to use super() to
invoke base class constructor.

CODE:
class Vehicle {
String brand;
Vehicle(String brand) {
this.brand = brand;
System.out.println("Vehicle
constructor invoked");
}
void display() {
System.out.println("Brand: "
+ brand);
}
}
class Car extends Vehicle {
int maxSpeed;
Car(String brand, int maxSpeed) {
super(brand);
this.maxSpeed = maxSpeed;
System.out.println("Car constructor
invoked");
}
void display() {
super.display();
System.out.println("Max Speed: "
+ maxSpeed);
}
}
public class SuperExample {
public static void main(String[] args) {
Car car = new Car("Toyota", 200);
car.display(); }
}

OUTPUT:
PRACTICAL 7: Write a program to demonstrate
run-time polymorphism.

CODE:
class Animal {
public void makeSound() {
System.out.println("Animal is
making a sound");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Dog is
barking");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Cat is
meowing");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Dog();
Animal animal3 = new Cat();
animal1.makeSound();
animal2.makeSound();
animal3.makeSound();
}
}

OUTPUT:
PRACTICAL 8: Write a program to demonstrate
the concept of aggregation.

CODE:
class Address {
private String street;
private String city;
private String state;
public Address(String street,
String city, String state) {
this.street = street;
this.city = city;
this.state = state;
}
public String getFullAddress() {
return street + ", " + city + ", "
+ state;
}
}
class Employee {
private String name;
private Address address;
public Employee(String name,
Address address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
public class AggregationExample {
public static void main(String[] args) {
Address address = new Address
("123 Main St", "Cityville", "Stateville");
Employee employee = new Employee
("John Doe", address);
System.out.println("Employee Name: "
+ employee.getName());
System.out.println("Employee Address: "
+ employee.getAddress().getFullAddress());
}
}

OUTPUT:
PRACTICAL 9: Write a program to demonstrate
the concept of abstract class with constructor
and ``final`` method.

CODE:
abstract class Shape {
private String name;
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public final void displayInfo() {
System.out.println("Shape: " +
getName());
}
public abstract double calculateArea();
}
class Circle extends Shape {
private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
public double calculateArea() {
return Math.PI * radius * radius;
}
}
public class AbstractExample {
public static void main(String[] args) {
Circle circle = new Circle("Circle", 5.0);
circle.displayInfo();
System.out.println("Area: "
+ circle.calculateArea());
}
}

OUTPUT:
PRACTICAL 10: Write a program to demonstrate
the concept of interface when two interfaces
have unique methods and same data members.

CODE:
interface Animal {
int age = 10;
void eat();
}
interface Mammal {
void sleep();
}
class Dog implements Animal,
Mammal {
public void eat() {
System.out.println("Dog is eating");
}
public void sleep() {
System.out.println("Dog is sleeping");
}
public void displayAge() {
System.out.println("Age: " + age);
}
}
public class InterfaceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.sleep();
dog.displayAge();
}
}

OUTPUT:
PRACTICAL 11: Write a program to demonstrate
checked exception during file handling.

CODE:
import java.io.*;
class GFG {
public static void main(String args[]) {
FileInputStream GFG
= new FileInputStream("/Desktop/GFG.txt");
}
}

OUTPUT:
PRACTICAL 12: Write a program to demonstrate
unchecked exception.

CODE:
public class UncheckedExceptionExample
{
public static void main(String[] args) {
int[] numbers = { 1, 2, 3, 4, 5 };
int index = 6;
try {
int value = numbers[index];
System.out.println("Value at index " + index +
": " + value);
} catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Error: Array index out of
bounds.");
}
System.out.println("Program continues...");
}
}

OUTPUT:
PRACTICAL 13: Write a program to demonstrate
creation of multiple child threads.

CODE:
class ChildThread extends Thread {
private int threadNumber;
public ChildThread(int threadNumber) {
this.threadNumber = threadNumber;
}
public void run() {
System.out.println("Child Thread " +
threadNumber + " is running.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println("Child Thread " +
threadNumber + " was interrupted.");
}
System.out.println("Child Thread " +
threadNumber + " finished.");
}
}
public class MultipleThreadsExample {
public static void main(String[] args) {
ChildThread thread1 = new ChildThread(1);
ChildThread thread2 = new ChildThread(2);
ChildThread thread3 = new ChildThread(3);
thread1.start();
thread2.start();
thread3.start();
System.out.println("Main Thread finished.");
}
}

OUTPUT:
PRACTICAL 14: Write a program to use Byte
stream class to read from a text file and display
the content on the output screen.

CODE:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class ReadBytes {
public static void main(String[] args) {
try {
Scanner KB = new Scanner(System.in);
System.out.print("Enter Text File Name:");
String filename = KB.next();
FileInputStream FI = new FileInputStream(filename);
byte b[] = new byte[FI.available()];
FI.read(b);
String c = new String(b);
System.out.println(c);
FI.close();
} catch (IOException e)
{
System.out.println(e);
}
}
}
OUTPUT:
PRACTICAL 15: Write a program to demonstrate
any event handling.

CODE:
import java.awt.*;
import java.awt.event.*;
class GFG extends Frame implements ActionListener {
TextField textField;
GFGTop()
{
textField = new TextField();
textField.setBounds(60, 50, 180, 25);
Button button = new Button("click Here");
button.setBounds(100, 120, 80, 30);
button.addActionListener(this);
add(textField);
add(button);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
textField.setText("GFG!");
}
public static void main(String[] args)
{
new GFGTop();
}
}
OUTPUT:

You might also like