[go: up one dir, main page]

0% found this document useful (0 votes)
54 views93 pages

Oop Lab Full Programs

OOP LAB FULL PROGRAMS is a collection of programs that demonstrate the concepts of object-oriented programming (OOP). These programs are written in a variety of languages, including Java, C++, and Python.
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)
54 views93 pages

Oop Lab Full Programs

OOP LAB FULL PROGRAMS is a collection of programs that demonstrate the concepts of object-oriented programming (OOP). These programs are written in a variety of languages, including Java, C++, and Python.
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/ 93

REG.

NO: 21TDL002 NAME: AKASHARA PAVITHRAN YEAR/SEM/SEC:


Y2/S4/Aa

CLASS AND OBJECTS

SOURCE CODE:

#include <iostream>

#include<conio.h>

using namespace std;

// Student Class Declaration

class StudentClass {

private://Access - Specifier

//Member Variable Declaration

char name[20];

int regNo, sub1, sub2, sub3;

float total, avg;

public://Access - Specifier

//Member Functions read() and print() Declaration

void read() {

//Get Input Values For Object Variables

cout << "Enter Name :";

cin >> name;

cout << "Enter Registration Number :";

cin >> regNo;


cout << "Enter Marks for Subject 1,2 and 3 :";

cin >> sub1 >> sub2>> sub3;

void sum() {

total = sub1 + sub2 + sub3;

avg = total / 3;

void print() {

//Show the Output

cout << "Name :" << name << endl;

cout << "Registration Number :" << regNo << endl;

cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;

cout << "Total :" << total << endl;

cout << "Average :" << avg << endl;

};

int main() {

// Object Creation For Class

StudentClass stu1, stu2;

cout << "Read and Print Student Information Class Example Program In C++\n";

cout << "\nStudentClass : Student 1" << endl;

stu1.read();

stu1.sum();

stu1.print();

cout << "\nStudentClass : Student 2" << endl;

stu2.read();
stu2.sum();

stu2.print();

getch();

return 0;

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

CONSTRUCTOR AND DESTRUCTOR

SOURCE CODE:

#include <iostream>

#include<conio.h>

using namespace std;

class stud

private:

char name[20],add[20];

int roll;

public:

stud();

~stud();

void read();

void display();

};

stud::stud()

cout<<"Constructor\n";

cout<<"This is student details"<<endl;

void stud::read()

{
cout<<"Enter the student name:";

cin>>name;

cout<<"Enter the roll number:";

cin>>roll;

cout<<"Enter the student address:";

cin>>add;

void stud::display()

cout<<"Student Name:"<<name<<endl;

cout<<"Roll number:"<<roll<<endl;

cout<<"Student Address:"<<add<<endl;

stud::~stud()

cout<<"Student details is closed";

int main()

stud stud;

stud.read();

stud.display();

return 0; }
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

FUNCTION OVERLOADING

SOURCE CODE:

#include<iostream>

#include<math.h>

#define pi 3.14

#define e 0.5

#include<conio.h>

using namespace std;

class shape

public:

float area(float a)

return(pi*a*a);

float area(float a,float b)

return(a*b);

float area(float a,float b,float c)

float temp;

temp=e*a*b*c;
return temp;

};

int main()

float r,l,b,side[3];

shape circle,rect,tri;

cout<<"CIRCLE"<<endl;

cout<<"Enter radius of circle:";

cin>>r;

cout<<"Area of the circle:"<<circle.area(r)<<"sq.unit"<<endl;

cout<<"RECTANGLE"<<endl;

cout<<"Enter length and breadth of rectangle:";

cin>>l>>b;

cout<<"Area of the circle:"<<rect.area(l,b)<<"sq.unit"<<endl;

cout<<"TRIANGLE"<<endl;

cout<<"Enter sides of triangle:";

cin>>side[0]>>side[1]>>side[2];

cout<<"Area of the circle:"<<tri.area(side[0],side[1],side[2])<<"sq.unit"<<endl;

return 0;

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

ABSTRACT CLASS

SOURCE CODE:

#include<iostream>

using namespace std;

class shape

protected:

float dimension;

public:

void getdimension()

cin>>dimension;

virtual float calculatearea()=0;

};

class square:public shape

public:

float calculatearea()

return dimension*dimension;

};

int main()
{

square s;

cout<<"ABSTRACT CLASS";

cout<<"\n Enter the length of the square:";

s.getdimension();

cout<<"Area of the square:"<<s.calculatearea()<<endl;

return 0;

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

MULTILEVEL INHERITANCE

SOURCE CODE:

#include<iostream>

using namespace std;

class First

public:

int a, b, s;

void input()

cout<< "MULTILEVEL INHERITANCE";

cout << "\n Enter two numbers:";

cin >> a>>b;

};

class Second : public First

public:

void add() {

s = a + b;

};

class Third : public Second

{
public:

void display()

cout << "Sum is:" << s;

};

int main()

Third th;

th.input();

th.add();

th.display();

return 0;

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

MULTIPLE INHERITANCE

SOURCE CODE:

#include<iostream>

using namespace std;

class A

protected:

int a;

public:

void get_a(int n)

a=n;

};

class B

protected:

int b;

public:

void get_b(int n)

b=n;

}
};

class C : public A,public B

public:

void display()

cout<<"MULTIPLE INHERITANCE";

std::cout<<"\n The value of a is : "<<a<<std::endl;

std::cout<<"The value of b is : "<<b<<std::endl;

cout<<"Subtraction of a and b is : "<<a-b;

};

int main()

C c;

c.get_a(50);

c.get_b(20);

c.display();

return 0;

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A

HYBRID INHERITANCE

SOURCE CODE:

#include <iostream>

using namespace std;

// base class

class Add

public:

int num1, num2;

int addition(int num1, int num2)

return num1 + num2;

// base class

class Prdt

public:

int a, b;

int product(int a, int b)

return a * b;

}
};

// first sub class

class Average : public Add

public:

int c, d;

void avg()

cout << "\nAverage of the 2 numbers = " << (float)Add::addition(c, d) / 2;

};

// second sub class

class Print : public Add, public Prdt

public:

int e, f;

void print()

cout << "Sum of the 2 numbers = " << Add::addition(e, f);

cout << "\nProduct of the 2 numbers = " << Prdt::product(e, f);

};

// main function

int main()

Print obj1;

Average obj2;

cout<<"HYBRID INHERITANCE";

cout <<"\n Enter 2 numbers: ";


cin >> obj1.e >> obj1.f;

obj1.print();

obj2.c = obj1.e;

obj2.d = obj1.f;

obj2.avg();

return 0;

OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

CLASS AND OBJECTS

SOURCE CODE:

import java.util.Scanner;

class Employee {

int id;

String name;

float sal;

class Main {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter How many employee:");

int k = sc.nextInt();

Employee emp[] = new Employee[k];

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

emp[i] = new Employee();


System.out.println("Enter " + (i + 1) + " Employee data :");

System.out.print("Enter employee id :");

emp[i].id = sc.nextInt();

System.out.print("Enter employee name :");

emp[i].name = sc.next();

System.out.print("Enter employee salary :");

emp[i].sal = sc.nextFloat();

System.out.println("\n\n============ All employee details are


:============\n");

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

System.out.println("Employee id name and salary :" + emp[i].id + " " +


emp[i].name + " " + emp[i].sal);

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

CONSTRUCTOR AND DESTRUCTOR

SOURCE CODE:

class ConstandDest{

public static void main(String args[]){

//create array of employee object

Employee[] obj = new Employee[2] ;

//create & initialize actual employee objects using constructor

obj[0] = new Employee(100,"RAJ");

obj[1] = new Employee(200,"STEVE");

//display the employee object data

System.out.println("Employee Object 1:");

obj[0].showData();

System.out.println("Employee Object 2:");

obj[1].showData();

obj=null;

System.gc();

//Employee class with empId and name as attributes

class Employee{

int empId;

String name;
//Employee class constructor

Employee(int eid, String n){

empId = eid;

name = n;

public void showData(){

System.out.print("EmpId = "+empId + " " + " Employee Name = "+name);

System.out.println();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

FUNCTION OVERLOADING

SOURCE CODE:

// Java program to demonstrate working of method

// overloading in Java.

public class Sum {

// Overloaded sum(). This sum takes two int parameters

public void sum(int x, int y)

int add = x + y;

System.out.println("Sum of Two Interger numbers is:"+add);

// Overloaded sum(). This sum takes three int parameters

public void sum(int x, int y, int z)

int addd=x + y + z;

System.out.println("Sum of Three Interger numbers is:"+addd);

// Overloaded sum(). This sum takes two double parameters

public void sum(double x, double y)


{

double ad=x + y;

System.out.println("Sum of Two Float numbers is:"+ad);

// Driver code

public static void main(String args[])

Sum s = new Sum();

s.sum(10, 20);

s.sum(10, 20, 30);

s.sum(10.5, 20.5);

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

MULTIPLE INHERITANCE

SOURCE CODE:

import java.util.*;

interface Emp{

public void display();

class Alpha{

int a,b;

void getdata()

Scanner scn=new Scanner(System.in);

System.out.println("Enter A value is:");

a=scn.nextInt();

System.out.println("Enter B value is:");

b=scn.nextInt();

class Multiple extends Alpha implements Emp{

int add, sub,mul;

public void display()

{
add=a+b;

sub=a-b;

mul=a*b;

System.out.println("Addition value is:" +add);

System.out.println("Subtracted value is:" +sub);

System.out.println("Multiplied value is:" +mul);

public static void main(String[] args)

Multiple obj=new Multiple();

obj.getdata();

obj.display();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

MULTILEVEL INHERITANCE

SOURCE CODE:

import java.util.*;

class Alpha{

int a,b;

void getdata()

Scanner scn=new Scanner(System.in);

System.out.println("Enter A value is:");

a=scn.nextInt();

System.out.println("Enter B value is:");

b=scn.nextInt();

class Beta extends Alpha {

int add, sub,mul;

public void display()

add=a+b;

sub=a-b;

mul=a*b;

System.out.println("Addition value is:" +add);


System.out.println("Subtracted value is:" +sub);

System.out.println("Multiplied value is:" +mul);

class Multilevel extends Beta{

public static void main(String[] args)

Multilevel obj=new Multilevel();

obj.getdata();

obj.display();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

HYBRID INHERITANCE

SOURCE CODE:

import java.util.*;

class Alpha{

int a,b,c,d;

Scanner scn=new Scanner(System.in);

void getdata()

System.out.println("Enter A value is:");

a=scn.nextInt();

System.out.println("Enter B value is:");

b=scn.nextInt();

void getdata1()

System.out.println("Enter C value is:");

c=scn.nextInt();

System.out.println("Enter D value is:");

d=scn.nextInt();

}
class Beta extends Alpha {

int add, sub,mul;

public void display()

add=a+b;

sub=a-b;

mul=a*b;

System.out.println("Addition value A and Bis:" +add);

System.out.println("Subtracted value A and B is:" +sub);

System.out.println("Multiplied value A and Bis:" +mul);

class Delta extends Alpha {

int add1, sub1,mul1;

public void display1()

add1=c+d;

sub1=c-d;

mul1=c*d;

System.out.println("Addition value C and D is:" +add1);

System.out.println("Subtracted value C and D is:" +sub1);

System.out.println("Multiplied value C and D is:" +mul1);

}
class Hybrid extends Beta{

public static void main(String[] args)

Hybrid obj=new Hybrid();

Delta obj1=new Delta();

obj.getdata();

obj1.getdata1();

obj.display();

obj1.display1();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

I/O STREAMS AND FUNCTIONS

SOURCE CODE:

INPUT STREAM:

import java.io.*;

class Streaminput{

public static void main(String[] args) throws IOException {

try{

// loading a file into f variable

FileInputStream f = new FileInputStream("input.txt");

// initializing x to 0

int x = 0;

// while loop untill the end of the file.

while ((x = f.read())!=-1){

// printing the character

System.out.print((char)x);

// closing a file

f.close();

catch(Exception e){

// printing exception
System.out.println(e);

OUTPUT STREAM:

import java.io.*;

class Streamoutput{

public static void main(String[] args) throws IOException {

try{

// loading a file into f variable

FileOutputStream f = new FileOutputStream("output.txt");

String s = "Hello I am Nagaraj Welcome to CSE";

char arr[] = s.toCharArray();

// initializing x to 0

int x = 0;

// while loop untill the end of the string.

while(x < s.length()){

// writing a byte into "output.txt" file

f.write(arr[x++]);

// closing a file

f.close();

catch(Exception e){

// printing exception

System.out.println(e);

}
}

}
OUTPUT:

INPUT STREAM

OUTPUT STREAM
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

TYPE CONVERSIONS

SOURCE CODE:

//Type Casting Program in Java

import java.util.Scanner;

public class Conversion

public static void main(String[] args)

//Take input from the user

// create an object of Scanner class

Scanner sc = new Scanner(System.in);

// ask users to enter the number

System.out.println("Enter the number: ");

double d=sc.nextDouble();

// narrowing or explicit type conversion

float f=(float)d;

//explicit type casting

long l = (long)d;

//explicit type casting

int i = (int)l;

System.out.println("After narrowing or explicit type conversion values are: ");

System.out.println("Double value: "+d);


//fractional part lost

System.out.println("Float value: "+f);

System.out.println("Long value: "+l);

//fractional part lost

System.out.println("Int value: "+i);

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

EXCEPTION HANDLING

SOURCE CODE:

import java.util.*;

class Excepthandle

void getdata()

int a,b,result;

Scanner input=new Scanner(System.in);

System.out.println("Enter A value is:");

a=input.nextInt();

System.out.println("Enter B value is:");

b=input.nextInt();

try{

result=a/b;

System.out.println("Result is :"+result);

catch(Exception e)

System.out.println("Exception caught: Divided by zero give another


number");

getdata();

}
public static void main(String[] args)

Excepthandle obj=new Excepthandle();

obj.getdata();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

NETWORKING CONCEPTS

SOURCE CODE:

CLIENT SIDE:

// A Java program for a ClientSide

import java.io.*;

import java.net.*;

public class clientSide {

// initialize socket and input output streams

private Socket socket = null;

private DataInputStream input = null;

private DataOutputStream out = null;

// constructor to put ip address and port

public clientSide(String address, int port)

// establish a connection

try {
socket = new Socket(address, port);

System.out.println("Connected");

// takes input from terminal

input = new DataInputStream(System.in);

// sends output to the socket

out = new DataOutputStream(

socket.getOutputStream());

catch (UnknownHostException u) {

System.out.println(u);

catch (IOException i) {

System.out.println(i);

// string to read message from input

String line = "";

// keep reading until "End" is input

while (!line.equals("End")) {

try {
line = input.readLine();

out.writeUTF(line);

catch (IOException i) {

System.out.println(i);

// close the connection

try {

input.close();

out.close();

socket.close();

catch (IOException i) {

System.out.println(i);

public static void main(String[] args)

clientSide client
= new clientSide("127.0.0.1", 5000);

}
SERVER SIDE:

// A Java program for a serverSide

import java.io.*;

import java.net.*;

public class serverSide {

// initialize socket and input stream

private Socket socket = null;

private ServerSocket server = null;

private DataInputStream in = null;

// constructor with port

public serverSide(int port)

// starts server and waits for a connection

try {

server = new ServerSocket(port);

System.out.println("Server started");

System.out.println("Waiting for a client ...");

socket = server.accept();

System.out.println("Client accepted");

// takes input from the client socket


in = new DataInputStream(

new BufferedInputStream(

socket.getInputStream()));

String line = "";

// reads message from client until "End" is sent

while (!line.equals("End")) {

try {

line = in.readUTF();

System.out.println(line);

catch (IOException i) {

System.out.println(i);

System.out.println("Closing connection");

// close connection

socket.close();

in.close();

catch (IOException i) {

System.out.println(i);

}
public static void main(String[] args)

serverSide server = new serverSide(5000);

}
OUTPUT:

CLIENT SIDE

SERVER SIDE
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN
YEAR/SEM/SEC: Y2/S4/A

ABSTRACT CLASS

SOURCE CODE:

import java.util.*;

abstract class Alpha{

int a,b;

void getdata()

Scanner scn=new Scanner(System.in);

System.out.println("Enter A value is:");

a=scn.nextInt();

System.out.println("Enter B value is:");

b=scn.nextInt();

abstract void display();

public class Abstr extends Alpha{

int add, sub,mul;

void display()

add=a+b;

sub=a-b;

mul=a*b;

System.out.println("Addition value is:" +add);


System.out.println("Subtracted value is:" +sub);

System.out.println("Multiplied value is:" +mul);

public static void main(String[] args)

Abstr obj=new Abstr();

obj.getdata();

obj.display();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN
YEAR/SEM/SEC: Y2/S4/A

RMI CONCEPTS

SOURCE CODE:

RMI INTERFACE:

import java.rmi.*;

public interface AddServerInterface extends Remote

{double add(double d1,double d2)throws RemoteException; }

RMI IMPLEMENTING CLASS:

import java.rmi.*;

import java.rmi.server.*;

public class AddServerImplementingClass extends UnicastRemoteObject


implements AddServerInterface{

public AddServerImplementingClass()throws RemoteException{

public double add(double d1,double d2)throws RemoteException{

return d1+d2;

}
RMI SERVER SIDE:

import java.net.*;

import java.rmi.*;

public class AddServer

public static void main(String[] args)throws RemoteException

try {

AddServerImplementingClass obj=new AddServerImplementingClass();

Naming.rebind("AddServer",obj);

catch(Exception e)

System.out.println("Exception"+e);

}
RMI CLIENT SIDE:

import java.rmi.*;

public class AddClient{

public static void main(String[] args){

try {

String addServerURL="rmi://"+args[0]+"/AddServer";

AddServerInterface obj=(AddServerInterface)Naming.lookup(addServerURL);

System.out.println("the first number is="+args[1]);

double d1=Double.parseDouble(args[1]);

System.out.println("the second number is="+args[2]);

double d2=Double.parseDouble(args[2]);

System.out.println("the sum is"+obj.add(d1,d2));

catch(Exception e)

System.out.println("Exception="+e);

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

SWING CONCEPTS

SOURCE CODE:

import java.io.*;

import javax.swing.*;

class Add{

void addTwo() {

String firstNumber=JOptionPane.showInputDialog("enter first integer");

String secondNumber=JOptionPane.showInputDialog("enter second integer");

int number1=Integer.parseInt(firstNumber);

int number2=Integer.parseInt(secondNumber);

int sum=number1+number2;

JOptionPane.showMessageDialog(null,"the sum is"+sum,"sum of two


Integers",JOptionPane.PLAIN_MESSAGE);

class Sub{

public void subTwo()

String firstNumber=JOptionPane.showInputDialog("enter first integer");

String secondNumber=JOptionPane.showInputDialog("enter second integer");

int number1=Integer.parseInt(firstNumber);

int number2=Integer.parseInt(secondNumber);

int sub=number1-number2;
JOptionPane.showMessageDialog(null,"the sub is"+sub,"sub of two
Integers",JOptionPane.PLAIN_MESSAGE);

class Mul

public void mulTwo()

String firstNumber=JOptionPane.showInputDialog("enter first integer");

String secondNumber=JOptionPane.showInputDialog("enter second integer");

int number1=Integer.parseInt(firstNumber);

int number2=Integer.parseInt(secondNumber);

int mul=number1*number2;

JOptionPane.showMessageDialog(null,"the mul is: "+mul,"mul of two


Integers",JOptionPane.PLAIN_MESSAGE);

class Div

public void divTwo()

String firstNumber=JOptionPane.showInputDialog("enter first integer");

String secondNumber=JOptionPane.showInputDialog("enter second integer");

int number1=Integer.parseInt(firstNumber);

int number2=Integer.parseInt(secondNumber);

int div=number1/number2;

JOptionPane.showMessageDialog(null,"the div is"+div,"div of two


Integers",JOptionPane.PLAIN_MESSAGE);

}
public class Myclass

public static void main(String[] args)throws IOException

Add a=new Add();

Sub s=new Sub();

Mul m=new Mul();

Div d=new Div();

DataInputStream ds=new DataInputStream(System.in);

while(true)

System.out.println("\nArithmetic Operation");

System.out.println("\n\t1.Addition");

System.out.println("\n\t2.Subtraction");

System.out.println("\n\t3.Multiplication");

System.out.println("\n\t4.Division");

System.out.println("\n\t5.Exit");

System.out.println("\nenter the choice");

String choice1=ds.readLine();

int choice=Integer.parseInt(choice1);

switch(choice)

case 1:

a.addTwo();

break;

case 2:

s.subTwo();

break;

case 3:
m.mulTwo();

break;

case 4:

d.divTwo();

break;

case 5:

System.exit(1);

break;

OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

SIMPLE CALCULATOR

SOURCE CODE:

// Java program to create a simple calculator

// with basic +, -, /, * using java swing elements

import java.awt.event.*;

import javax.swing.*;

import java.awt.*;

class calculator extends JFrame implements ActionListener {

// create a frame

static JFrame f;

// create a textfield

static JTextField l;

// store operator and operands

String s0, s1, s2;

// default constructor

calculator()

s0 = s1 = s2 = "";

// main function

public static void main(String args[])

// create a frame

f = new JFrame("calculator");
try {

// set look and feel

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

catch (Exception e) {

System.err.println(e.getMessage());

// create a object of class

calculator c = new calculator();

// create a textfield

l = new JTextField(16);

// set the textfield to non editable

l.setEditable(false);

// create number buttons and some operators

JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq,
beq1;

// create number buttons

b0 = new JButton("0");

b1 = new JButton("1");

b2 = new JButton("2");

b3 = new JButton("3");

b4 = new JButton("4");

b5 = new JButton("5");

b6 = new JButton("6");

b7 = new JButton("7");

b8 = new JButton("8");

b9 = new JButton("9");

// equals button

beq1 = new JButton("=");


// create operator buttons

ba = new JButton("+");

bs = new JButton("-");

bd = new JButton("/");

bm = new JButton("*");

beq = new JButton("C");

// create . button

be = new JButton(".");

// create a panel

JPanel p = new JPanel();

// add action listeners

bm.addActionListener(c);

bd.addActionListener(c);

bs.addActionListener(c);

ba.addActionListener(c);

b9.addActionListener(c);

b8.addActionListener(c);

b7.addActionListener(c);

b6.addActionListener(c);

b5.addActionListener(c);

b4.addActionListener(c);

b3.addActionListener(c);

b2.addActionListener(c);

b1.addActionListener(c);

b0.addActionListener(c);

be.addActionListener(c);

beq.addActionListener(c);

beq1.addActionListener(c);

// add elements to panel


p.add(l);

p.add(ba);

p.add(b1);

p.add(b2);

p.add(b3);

p.add(bs);

p.add(b4);

p.add(b5);

p.add(b6);

p.add(bm);

p.add(b7);

p.add(b8);

p.add(b9);

p.add(bd);

p.add(be);

p.add(b0);

p.add(beq);

p.add(beq1);

// set Background of panel

p.setBackground(Color.blue);

// add panel to frame

f.add(p);

f.setSize(200, 220);

f.show();

public void actionPerformed(ActionEvent e)

String s = e.getActionCommand();

// if the value is a number


if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {

// if operand is present then add to second no

if (!s1.equals(""))

s2 = s2 + s;

else

s0 = s0 + s;

// set the value of text

l.setText(s0 + s1 + s2);

else if (s.charAt(0) == 'C') {

// clear the one letter

s0 = s1 = s2 = "";

// set the value of text

l.setText(s0 + s1 + s2);

else if (s.charAt(0) == '=') {

double te;

// store the value in 1st

if (s1.equals("+"))

te = (Double.parseDouble(s0) + Double.parseDouble(s2));

else if (s1.equals("-"))

te = (Double.parseDouble(s0) - Double.parseDouble(s2));

else if (s1.equals("/"))

te = (Double.parseDouble(s0) / Double.parseDouble(s2));

else

te = (Double.parseDouble(s0) * Double.parseDouble(s2));

l.setText(s0 + s1 + s2 + "=" + te);

s0 = Double.toString(te);

s1 = s2 = "";
}

else {

// if there was no operand

if (s1.equals("") || s2.equals(""))

s1 = s;

// else evaluate

else {

double te;

// store the value in 1st

if (s1.equals("+"))

te = (Double.parseDouble(s0) +
Double.parseDouble(s2));

else if (s1.equals("-"))

te = (Double.parseDouble(s0) -
Double.parseDouble(s2));

else if (s1.equals("/"))

te = (Double.parseDouble(s0) /
Double.parseDouble(s2));

else

te = (Double.parseDouble(s0) *
Double.parseDouble(s2));

// convert it to string

s0 = Double.toString(te);

// place the operator

s1 = s;

// make the operand blank

s2 = "";

// set the value of text

l.setText(s0 + s1 + s2);
}}}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

AWT CONCEPTS

SOURCE CODE:

// Java program to implement

// a Simple Registration Form

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class MyFrame

extends JFrame

implements ActionListener {

// Components of the Form

private Container c;

private JLabel title;

private JLabel name;

private JTextField tname;

private JLabel mno;

private JTextField tmno;

private JLabel gender;

private JRadioButton male;

private JRadioButton female;

private ButtonGroup gengp;

private JLabel dob;

private JComboBox date;


private JComboBox month;

private JComboBox year;

private JLabel add;

private JTextArea tadd;

private JCheckBox term;

private JButton sub;

private JButton reset;

private JTextArea tout;

private JLabel res;

private JTextArea resadd;

private String dates[]

= { "1", "2", "3", "4", "5",

"6", "7", "8", "9", "10",

"11", "12", "13", "14", "15",

"16", "17", "18", "19", "20",

"21", "22", "23", "24", "25",

"26", "27", "28", "29", "30",

"31" };

private String months[]

= { "Jan", "feb", "Mar", "Apr",

"May", "Jun", "July", "Aug",

"Sup", "Oct", "Nov", "Dec" };

private String years[]

= { "1995", "1996", "1997", "1998",

"1999", "2000", "2001", "2002",

"2003", "2004", "2005", "2006",

"2007", "2008", "2009", "2010",

"2011", "2012", "2013", "2014",

"2015", "2016", "2017", "2018",


"2019" };

// constructor, to initialize the components

// with default values.

public MyFrame()

setTitle("Registration Form");

setBounds(300, 90, 900, 600);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setResizable(false);

c = getContentPane();

c.setLayout(null);

title = new JLabel("Registration Form");

title.setFont(new Font("Arial", Font.PLAIN, 30));

title.setSize(300, 30);

title.setLocation(300, 30);

c.add(title);

name = new JLabel("Name");

name.setFont(new Font("Arial", Font.PLAIN, 20));

name.setSize(100, 20);

name.setLocation(100, 100);

c.add(name);

tname = new JTextField();

tname.setFont(new Font("Arial", Font.PLAIN, 15));

tname.setSize(190, 20);

tname.setLocation(200, 100);

c.add(tname);

mno = new JLabel("Mobile");

mno.setFont(new Font("Arial", Font.PLAIN, 20));


mno.setSize(100, 20);

mno.setLocation(100, 150);

c.add(mno);

tmno = new JTextField();

tmno.setFont(new Font("Arial", Font.PLAIN, 15));

tmno.setSize(150, 20);

tmno.setLocation(200, 150);

c.add(tmno);

gender = new JLabel("Gender");

gender.setFont(new Font("Arial", Font.PLAIN, 20));

gender.setSize(100, 20);

gender.setLocation(100, 200);

c.add(gender);

male = new JRadioButton("Male");

male.setFont(new Font("Arial", Font.PLAIN, 15));

male.setSelected(true);

male.setSize(75, 20);

male.setLocation(200, 200);

c.add(male);

female = new JRadioButton("Female");

female.setFont(new Font("Arial", Font.PLAIN, 15));

female.setSelected(false);

female.setSize(80, 20);

female.setLocation(275, 200);

c.add(female);

gengp = new ButtonGroup();

gengp.add(male);

gengp.add(female);
dob = new JLabel("DOB");

dob.setFont(new Font("Arial", Font.PLAIN, 20));

dob.setSize(100, 20);

dob.setLocation(100, 250);

c.add(dob);

date = new JComboBox(dates);

date.setFont(new Font("Arial", Font.PLAIN, 15));

date.setSize(50, 20);

date.setLocation(200, 250);

c.add(date);

month = new JComboBox(months);

month.setFont(new Font("Arial", Font.PLAIN, 15));

month.setSize(60, 20);

month.setLocation(250, 250);

c.add(month);

year = new JComboBox(years);

year.setFont(new Font("Arial", Font.PLAIN, 15));

year.setSize(60, 20);

year.setLocation(320, 250);

c.add(year);

add = new JLabel("Address");

add.setFont(new Font("Arial", Font.PLAIN, 20));

add.setSize(100, 20);

add.setLocation(100, 300);

c.add(add);

tadd = new JTextArea();

tadd.setFont(new Font("Arial", Font.PLAIN, 15));

tadd.setSize(200, 75);

tadd.setLocation(200, 300);
tadd.setLineWrap(true);

c.add(tadd);

term = new JCheckBox("Accept Terms And Conditions.");

term.setFont(new Font("Arial", Font.PLAIN, 15));

term.setSize(250, 20);

term.setLocation(150, 400);

c.add(term);

sub = new JButton("Submit");

sub.setFont(new Font("Arial", Font.PLAIN, 15));

sub.setSize(100, 20);

sub.setLocation(150, 450);

sub.addActionListener(this);

c.add(sub);

reset = new JButton("Reset");

reset.setFont(new Font("Arial", Font.PLAIN, 15));

reset.setSize(100, 20);

reset.setLocation(270, 450);

reset.addActionListener(this);

c.add(reset);

tout = new JTextArea();

tout.setFont(new Font("Arial", Font.PLAIN, 15));

tout.setSize(300, 400);

tout.setLocation(500, 100);

tout.setLineWrap(true);

tout.setEditable(false);

c.add(tout);

res = new JLabel("");

res.setFont(new Font("Arial", Font.PLAIN, 20));

res.setSize(500, 25);
res.setLocation(100, 500);

c.add(res);

resadd = new JTextArea();

resadd.setFont(new Font("Arial", Font.PLAIN, 15));

resadd.setSize(200, 75);

resadd.setLocation(580, 175);

resadd.setLineWrap(true);

c.add(resadd);

setVisible(true);

// method actionPerformed()

// to get the action performed

// by the user and act accordingly

public void actionPerformed(ActionEvent e)

if (e.getSource() == sub) {

if (term.isSelected()) {

String data1;

String data= "Name : "+ tname.getText() + "\n"+ "Mobile :


"

+ tmno.getText() + "\n";

if (male.isSelected())

data1 = "Gender : Male"

+ "\n";

else

data1 = "Gender : Female"

+ "\n";

String data2

= "DOB : "
+ (String)date.getSelectedItem()

+ "/" + (String)month.getSelectedItem()

+ "/" + (String)year.getSelectedItem()

+ "\n";

String data3 = "Address : " + tadd.getText();

tout.setText(data + data1 + data2 + data3);

tout.setEditable(false);

res.setText("Registration Successfully..");

else {

tout.setText("");

resadd.setText("");

res.setText("Please accept the"

+ " terms & conditions..");

else if (e.getSource() == reset) {

String def = "";

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);

}
}

// Driver Code

class Registration {

public static void main(String[] args) throws Exception

MyFrame f = new MyFrame();

}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A

APPLET CONCEPTS

SOURCE CODE:

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.applet.Applet;

public class Signal extends Applet

implements ActionListener

int colourNum; //global variable which is responible for changing the light

Button bttn1 = new Button ("Stop Traffic" );

Button bttn2 = new Button ("Caution");

Button bttn3 = new Button ("Proceed");

public void init ()

setBackground (Color.lightGray);

bttn1.addActionListener (this); // stop light

bttn2.addActionListener (this); // yellow light

bttn3.addActionListener (this); // green light

add (bttn1);

add (bttn2);

add (bttn3);

}
public void paint (Graphics g) // responsible for graphics “within” the window

g.setColor (Color.black);

g.setColor(colourNum == 1? Color.red : Color.red.darker().darker());

g.fillOval (30, 40, 20, 20); // red light

g.setColor(colourNum == 2? Color.yellow : Color.yellow.darker().darker());

g.fillOval (30, 70, 20, 20); // yello light

g.setColor(colourNum == 3? Color.green : Color.green.darker().darker());

g.fillOval (30, 100, 20, 20); // green light

public void actionPerformed (ActionEvent evt)

if (evt.getSource () == bttn1)

colourNum = 1;

else if (evt.getSource () == bttn2)

colourNum = 2;

else

colourNum = 3;

repaint ();

}
HTML CODE:

<html>

<head>

<title></title>

</head>

<body>

<p>Traffic Applet

<applet code="Signal.class" width=300 height=200>

</applet>

</body>

</html>
OUTPUT:

You might also like