Oop Lab Full Programs
Oop Lab Full Programs
SOURCE CODE:
#include <iostream>
#include<conio.h>
class StudentClass {
private://Access - Specifier
char name[20];
public://Access - Specifier
void read() {
void sum() {
avg = total / 3;
void print() {
cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;
};
int main() {
cout << "Read and Print Student Information Class Example Program In C++\n";
stu1.read();
stu1.sum();
stu1.print();
stu2.read();
stu2.sum();
stu2.print();
getch();
return 0;
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A
SOURCE CODE:
#include <iostream>
#include<conio.h>
class stud
private:
char name[20],add[20];
int roll;
public:
stud();
~stud();
void read();
void display();
};
stud::stud()
cout<<"Constructor\n";
void stud::read()
{
cout<<"Enter the student name:";
cin>>name;
cin>>roll;
cin>>add;
void stud::display()
cout<<"Student Name:"<<name<<endl;
cout<<"Roll number:"<<roll<<endl;
cout<<"Student Address:"<<add<<endl;
stud::~stud()
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>
class shape
public:
float area(float a)
return(pi*a*a);
return(a*b);
float temp;
temp=e*a*b*c;
return temp;
};
int main()
float r,l,b,side[3];
shape circle,rect,tri;
cout<<"CIRCLE"<<endl;
cin>>r;
cout<<"RECTANGLE"<<endl;
cin>>l>>b;
cout<<"TRIANGLE"<<endl;
cin>>side[0]>>side[1]>>side[2];
return 0;
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A
ABSTRACT CLASS
SOURCE CODE:
#include<iostream>
class shape
protected:
float dimension;
public:
void getdimension()
cin>>dimension;
};
public:
float calculatearea()
return dimension*dimension;
};
int main()
{
square s;
cout<<"ABSTRACT CLASS";
s.getdimension();
return 0;
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC: Y2/S4/A
MULTILEVEL INHERITANCE
SOURCE CODE:
#include<iostream>
class First
public:
int a, b, s;
void input()
};
public:
void add() {
s = a + b;
};
{
public:
void display()
};
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>
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;
}
};
public:
void display()
cout<<"MULTIPLE INHERITANCE";
};
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>
// base class
class Add
public:
// base class
class Prdt
public:
int a, b;
return a * b;
}
};
public:
int c, d;
void avg()
};
public:
int e, f;
void print()
};
// main function
int main()
Print obj1;
Average obj2;
cout<<"HYBRID INHERITANCE";
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
SOURCE CODE:
import java.util.Scanner;
class Employee {
int id;
String name;
float sal;
class Main {
int k = sc.nextInt();
emp[i].id = sc.nextInt();
emp[i].name = sc.next();
emp[i].sal = sc.nextFloat();
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A
SOURCE CODE:
class ConstandDest{
obj[0].showData();
obj[1].showData();
obj=null;
System.gc();
class Employee{
int empId;
String name;
//Employee class constructor
empId = eid;
name = n;
System.out.println();
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A
FUNCTION OVERLOADING
SOURCE CODE:
// overloading in Java.
int add = x + y;
int addd=x + y + z;
double ad=x + y;
// Driver code
s.sum(10, 20);
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{
class Alpha{
int a,b;
void getdata()
a=scn.nextInt();
b=scn.nextInt();
{
add=a+b;
sub=a-b;
mul=a*b;
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()
a=scn.nextInt();
b=scn.nextInt();
add=a+b;
sub=a-b;
mul=a*b;
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;
void getdata()
a=scn.nextInt();
b=scn.nextInt();
void getdata1()
c=scn.nextInt();
d=scn.nextInt();
}
class Beta extends Alpha {
add=a+b;
sub=a-b;
mul=a*b;
add1=c+d;
sub1=c-d;
mul1=c*d;
}
class Hybrid extends Beta{
obj.getdata();
obj1.getdata1();
obj.display();
obj1.display1();
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A
SOURCE CODE:
INPUT STREAM:
import java.io.*;
class Streaminput{
try{
// initializing x to 0
int x = 0;
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{
try{
// initializing x to 0
int x = 0;
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:
import java.util.Scanner;
double d=sc.nextDouble();
float f=(float)d;
long l = (long)d;
int i = (int)l;
}
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;
a=input.nextInt();
b=input.nextInt();
try{
result=a/b;
System.out.println("Result is :"+result);
catch(Exception e)
getdata();
}
public static void main(String[] args)
obj.getdata();
}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A
NETWORKING CONCEPTS
SOURCE CODE:
CLIENT SIDE:
import java.io.*;
import java.net.*;
// establish a connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
socket.getOutputStream());
catch (UnknownHostException u) {
System.out.println(u);
catch (IOException i) {
System.out.println(i);
while (!line.equals("End")) {
try {
line = input.readLine();
out.writeUTF(line);
catch (IOException i) {
System.out.println(i);
try {
input.close();
out.close();
socket.close();
catch (IOException i) {
System.out.println(i);
clientSide client
= new clientSide("127.0.0.1", 5000);
}
SERVER SIDE:
import java.io.*;
import java.net.*;
try {
System.out.println("Server started");
socket = server.accept();
System.out.println("Client accepted");
new BufferedInputStream(
socket.getInputStream()));
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)
}
OUTPUT:
CLIENT SIDE
SERVER SIDE
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN
YEAR/SEM/SEC: Y2/S4/A
ABSTRACT CLASS
SOURCE CODE:
import java.util.*;
int a,b;
void getdata()
a=scn.nextInt();
b=scn.nextInt();
void display()
add=a+b;
sub=a-b;
mul=a*b;
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.*;
import java.rmi.*;
import java.rmi.server.*;
return d1+d2;
}
RMI SERVER SIDE:
import java.net.*;
import java.rmi.*;
try {
Naming.rebind("AddServer",obj);
catch(Exception e)
System.out.println("Exception"+e);
}
RMI CLIENT SIDE:
import java.rmi.*;
try {
String addServerURL="rmi://"+args[0]+"/AddServer";
AddServerInterface obj=(AddServerInterface)Naming.lookup(addServerURL);
double d1=Double.parseDouble(args[1]);
double d2=Double.parseDouble(args[2]);
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() {
int number1=Integer.parseInt(firstNumber);
int number2=Integer.parseInt(secondNumber);
int sum=number1+number2;
class Sub{
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
int number1=Integer.parseInt(firstNumber);
int number2=Integer.parseInt(secondNumber);
int mul=number1*number2;
class Div
int number1=Integer.parseInt(firstNumber);
int number2=Integer.parseInt(secondNumber);
int div=number1/number2;
}
public class Myclass
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");
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:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
// create a frame
static JFrame f;
// create a textfield
static JTextField l;
// default constructor
calculator()
s0 = s1 = s2 = "";
// main function
// create a frame
f = new JFrame("calculator");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
catch (Exception e) {
System.err.println(e.getMessage());
// create a textfield
l = new JTextField(16);
l.setEditable(false);
JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq,
beq1;
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
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
// create . button
be = new JButton(".");
// create a panel
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);
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);
p.setBackground(Color.blue);
f.add(p);
f.setSize(200, 220);
f.show();
String s = e.getActionCommand();
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;
l.setText(s0 + s1 + s2);
s0 = s1 = s2 = "";
l.setText(s0 + s1 + s2);
double te;
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));
s0 = Double.toString(te);
s1 = s2 = "";
}
else {
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
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);
s1 = s;
s2 = "";
l.setText(s0 + s1 + s2);
}}}
OUTPUT:
REG. NO: 21TDL002 NAME: AKSHARA PAVITHRAN YEAR/SEM/SEC:
Y2/S4/A
AWT CONCEPTS
SOURCE CODE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame
extends JFrame
implements ActionListener {
private Container c;
"31" };
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.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 = new JLabel("DOB");
dob.setSize(100, 20);
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);
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);
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;
+ tmno.getText() + "\n";
if (male.isSelected())
+ "\n";
else
+ "\n";
String data2
= "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);
}
}
// Driver Code
class Registration {
}
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;
implements ActionListener
int colourNum; //global variable which is responible for changing the light
setBackground (Color.lightGray);
add (bttn1);
add (bttn2);
add (bttn3);
}
public void paint (Graphics g) // responsible for graphics “within” the window
g.setColor (Color.black);
if (evt.getSource () == bttn1)
colourNum = 1;
colourNum = 2;
else
colourNum = 3;
repaint ();
}
HTML CODE:
<html>
<head>
<title></title>
</head>
<body>
<p>Traffic Applet
</applet>
</body>
</html>
OUTPUT: