Ankit Oops
Ankit Oops
1
ANKIT KUMAR 240097140030
27 Write a program in Java for exception handling by using try,
catch and finally
28 Write a program in Java for throw and throws Exception
2
ANKIT KUMAR 240097140030
Program No. 1
Output:
3
ANKIT KUMAR 240097140030
Program No. 2
Objective: Write a program in Java to understand the difference between print () and
println ().
Source Code:
public class abc
{
public static void main(String[] args)
{
// using print method
System.out.print("Galgotias");
// using print method
System.out.print(" College");
// Moving to the next line
System.out.println();
// using println method
System.out.println("Galgotias");
// using println method
System.out.println(" College");
}
}
Output:
4
ANKIT KUMAR 240097140030
Program No. 3
Objective: Write a program in Java with two classes create a object of first class and
call into another class (having main method).
Source Code:
class Myclass {
// Method in the first class
public void displayMessage(String message)
{
System.out.println(message);
}
}
// Second class with the main method
public class MainClass {
}
}
Output:
5
ANKIT KUMAR 240097140030
Program No. 4
Output:
6
ANKIT KUMAR 240097140030
Program No. 5
{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of the first number ::");
int a = sc.nextInt();
System.out.println("Enter the value of the first number ::");
int b = sc.nextInt();
int result = a*b;
System.out.println("Product of the given two numbers is ::"+result);
}
}
Output:
7
ANKIT KUMAR 240097140030
Program No. 6
Objective: Write a program in java to illustrate the concept of local, instance and static
variable.
Source Code:
public class VariableExample
{
// Static variable (class variable)
static int staticVar = 150;
// Instance variable
int instanceVar = 100;
// Method to demonstrate local variable
public void myMethod() {
// Local variable
int localVar = 20;
System.out.println("Local Variable: " + localVar);
System.out.println("Instance Variable (inside method): " + instanceVar);
System.out.println("Static Variable (inside method): " + staticVar);
}
public static void main(String[] args) {
VariableExample obj1 = new VariableExample();
VariableExample obj2 = new VariableExample();
System.out.println("Object 1");
obj1.myMethod();
System.out.println("Instance Variable (obj1): " + obj1.instanceVar);
System.out.println("Static Variable: " + VariableExample.staticVar);
System.out.println("\n Object 2");
obj2.myMethod();
System.out.println("Instance Variable (obj2): " + obj2.instanceVar);
8
ANKIT KUMAR 240097140030
System.out.println("Static Variable: " + VariableExample.staticVar);
System.out.println("\n Modifying Variables");
obj1.instanceVar = 75;
VariableExample.staticVar = 200;
System.out.println("Instance Variable (obj1) after modification: " + obj1.instanceVar);
System.out.println("Instance Variable (obj2) after modification: " + obj2.instanceVar);
System.out.println("Static Variable after modification: " + VariableExample.staticVar);
}
}
Output:
9
ANKIT KUMAR 240097140030
Program No. 7
Objective: Write a program in java to implement implicit and explicit type casting.
Source Code:
public class TypeCastingExample {
public static void main(String[] args)
{
int intValue = 100;
long longValue = intValue; // int is automatically converted to long
float floatValue = longValue; // long is automatically converted to float
double doubleValue = floatValue; // float is automatically converted to double
System.out.println("Implicit Type Casting:");
System.out.println("int value: " + intValue);
System.out.println("long value: " + longValue);
System.out.println("float value: " + floatValue);
System.out.println("double value: " + doubleValue);
10
ANKIT KUMAR 240097140030
Program No. 8
Output:
12
ANKIT KUMAR 240097140030
Program No. 9
14
ANKIT KUMAR 240097140030
Program No. 10
15
ANKIT KUMAR 240097140030
Output:
16
ANKIT KUMAR 240097140030
Program No. 11
17
ANKIT KUMAR 240097140030
Output:
18
ANKIT KUMAR 240097140030
Program No. 12
Objective: Write a program in java to show run time polymorphism (up casting).
Source Code:
class Shape
{
void draw()
{
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
void draw()
{
System.out.println("Drawing a circle");
}
}
class Rectangle extends Shape
{
@Override
void draw()
{
System.out.println("Drawing a rectangle");
}
}
public class RuntimePolymorphismDemo
{
public static void main(String[] args)
{
19
ANKIT KUMAR 240097140030
Shape s;
s = new Circle();
s.draw();
s = new Rectangle();
s.draw();
}
}
Output:
20
ANKIT KUMAR 240097140030
Program No. 13
}
}
public class TestAccessSpecifiers {
public static void main(String[] args) {
AccessSpecifierExample obj = new AccessSpecifierExample();
System.out.println("Default Variable: " + obj.defaultVar);
System.out.println("Protected Variable: " + obj.protectedVar);
System.out.println("Public Variable: " + obj.publicVar);
obj.display();
}
}
21
ANKIT KUMAR 240097140030
Output:
22
ANKIT KUMAR 240097140030
Program No. 14
{
System.out.println(numbers[i]);
}
}
}
Output:
23
ANKIT KUMAR 240097140030
Program No. 15
Objective: Write a program in java to copy the elements from one array to another
array.
Source Code:
public class CopyArrayExample
{
public static void main(String[] args)
{
int[] originalArray = {1, 2, 3, 4, 5};
int[] copiedArray = new int[originalArray.length];
// Copy elements one by one
for (int i = 0; i < originalArray.length; i++)
{
copiedArray[i] = originalArray[i];
}
System.out.print("Original array: ");
for (int i = 0; i < originalArray.length; i++)
{
System.out.print(originalArray[i] + " ");
}
System.out.print("\nCopied array: ");
for (int i = 0; i < copiedArray.length; i++)
{
System.out.print(copiedArray[i] + " ");
}
}
}
24
ANKIT KUMAR 240097140030
Output:
25
ANKIT KUMAR 240097140030
Program No. 16
Objective: Wap in java to perform the addition and multiplication in 2-D array.
SOURCE CODE:
public class MatrixOperations
// Addition of matrices
System.out.println("Addition of matrices:");
printMatrix(sum);
product[i][j] = 0;
26
ANKIT KUMAR 240097140030
{
printMatrix(product);
System.out.print(val + "\t");
System.out.println();
27
ANKIT KUMAR 240097140030
OUTPUT:
Addition of matrices:
8 10 12
14 16 18
28
ANKIT KUMAR 240097140030
Program 17.
Objective: Wap in java for simple inheritance.
SOURCE CODE:
// Parent class
class Animal
{
void eat()
{
System.out.println("This animal eats food.");
}
}
// Child class
class Dog extends Animal
{
void bark()
{
System.out.println("The dog barks.");
}
}
// Main class
public class SimpleInheritance
{
public static void main(String[] args)
{
Dog myDog = new Dog();
// Calling method of parent class using child object
myDog.eat();
// Calling method of child class
29
ANKIT KUMAR 240097140030
myDog.bark();
}
}
OUTPUT:
This animal eats food.
The dog barks.
30
ANKIT KUMAR 240097140030
Program 18.
Objective: Wap in java for final keyword.
SOURCE CODE:
{
System.out.println("Final variable value is: " + value);
31
ANKIT KUMAR 240097140030
// Uncommenting below will cause compile-time error
// value = 200;
}
}
// Main class
public class FinalKeywordExample
{
public static void main(String[] args)
{
SubFinalDemo obj = new SubFinalDemo();
obj.displayMessage();
obj.showValue();
}
}
OUTPUT:
This is a final method.
Final variable value is: 100
32
ANKIT KUMAR 240097140030
Program 19.
Objective: Wap in java for super keyword.
SOURCE CODE:
class Animal
{
void sound()
{
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal
{
void sound()
{
super.sound(); // Calls the sound() method of Animal class
System.out.println("Dog barks");
}
}
public class TestSuper
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.sound();
}
}
33
ANKIT KUMAR 240097140030
OUTPUT:
34
ANKIT KUMAR 240097140030
Program 20.
Objective: Wap in java chaining constructor.
SOURCE CODE:
class Employee
{
String name;
int id;
double salary;
// Constructor with no arguments
Employee()
{
this("Unknown", 0, 0.0); // Calls the parameterized constructor
System.out.println("No-argument constructor called.");
}
// Constructor with one argument
Employee(String name)
{
this(name, 0, 0.0); // Calls the parameterized constructor
System.out.println("Constructor with one argument called.");
}
// Constructor with two arguments
Employee(String name, int id)
{
this(name, id, 0.0); // Calls the parameterized constructor
System.out.println("Constructor with two arguments called.");
}
// Constructor with three arguments
Employee(String name, int id, double salary)
35
ANKIT KUMAR 240097140030
{
this.name = name;
this.id = id;
this.salary = salary;
}
}
public class TestConstructorChaining
{
public static void main(String[] args)
{
System.out.println("Creating employee1:");
Employee employee1 = new Employee();
employee1.display();
System.out.println("\nCreating employee2:");
Employee employee2 = new Employee("Alice");
employee2.display();
System.out.println("\nCreating employee3:");
Employee employee3 = new Employee("Bob", 101);
employee3.display();
System.out.println("\nCreating employee4:");
36
ANKIT KUMAR 240097140030
employee4.display();
}
}
OUTPUT:
Creating employee1:
Constructor with three arguments called.
No-argument constructor called.
37
ANKIT KUMAR 240097140030
Program 21.
Objective: Wap in java for abstract method and abstract class.
SOURCE CODE:
// Abstract class
abstract class Animal
{
// Abstract method (no body)
public abstract void sound();
// Regular method
public void sleep()
{
System.out.println("This animal is sleeping.");
}
}
// Dog class that extends Animal
class Dog extends Animal
{
// Providing implementation for abstract method
public void sound()
{
System.out.println("The dog says: Bark");
}
}
// Cat class that extends Animal
class Cat extends Animal
{
// Providing implementation for abstract method
public void sound()
38
ANKIT KUMAR 240097140030
{
System.out.println("The cat says: Meow");
}
}
// Main class to test the abstract class and its implementation
public class AbstractClassExample
{
}
}
OUTPUT:
The dog says: Bark
The cat says: Meow
This animal is sleeping.
This animal is sleeping.
39
ANKIT KUMAR 240097140030
Program 22.
Objective: Wap in java for interface.
SOURCECODE:
40
ANKIT KUMAR 240097140030
{
public static void main(String[] args)
{
Animal myDog = new Dog(); // Create a Dog object
Animal myCat = new Cat(); // Create a Cat object
myDog.sound(); // Output: The dog barks
myCat.sound(); // Output: The cat meows
}
}
OUTPUT:
The dog barks
The cat meows
41
ANKIT KUMAR 240097140030
Program 23.
Objective: WAP IN JAVA MULTIPLE INHERITANCE.
SOURCE CODE:
// Interface for animal behavior
interface Animal
{
void eat();
}
// Interface for vehicle behavior
interface Vehicle
{
void drive();
}
// Class that implements both Animal and Vehicle interfaces
class AmphibiousVehicle implements Animal, Vehicle
{
public void eat()
{
System.out.println("This amphibious vehicle can eat.");
}
public void drive()
{
System.out.println("This amphibious vehicle can drive.");
}
}
public class TestMultipleInheritance
{
public static void main(String[] args)
42
ANKIT KUMAR 240097140030
{
AmphibiousVehicle av = new AmphibiousVehicle();
av.eat(); // Output: This amphibious vehicle can eat.
av.drive(); // Output: This amphibious vehicle can drive.
}
}
OUTPUT:
This amphibious vehicle can eat.
This amphibious vehicle can drive.
43
ANKIT KUMAR 240097140030
Program 24.
Objective: Wap in java for object cloning(SHALLOW AND DEEP COPY)
SOURCE CODE:
{
this.city = city;
}
}
// Person class representing a person with a name, age, and address
class Person implements Cloneable
{
String name;
int age;
Address address;
Person(String name, int age, Address address)
{
this.name = name;
this.age = age;
this.address = address;
}
// Overriding clone() method to perform shallow copy
@Override
44
ANKIT KUMAR 240097140030
return super.clone();
}
// Deep copy constructor
public Person(Person other)
{
this.name = other.name;
this.age = other.age;
this.address = new Address(other.address.city);
}
@Override
public String toString()
{
return "Person{name='" + name + "', age=" + age + ", address=" + address.city + "}";
}
}
public class ObjectCloningExample
{
45
ANKIT KUMAR 240097140030
// Modifying the address in the shallow copy
shallowCopy.address.city = "Los Angeles";
// Displaying the original and copied objects
System.out.println("Original Person: " + originalPerson);
System.out.println("Shallow Copy: " + shallowCopy);
System.out.println("Deep Copy: " + deepCopy);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}
}
OUTPUT:
Original Person: Person{name='Alice', age=30, address=Los Angeles}
Shallow Copy: Person{name='Alice', age=30, address=Los Angeles}
Deep Copy: Person{name='Alice', age=30, address=New York}
46
ANKIT KUMAR 240097140030
Program 25.
Objective: Wap in java for all inner classes(ALL TYPES)
SOURCE CODE:
// Outer class
class Outer
{
private String outerField = "Outer Field";
// 1. Non-static Inner Class
class Inner
{
void display()
{
System.out.println("Non-static Inner Class: " + outerField);
}
}
// 2. Static Nested Class
static class StaticNested
{
void display()
{
System.out.println("Static Nested Class");
}
}
void createLocalInnerClass()
{
// 3. Local Inner Class (defined inside a method)
class LocalInner
{
47
ANKIT KUMAR 240097140030
void display()
{
System.out.println("Local Inner Class: " + outerField);
}
}
LocalInner localInner = new LocalInner();
localInner.display();
}
void createAnonymousInnerClass()
{
// 4. Anonymous Inner Class
Runnable anonymousInner = new Runnable()
{
public void run()
{
System.out.println("Anonymous Inner Class");
}
};
anonymousInner.run();
}
}
public class InnerClassDemo
{
public static void main(String[] args)
{
// Creating instance of non-static inner class
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
48
ANKIT KUMAR 240097140030
inner.display();
// Creating instance of static nested class
Outer.StaticNested staticNested = new Outer.StaticNested();
staticNested.display();
// Demonstrating local inner class
outer.createLocalInnerClass();
// Demonstrating anonymous inner class
outer.createAnonymousInnerClass();
}
}
OUTPUT:
Non-static Inner Class: Outer Field
Static Nested Class
Local Inner Class: Outer Field
Anonymous Inner Class
49
ANKIT KUMAR 240097140030
Program 26.
Objective: Wap in java to create package(USER DEFINED PACKAGE)
SOURCE CODE:
Step 1: Create a Package
First, define a package by adding the package keyword at the top of your Java source file.
File: mypackage/HelloWorld.java
package mypackage;
public class HelloWorld
{
public void displayMessage()
{
System.out.println("Hello from the user-defined package!");
}
}
Step 2: Compile the Package
To compile the HelloWorld.java file and place the resulting class file in the appropriate directory
structure:
javac -d . mypackage/HelloWorld.java
Step 3: Create a Class to Use the Package
Now, create another Java class that imports and uses the HelloWorld class from the mypackage package.
File: TestPackage.java
import mypackage.HelloWorld;
public class TestPackage
{
public static void main(String[] args)
{
HelloWorld obj = new HelloWorld();
obj.displayMessage();
50
ANKIT KUMAR 240097140030
}
}
Step 4: Compile and Run the Program
To compile and run the TestPackage class:
javac TestPackage.java
java TestPackage
OUTPUT:
Hello from the user-defined package!
51
ANKIT KUMAR 240097140030
Program 27.
Objective: Wap in java for exception handling by using TRY,CATCH AND FINALLY.
SOURCE CODE:
public class ExceptionHandlingExample
{
public static void main(String[] args)
{
Try
{
// Code that may throw an exception
int result = 10 / 0; // This will throw ArithmeticException
System.out.println("Result: " + result);
}
catch (ArithmeticException e)
{
// Handling the exception
System.out.println("Exception caught: " + e);
}
Finally
{
// Code that will execute regardless of exception
System.out.println("Finally block executed.");
}
}
}
OUTPUT:
Exception caught: java.lang.ArithmeticException: / by zero
Finally block executed.
52
ANKIT KUMAR 240097140030
Program 28.
Objective: Wap in java for THROW AND THROWS EXCEPTION
SOURCE CODE:
// Custom exception class
class InvalidAgeException extends Exception
{
public InvalidAgeException(String message)
{
super(message);
}
}
public class AgeValidator
{
// Method that may throw an exception
public void validateAge(int age) throws InvalidAgeException
{
if (age < 18)
{
throw new InvalidAgeException("Age must be at least 18.");
}
System.out.println("Age is valid: " + age);
}
{
validator.validateAge(16); // This will throw an exception
53
ANKIT KUMAR 240097140030
}
catch (InvalidAgeException e)
{
System.out.println("Caught exception: " + e.getMessage());
}
System.out.println("Program continues...");
}
}
OUTPUT:
Caught exception: Age must be at least 18.
Program continues...
54
ANKIT KUMAR 240097140030
Program 29.
Objective: Wap in java for throw your own exceptions..
SOURCE CODE:
// Custom exception class
class InvalidAgeException extends Exception
{
public InvalidAgeException(String message)
{
super(message);
}
}
public class AgeValidator
{
// Method that may throw an exception
public void validateAge(int age) throws InvalidAgeException
{
if (age < 18)
{
throw new InvalidAgeException("Age must be at least 18.");
}
System.out.println("Age is valid: " + age);
}
public static void main(String[] args)
{
AgeValidator validator = new AgeValidator();
Try
{
validator.validateAge(16); // This will throw an exception
55
ANKIT KUMAR 240097140030
}
catch (InvalidAgeException e)
{
System.out.println("Caught exception: " + e.getMessage());
}
System.out.println("Program continues...");
}
}
OUTPUT:
Caught exception: Age must be at least 18.
Program continues...
56
ANKIT KUMAR 240097140030
Program 30.
Objective: Wap in java to reading and writing in FILES USING BYTE STREAM.
SOURCE CODE:
import java.io.*;
public class ByteStreamExample
{
public static void main(String[] args)
{
// Data to write to the file
String data = "Hello, this is a test message.";
// Writing data to a file using FileOutputStream
try (FileOutputStream fos = new FileOutputStream("output.txt"))
{
byte[] byteData = data.getBytes(); // Convert string to byte array
fos.write(byteData); // Write byte array to file
System.out.println("Data written to file successfully.");
}
catch (IOException e)
{
System.out.println("An error occurred while writing to the file.");
e.printStackTrace();
}
// Reading data from the file using FileInputStream
try (FileInputStream fis = new FileInputStream("output.txt"))
{
int content;
System.out.println("Reading from file:");
while ((content = fis.read()) != -1)
57
ANKIT KUMAR 240097140030
{
System.out.print((char) content); // Convert byte to char and print
}
System.out.println("\nData read from file successfully.");
}
catch (IOException e)
{
System.out.println("An error occurred while reading from the file.");
e.printStackTrace();
}
}
}
OUTPUT:
Data written to file successfully.
Reading from file:
Hello, this is a test message.
Data read from file successfully.
58
ANKIT KUMAR 240097140030
Program 31.
Objective: Wap in java to reading and writing in FILE USING CHARACTER STREAM.
SOURCE CODE:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
59
ANKIT KUMAR 240097140030
while ((character = reader.read()) != -1)
{
System.out.print((char) character);
}
System.out.println("\nData read from file successfully.");
}
catch (IOException e)
{
System.out.println("An error occurred while reading from the file.");
e.printStackTrace();
}
}
}
OUTPUT:
Data written to file successfully.
Reading from file:
Hello, this is a test message using character streams.
Data read from file successfully.
60
ANKIT KUMAR 240097140030
Program 32.
Objective: Wap in java for reading and writing THROUGH CONSOLE CLASS.
SOURCE CODE:
import java.io.Console;
public class ConsoleExample
{
public static void main(String[] args)
{
// Obtain the system console
Console console = System.console();
61
ANKIT KUMAR 240097140030
Program 33.
Objective: Wap in java how to create thread using thread class.
SOURCE CODE:
{
System.out.println("This code is running in a thread.");
}
}
public class ThreadExample
{
public static void main(String[] args)
{
MyThread thread = new MyThread();
thread.start(); // Start the thread
62
ANKIT KUMAR 240097140030
Program 34.
Objective: Wap in java HOW TO CREATE THREAD USING RUNNABLE INTERFACE.
SOURCE CODE:
// Implementing the Runnable interface
class MyRunnable implements Runnable
{
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is executing the run() method.");
}
}
63
ANKIT KUMAR 240097140030
PROGRAM NO: 35
Objective: Wap in java to implement multithreading.
SOURCE CODE:
try
System.out.println(
+ " is running");
catch (Exception e)
// Throwing an exception
System.out.println("Exception is caught");
// Main Class
object.start();
OUTPUT:
Thread 13 is running
Thread 14 is running
Thread 12 is running
Thread 11 is running
Thread 16 is running
Thread 15 is running
Thread 18 is running
Thread 17 is running
65
ANKIT KUMAR 240097140030
PROGRAM NO: 36
Objective: WAP in java to achieve synchronization in threads.
Source code:
class TicketBooking
availableTickets -= tickets;
else
return availableTickets;
try
catch (InterruptedException e)
e.printStackTrace();
});
try
67
ANKIT KUMAR 240097140030
}
catch (InterruptedException e)
e.printStackTrace();
});
t1.start();
t2.start();
Try
t1.join();
t2.join();
catch (InterruptedException e)
e.printStackTrace();
68
ANKIT KUMAR 240097140030
OUTPUT:
69
ANKIT KUMAR 240097140030
PROGRAM NO: 37
Objective: WAP in java to implement the concept of priorities in threads.
Source code:
import java.lang.*;
System.out.println(Thread.currentThread().getName()
+ Thread.currentThread().getPriority());
70
ANKIT KUMAR 240097140030
// Setting priorities of above threads by
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
// t3.setPriority(21);
t1.start();
t2.start();
t3.start();
// Thread - 0, 1 , 2 signify 1 , 2 , 3
// respectively
71
ANKIT KUMAR 240097140030
OUTPUT:
t1 thread priority: 5
t2 thread priority: 5
t3 thread priority: 5
t1 thread priority: 2
t2 thread priority: 5
t3 thread priority: 8
Thread-1 is running with priority 5
Thread-2 is running with priority 8
Thread-0 is running with priority 2
72
ANKIT KUMAR 240097140030
PROGRAM NO: 38
Objective: WAP in java to illustrate the concept of generic programming.
SOURCE CODE:
class Test<T>
T obj;
class Geeks
System.out.println(iObj.getObject());
System.out.println(sObj.getObject());
OUTPUT:
15
Hello World
73
ANKIT KUMAR 240097140030
PROGRAM NO: 39
Objective: WAP in java to illustrate the concept of event handling(using
various event handlers)
SOURCE CODE:
import java.awt.*;
import java.awt.event.*;
TextField textField;
GFGTop()
// Component Creation
button.addActionListener(this);
// add Components
add(textField);
add(button);
// set visibility
setVisible(true);
}
74
ANKIT KUMAR 240097140030
// implementing method of actionListener
textField.setText("GFG!");
new GFGTop();
OUTPUT:
75
ANKIT KUMAR 240097140030
PROGRAM NO: 40
OBJECTIVE:Create a simple student application using various swing components(like
Jframe,Jbutton,Jlabel,textfields,text areas,checkbox,radio box).
SOURCE CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame
extends JFrame
implements ActionListener
private Container c;
76
ANKIT KUMAR 240097140030
private JComboBox year;
"31" };
"2019" };
77
ANKIT KUMAR 240097140030
// constructor, to initialize the components
public MyFrame()
setTitle("Registration Form");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
c = getContentPane();
c.setLayout(null);
title.setSize(300, 30);
title.setLocation(300, 30);
c.add(title);
name.setSize(100, 20);
name.setLocation(100, 100);
c.add(name);
tname.setSize(190, 20);
tname.setLocation(200, 100);
c.add(tname);
mno.setSize(100, 20);
78
ANKIT KUMAR 240097140030
mno.setLocation(100, 150);
c.add(mno);
tmno.setSize(150, 20);
tmno.setLocation(200, 150);
c.add(tmno);
gender.setSize(100, 20);
gender.setLocation(100, 200);
c.add(gender);
male.setSelected(true);
male.setSize(75, 20);
male.setLocation(200, 200);
c.add(male);
female.setSelected(false);
female.setSize(80, 20);
female.setLocation(275, 200);
c.add(female);
gengp.add(male);
gengp.add(female);
dob.setLocation(100, 250);
c.add(dob);
date.setSize(50, 20);
date.setLocation(200, 250);
c.add(date);
month.setSize(60, 20);
month.setLocation(250, 250);
c.add(month);
year.setSize(60, 20);
year.setLocation(320, 250);
c.add(year);
add.setSize(100, 20);
add.setLocation(100, 300);
c.add(add);
tadd.setSize(200, 75);
tadd.setLocation(200, 300);
tadd.setLineWrap(true);
c.add(tadd);
80
ANKIT KUMAR 240097140030
term = new JCheckBox("Accept Terms And Conditions.");
term.setSize(250, 20);
term.setLocation(150, 400);
c.add(term);
sub.setSize(100, 20);
sub.setLocation(150, 450);
sub.addActionListener(this);
c.add(sub);
reset.setSize(100, 20);
reset.setLocation(270, 450);
reset.addActionListener(this);
c.add(reset);
tout.setSize(300, 400);
tout.setLocation(500, 100);
tout.setLineWrap(true);
tout.setEditable(false);
c.add(tout);
res.setSize(500, 25);
res.setLocation(100, 500);
81
ANKIT KUMAR 240097140030
c.add(res);
resadd.setSize(200, 75);
resadd.setLocation(580, 175);
resadd.setLineWrap(true);
c.add(resadd);
setVisible(true);
// method actionPerformed()
if (e.getSource() == sub) {
if (term.isSelected()) {
String data1;
String data
= "Name : "
+ tname.getText() + "\n"
+ "Mobile : "
+ tmno.getText() + "\n";
if (male.isSelected())
+ "\n";
else
+ "\n";
String data2
82
ANKIT KUMAR 240097140030
= "DOB : "
+ (String)date.getSelectedItem()
+ "/" + (String)month.getSelectedItem()
+ "/" + (String)year.getSelectedItem()
+ "\n";
tout.setEditable(false);
res.setText("Registration Successfully..");
else {
tout.setText("");
resadd.setText("");
tname.setText(def);
tadd.setText(def);
tmno.setText(def);
res.setText(def);
tout.setText(def);
term.setSelected(false);
date.setSelectedIndex(0);
month.setSelectedIndex(0);
year.setSelectedIndex(0);
resadd.setText(def);
83
ANKIT KUMAR 240097140030
}
// Driver Code
class Registration {
OUTPUT:
84
ANKIT KUMAR 240097140030