Java
Java
PROGRAM1
Aim: Write a program declaring a class Rectangle with data members length and breadth
and member functions Input, Output, and Calc Area.
CODE:
import java.util.Scanner;
length = scanner.nextDouble();
breadth = scanner.nextDouble();
System.out.println("Rectangle Details:");
rectangle.input();
rectangle.output();
}
}
OUTPUT:
PROGRAM2
Aim: Write a program to demonstrate the use of method overloading to calculate the area
of a square, rectangle, and triangle.
CODE:
public class AreaCalculator {
if (isTriangle) {
}}
OUTPUT:
PROGRAM3
Aim: Write a program to demonstrate the use of static variables, static methods, and static
blocks.
CODE:
public class StaticDemo {
staticVariable = 10;
StaticDemo.staticMethod();
OUTPUT:
PROGRAM4
Aim: Write a program to demonstrate the concept of “this”.
CODE:
public class ThisDemo {
this.value = value;
this.value = value;
demo.displayValue();
demo.setValue(20);
demo.displayValue();
}}
OUTPUT:
PROGRAM5
Aim: Write a program to demonstrate multi-level and hierarchical inheritance.
CODE:
class Animal {
void eat() {
System.out.println("This animal eats food.");
void walk() {
void bark() {
void meow() {
dog.eat();
dog.walk();
dog.bark();
cat.eat();
cat.walk();
cat.meow();
OUTPUT:
PROGRAM6
Aim: Write a program to use “super()” to invoke the base class constructor.
CODE:
class Parent {
Parent() {
Child() {
super();
OUTPUT:
PROGRAM7
Aim: Write a program to demonstrate run-time polymorphism.
CODE:
class Animal {
void sound() {
void sound() {
System.out.println("Dog barks.");
void sound() {
System.out.println("Cat meows.");
Animal myAnimal;
myAnimal.sound();
myAnimal = new Cat();
myAnimal.sound();
OUTPUT:
PROGRAM8
Aim: Write a program to demonstrate the concept of aggregation.
CODE:
class Address {
this.city = city;
this.state = state;
this.country = country;
class Employee {
int id;
String name;
Address address;
this.id = id;
this.name = name;
this.address = address;
void display() {
employee.display();
OUTPUT:
PROGRAM9
Aim: Write a program to demonstrate the concept of an abstract class with a
constructor and “final” method.
CODE:
abstract class Animal {
Animal() {
System.out.println("Animal is created.");
System.out.println("Animal is sleeping.");
void sound() {
System.out.println("Dog barks.");
animal.sound();
animal.sleep(); }}
OUTPUT:
PROGRAM10
Aim: Write a program to demonstrate the concept of interface when two
interfaces have unique methods and the same data members.
CODE:
interface A {
void methodA();
interface B {
void methodB();
void displayData() {
obj.methodA();
obj.methodB();
obj.displayData();
OUTPUT:
PROGRAM11
Aim: Write a program to demonstrate a checked exception during file handling.
CODE:
import java.io.*;
try {
String line;
System.out.println(line);
reader.close();
} catch (IOException e) {
OUTPUT:
PROGRAM12
Aim: Write a program to demonstrate an unchecked exception.
CODE:
public class UncheckedExceptionDemo {
try {
int a = 10;
int b = 0;
int result = a / b;
} catch (ArithmeticException e) {
OUTPUT:
PROGRAM13
Aim: Write a program to demonstrate the creation of multiple child threads.
CODE:
class ChildThread extends Thread {
ChildThread(String name) {
threadName = name;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
public class MultipleThreadsDemo {
thread1.start();
thread2.start();
OUTPUT:
PROGRAM14
Aim: Write a program to demonstrate the creation of multiple child threads.
CODE:
import java.io.*;
int content;
System.out.print((char) content);
} catch (IOException e) {
OUTPUT:
PROGRAM15
Aim: 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.awt.*;
import java.awt.event.*;
TextField textField;
EventHandlingDemo() {
button.addActionListener(this);
add(button);
add(textField);
setSize(300, 200);
setLayout(null);
setVisible(true);
textField.setText("Hello, World!");
new EventHandlingDemo(); }}
PROGRAM16
Aim: Create a class Employee with attributes name, age, and address. Implement
methods getData() and showData(). The getData() method should take input from
the user, and showData() should display the data in the format:
Name:
Age:
Address:
CODE:
import java.util.Scanner;
this.name = scanner.nextLine();
this.age = scanner.nextInt();
this.address = scanner.nextLine();
employee.getData();
System.out.println("\nEmployee Details:");
employee.showData();
}
}
OUTPUT:
PROGRAM17
Aim: Write a Java program to perform basic calculator operations. Implement a
menu-driven program to select an operation (+, -, *, /).
CODE:
import java.util.Scanner;
// Display menu
System.out.println("Select operation:");
switch (choice) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
if (num2 != 0) {
} else {
break;
default:
System.out.println("Invalid choice!");
scanner.close();
OUTPUT:
PROGRAM18
Aim: Write a program to make use of BufferedStream to read lines from the
keyboard until 'STOP' is typed.
CODE:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
try {
String line;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
OUTPUT:
PROGRAM19
Aim: Declare a Java class Savings Account with members “account Number” and
“Balance”. Provide member functions as “deposit Amount()” and “withdraw Amount
()”. If the user tries to withdraw an amount greater than their balance, throw a
user-defined exception.
CODE:
public class SavingsAccount {
this.accountNumber = accountNumber;
this.balance = balance;
balance += amount;
balance -= amount;
return accountNumber;
return balance;
}
class InsufficientBalanceException extends Exception {
super(message);
PROGRAM20
Aim: Write a program creating 2 threads using Runnable interface. Print your
name in the “run ()” method of the first class and "Hello Java" in the “run ()”
method of the second thread.
CODE:
public class NamePrinter implements Runnable {
@Override
System.out.println("Your Name");
@Override
System.out.println("Hello Java");
}
public class Main {
t1.start();
t2.start();
OUTPUT:
PROGRAM21
Aim: Write a program using Swing to display a combination of RGB using 3
scrollbars.
CODE:
import javax.swing.*;
import java.awt.*;
public class RGBDisplay extends JFrame {
public RGBDisplay() {
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
colorPanel.setBackground(Color.BLACK);
add(redBar, BorderLayout.NORTH);
add(greenBar, BorderLayout.CENTER);
add(blueBar, BorderLayout.SOUTH);
add(colorPanel, BorderLayout.CENTER);
setVisible(true);
new RGBDisplay();
PROGRAM22
Aim: Write a Swing application that uses at least 5 Swing controls.
CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public SwingControlsExample() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JLabel
add(label);
// JTextField
add(textField);
// JButton
add(button);
// JCheckBox
add(checkBox);
// JRadioButton
group.add(radioButton1);
group.add(radioButton2);
add(radioButton1);
add(radioButton2);
button.addActionListener(new ActionListener() {
@Override
});
setVisible(true);
}
public static void main(String[] args) {
OUTPUT:
PROGRAM23
Aim: Write a program to implement border layout using Swing.
CODE:
import javax.swing.*;
import java.awt.*;
public BorderLayoutExample() {
setTitle("BorderLayout Example");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create buttons
add(northButton, BorderLayout.NORTH);
add(southButton, BorderLayout.SOUTH);
add(eastButton, BorderLayout.EAST);
add(westButton, BorderLayout.WEST);
add(centerButton, BorderLayout.CENTER);
setVisible(true);
new BorderLayoutExample();