0% found this document useful (0 votes)
48 views25 pages

JAVA Practicle Khushdeep Kaur

The document provides a comprehensive guide on Java programming, including installation steps, basic programs, and concepts such as operators, object creation, constructors, method overloading, and inheritance. It includes example code for various tasks like printing 'Hello World', calculating areas of different shapes, and checking for prime numbers. Additionally, it demonstrates the use of classes, constructors, and methods in Java.

Uploaded by

armaansohail9356
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)
48 views25 pages

JAVA Practicle Khushdeep Kaur

The document provides a comprehensive guide on Java programming, including installation steps, basic programs, and concepts such as operators, object creation, constructors, method overloading, and inheritance. It includes example code for various tasks like printing 'Hello World', calculating areas of different shapes, and checking for prime numbers. Additionally, it demonstrates the use of classes, constructors, and methods in Java.

Uploaded by

armaansohail9356
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
You are on page 1/ 25

+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |

_______________________________________________________________________________________
BASICS CORE JAVA -
A. Write steps of installation of java

1. Download Java JDK from the official Oracle website or OpenJDK.

2. Install Java by following the on-screen instructions.

3. Set up the environment variable:

- Add JAVA_HOME to system variables.

- Add Java bin directory to the PATH variable.

4. Verify installation using the command: `java -version`

B. Write a program to print hello world.

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

C. Write a program using every operator of Java

public class OperatorsDemo {


public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Arithmetic Operators: ");
System.out.println("Addition: " + (a + b));
System.out.println("Subtraction: " + (a - b));
System.out.println("Multiplication: " + (a * b));
System.out.println("Division: " + (a / b));
System.out.println("Modulus: " + (a % b));

System.out.println("Relational Operators: ");


System.out.println(a > b);
System.out.println(a < b);
System.out.println(a == b);

System.out.println("Logical Operators: ");


System.out.println((a > 5) && (b < 10));
System.out.println((a > 5) || (b > 10));
System.out.println(!(a > 5));
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 1 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

System.out.println("Bitwise Operators: ");


System.out.println(a & b);
System.out.println(a | b);

System.out.println("Assignment Operators: ");


a += 5; // Equivalent to a = a + 5;
System.out.println(a);

System.out.println("Ternary Operator: ");


int min = (a < b) ? a : b;
System.out.println(min);
}
}

D. Write a program to create the object & initialize the object through instance variable

class Student {
int rollNo;
String name;
}

public class StudentTest {


public static void main(String[] args) {
Student s1 = new Student();
s1.rollNo = 1323222;
s1.name = "Ankush";
System.out.println("Roll No: " + s1.rollNo + ", Name: " + s1.name);
}
}

E. Write a program to initialize the object through constructor & Function.

class Car {
String model;
int year;

// Constructor
Car(String model, int year) {
this.model = model;
this.year = year;
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 2 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Function to display details
void display() {
System.out.println("Car Model: " + model + ", Year: " + year);
}
}
public class CarTest {
public static void main(String[ ] args) {
Car c1 = new Car("Tesla", 2022);
c1.display();
}
}

F. Write a program to take 5 students and print their roll no, name & marks.

import java.util.Scanner;

class StudentDetails {
int rollNo;
String name;
float marks;

// Constructor
StudentDetails(int rollNo, String name, float marks) {
this.rollNo = rollNo;
this.name = name;
this.marks = marks;
}

// Display Function
void display() {
System.out.println("Roll No: " + rollNo + ", Name: " + name + ", Marks: " +
marks);
}
}

public class StudentArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
StudentDetails[] students = new StudentDetails[5];

for (int i = 0; i < 5; i++) {


System.out.print("Enter Roll No, Name & Marks for Student " + (i + 1) + ": ");
int rollNo = sc.nextInt();
String name = sc.next();
float marks = sc.nextFloat();
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 3 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
students[i] = new StudentDetails(rollNo, name, marks);
}

System.out.println("Student Details:");
for (StudentDetails student : students) {
student.display();
}
sc.close();
}
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 4 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 1 |> Write a program to accept a number as an input and find


out whether number is prime or not.

import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();

if (isPrime(num)) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

OUTPUT :-
Test Case 1:

Enter a number: 2
2 is a prime number.

Test Case 2:

Enter a number: 10
10 is not a prime number.
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 5 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 2 |> Write a Program to calculate HCF and LCM.

import java.util.Scanner;
public class HCF_LCM_Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
scanner.close();

int hcf = findHCF(num1, num2);


int lcm = (num1 * num2) / hcf; // LCM formula: (a × b) / HCF
System.out.println("HCF of " + num1 + " and " + num2 + " is: " + hcf);
System.out.println("LCM of " + num1 + " and " + num2 + " is: " + lcm);
}
public static int findHCF(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}

OUTPUT :-

Enter first number: 12


Enter second number: 18
HCF of 12 and 18 is: 6
LCM of 12 and 18 is: 36

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 6 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 3 |> Write a program to implement constructors in java.

// Class with Default, Parameterized, and Copy Constructors


class Student {
String name;
int age;

// Default Constructor
public Student() {
System.out.println("Default constructor called.");
name = " khushdeep kaur";
age = 19;
}

// Parameterized Constructor
public Student(String name, int age) {
System.out.println("Parameterized constructor called.");
this.name = name;
this.age = age;
}

// Copy Constructor
public Student(Student other) {
System.out.println("Copy constructor called.");
this.name = other.name;
this.age = other.age;
}

// Method to display student details


public void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class ConstructorExample {
public static void main(String[] args) {
// Using Default Constructor
Student student1 = new Student();
student1.display();

// Using Parameterized Constructor


Student student2 = new Student("Tanuj", 24);
student2.display();

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 7 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Using Copy Constructor
Student student3 = new Student(student2);
student3.display();
}
}

OUTPUT :-

Default constructor called.


Name: khushdeep kaur, Age: 19
Parameterized constructor called.
Name: Tanuj, Age: 24
Copy constructor called.
Name: Tanuj, Age: 24

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 8 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 4 |> Write a program to find out area of rectangle, circle and


triangle using method overloading

class CalculateArea {
// Method to calculate the area of a rectangle
double area(double length, double breadth) {
return length * breadth;
}

// Method to calculate the area of a circle


double area(double radius) {
return Math.PI * radius * radius;
}
// Method to calculate the area of a triangle
double area(double base, double height) {
return 0.5 * base * height;
}
public static void main(String[] args) {
CalculateArea ca = new CalculateArea();

double rectangleArea = ca.area(10, 5);


double circleArea = ca.area(7);
double triangleArea = ca.area(8, 5);

System.out.println(rectangleArea);
System.out.println(circleArea);
System.out.println(triangleArea);
}

OUTPUT :-

50.0
153.93804002589985
20.0

Explanation:
Rectangle Area = 10 * 5 = 50.0
Circle Area = π * 7 * 7 ≈ 153.94
Triangle Area = 0.5 * 8 * 5 = 20.0

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 9 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 5 |> Write a program to elaborate the concept of multilevel


inheritance in java.
// Base class or parent class
class Animal {
void eat() {
System.out.println("This animal can eat.");
}
void sleep() {
System.out.println("This animal can sleep.");
}
}
// Derived class from the Animal class (Intermediate class)
class Mammal extends Animal {
void walk() {
System.out.println("This mammal can walk.");
}
}

// Derived class from the Mammal class (Leaf class in the hierarchy)
class Dog extends Mammal {
void bark() {
System.out.println("This dog can bark.");
}
}

public class MultilevelInheritanceDemo {


public static void main(String[] args) {
// Create an object of the Dog class
Dog dog = new Dog();

// Using methods from all the levels of the inheritance hierarchy


System.out.println("Behavior inherited from Animal class:");
dog.eat(); // Method inherited from the Animal class
dog.sleep(); // Method inherited from the Animal class

System.out.println("\nBehavior inherited from Mammal class:");


dog.walk(); // Method inherited from the Mammal class

System.out.println("\nBehavior specific to Dog class:");


dog.bark(); // Method from the Dog class itself
}
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 10 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

OUTPUT :-
OUTPUT :
Behavior inherited from Animal class:
This animal can eat.
This animal can sleep.

Behavior inherited from Mammal class:


This mammal can walk.

Behavior specific to Dog class:


This dog can bark.

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 11 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 6 |> Write a program to find out area of rectangle, circle and


triangle using method overriding.

// Base class
class Shape {
// Method to calculate the area (to be overridden by subclasses)
void calculateArea() {
System.out.println("This is the base class. Define specific shapes to calculate
the area.");
}
}

// Rectangle class inheriting from Shape


class Rectangle extends Shape {
private double length, width;

Rectangle(double length, double width) {


this.length = length;
this.width = width;
}

@Override
void calculateArea() {
double area = length * width;
System.out.println("Area of Rectangle: " + area);
}
}

// Circle class inheriting from Shape


class Circle extends Shape {
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 12 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
private double radius;

Circle(double radius) {
this.radius = radius;
}

@Override
void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of Circle: " + area);
}
}

// Triangle class inheriting from Shape


class Triangle extends Shape {
private double base, height;

Triangle(double base, double height) {


this.base = base;
this.height = height;
}

@Override
void calculateArea() {
double area = 0.5 * base * height;
System.out.println("Area of Triangle: " + area);
}
}

public class MethodOverridingDemo {


#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 13 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
public static void main(String[] args) {
// Create objects for each shape
Shape rectangle = new Rectangle(10, 5); // Rectangle with length=10 and
width=5
Shape circle = new Circle(7); // Circle with radius=7
Shape triangle = new Triangle(8, 4); // Triangle with base=8 and height=4

// Call the overridden methods


rectangle.calculateArea();
circle.calculateArea();
triangle.calculateArea();
}
}

OUTPUT :-

Area of Rectangle: 50.0


Area of Circle: 153.93804002589985
Area of Triangle: 16.0

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 14 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 7 |> Write a program to implement the concept of interfaces in


java.

// Interface definition
interface Animal {
// Abstract methods (implicitly public and abstract)
void eat();
void sleep();
}

// Interface definition
interface Pet {
void play();
}

// Class implementing multiple interfaces


class Dog implements Animal, Pet {
@Override
public void eat() {
System.out.println("Dog is eating.");
}

@Override
public void sleep() {
System.out.println("Dog is sleeping.");
}

@Override
public void play() {
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 15 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
System.out.println("Dog is playing fetch.");
}
}

public class InterfaceDemo {


public static void main(String[] args) {
// Create an object of Dog class
Dog myDog = new Dog();

// Call methods from the interfaces


myDog.eat(); // From Animal interface
myDog.sleep(); // From Animal interface
myDog.play(); // From Pet interface
}
}

OUTPUT :-

Dog is eating.
Dog is sleeping.
Dog is playing fetch.

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 16 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 8 |> Write a program to create your own packages and utilize
those packages in a class.

Step 1: Create a Custom Package

In Java, a package is essentially a way to group related classes together. Let's create a package called
mypackage that contains a class for basic arithmetic operations.

File: ArithmeticOperations.java (Saved in the mypackage directory)

// Declare the package


package mypackage;

// Class inside the package


public class ArithmeticOperations {
// Method to add two numbers
public int add(int a, int b) {
return a + b;
}
// Method to subtract two numbers
public int subtract(int a, int b) {
return a - b;
}
}

Step 2: Use the Custom Package

Now we'll create another class outside the package to use the mypackage package and its
ArithmeticOperations class.

File: MainApp.java

// Import the custom package


import mypackage.ArithmeticOperations;

public class MainApp {

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 17 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
public static void main(String[] args) {
// Create an object of the ArithmeticOperations class
ArithmeticOperations operations = new ArithmeticOperations();
// Perform arithmetic operations
int sum = operations.add(10, 5);
int difference = operations.subtract(10, 5);
// Display results
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
}
}

Step 3: Compilation and Execution

1. Save the ArithmeticOperations.java file in a directory named mypackage.


2. Open the command prompt or terminal, navigate to the directory containing mypackage, and
compile the class:

javac -d . ArithmeticOperations.java

The -d flag ensures the compiled class is placed in the correct directory structure for the package.

3. Save MainApp.java in the parent directory of mypackage and compile it:

javac MainApp.java

4. Run the MainApp program:

java MainApp

myDog.play(); // From Pet interface


}
}

OUTPUT :-
Sum: 15
Difference: 5

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 18 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

| 9 |> Write a program to implement the concept of interfaces in


java.

import java.util.StringTokenizer;

public class StringTokenizerExample {


public static void main(String[] args) {
String str = "Welcome to the world of Java programming!";

// Create a StringTokenizer object


StringTokenizer st = new StringTokenizer(str, " ");

System.out.println("Total number of tokens: " + st.countTokens());

// Loop through and print each token


while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}

OUTPUT :-
Total number of tokens: 7
Welcome
to
the
world
of
Java
programming!

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 19 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 10 |> Write a program to implement applets in java.

import java.applet.Applet;
import java.awt.Graphics;

/*
<applet code="MyApplet" width="300" height="200">
</applet>
*/

public class MyApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello, Khushdeep! This is a Java Applet!", 50, 100);
}
}

OUTPUT :-

Hello, Khushdeep! This is a Java Applet!

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 20 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 11 |> Write a program to apply the concept of exception
handling in classes.

import java.util.Scanner;

class Divider {
private int numerator;
private int denominator;

// Method to set values


public void setValues(int num, int den) {
this.numerator = num;
this.denominator = den;
}

// Method to perform division with exception handling


public void divide() {
try {
int result = numerator / denominator;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero is not allowed!");
}
}
}

public class ExceptionHandlingDemo {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Divider divider = new Divider();

OUTPUT :-
Enter numerator: 10
Enter denominator: 0
Exception caught: Division by zero is not allowed!

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 21 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 12 |> Write a program to create a frame, button, text box and
label.

import java.awt.*;
import java.awt.event.*;

public class MyFrameDemo {


public static void main(String[] args) {
// Create a frame
Frame frame = new Frame("AWT GUI Example");

// Create a label
Label label = new Label("Enter your name:");
label.setBounds(50, 80, 120, 30);

// Create a text field


TextField textField = new TextField();
textField.setBounds(180, 80, 150, 30);

// Create a button
Button button = new Button("Click Me");
button.setBounds(130, 130, 80, 30);

// Add action listener to the button


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
frame.setTitle("Welcome, " + name + "!");
}
});

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 22 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________

// Add components to the frame


frame.add(label);
frame.add(textField);
frame.add(button);

// Set frame properties


frame.setSize(400, 250);
frame.setLayout(null); // Absolute positioning
frame.setVisible(true);

// Close the frame on window close


frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
frame.dispose();
}
});
}
}

OUTPUT :-

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 23 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 13 |> Write a program to create a button. When the button is
click, an appropriate message should be displayed in the
dialog box.

import javax.swing.*;
import java.awt.event.*;

public class ButtonDialogExample {


public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Button Click Dialog Example");

// Create a button
JButton button = new JButton("Click Me");
button.setBounds(120, 80, 150, 40);

// Add action listener to the button


button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "Hello Anku! You clicked the
button.");
}
});

// Add button to frame


frame.add(button);

// Frame settings
frame.setSize(400, 250);
frame.setLayout(null); // Absolute layout

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 24 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

OUTPUT :-

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 25 )@$ | +-------+

You might also like