0% found this document useful (0 votes)
11 views26 pages

Final U

Uploaded by

mahambilal020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views26 pages

Final U

Uploaded by

mahambilal020
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

List of Labs

Lab No. Lab Description

1. Write a simple java program that takes input from user by using a method and
also shows the output .It use atleast 2 different data types.
2. Write a java program in oops of Encapsulation
3. Write a java program in oops using public and private access specifier for data
types, methods and constructors and also defines how to use private and public
data types, methods and constructors.
4. Write a java program in oops using parameterized different methods and use
them in main class.
5. Write a java program in oops using two reference for one object.
6. Write a java program in oops of using constructors and also show constructor
overloading.
7. Write a java program in oops of method/function overloading.
8. Write a java program in oops of parameter passing pass by value.
9. Write a java program in oops of parameter passing pass by Reference/ pass by
object.
10. Write a java program in oops of using static keyword with different function and
also using constructor .Also shows which will appears first on screen and static
property is a shared property.
11. Write a java program in oops of returning object from function.
12. Write a java program in oops of inheritance.
13. Write a java program in oops of multilevel inheritance.
14. Write a java program in oops of constructors using Super keyword.
15. Write a java program in oop of method overriding.
16. Write a java program in oop of using abstract classes with abstract and non
abstract methods or functions.
17. Write a java program in oop of using interface class and defining methods using
in interface.
18. Write a java program in oop of using interface class and defining methods using
in interface. Class implements two interfaces.
19. Write a java program in oop of using interface class and defining methods using
in interface. Interface extends interface and class extends class .interface
implements class.
20. Write a java program in oop of aggregation(weak association).
21. Write a java program in oop of composition(strong association).
22. Write a java program of simple GUI interface
23. Write a java program of simple GUI interface of calculator.
24. Write a java program of event driven programming in which it shows hello after
pressing button.
25. Write a java program of event driven programming of calculator.
26. Write a java program of event driven programming of a counter.

OBJECT ORIENTED PROGRAMMING IN JAVA

Program no 1:- Write a simple java program that takes input from
user by using a method and also shows the output .It use atleast 2
different data types.
Source Code Output
import java.util.Scanner;
class volume
{
double len,wi;
int he;
void calinput()
{
Scanner
s=newScanner(System.in);
System.out.print("Enter length:");
len=s.nextDouble();
System.out.print("Enter width:");
wi=s.nextDouble();
System.out.print("Enter height:");
he=s.nextInt();
}
void calvol()
{

System.out.println("Volume="+len*wi
*he);
}}
class V
{
public static void main(String args[])
{
volume v1=new volume();
v1.calinput();
v1.calvol();
}}
Program no 2:- Write a java program in oops of Encapsulation
Source Code Output
import java.util.Scanner;
class area
{
int len;
int breath;
area(int w,int h)
{
len = w;
breath = h;
}
void calarea()

{ Syste
m.out.println("Area="+len*breat
h);
}}
class A
{
public static void main(String
args[])
{
area a=new area(5,9);
a.calarea();
}}

Program no 3:- Write a java program in oops using public and private
access specifier for data types, methods and constructors and also
defines how to use private and public data types, methods and
constructors.
Source Code Output
class volume {
private int len, wi;
public int he;
public volume() {
System.out.println("Construct
or is calling");
}
void setvalues(int h, int w,
int height) {
len = h;
wi = w;
he = height;
}
private void calvolp() {

System.out.println("Volume =
" + len * wi * he);
}
public void calvol() {
calvolp(); }
}
class V {
public static void main
(String args[]) {
volume v1 = new
volume();
v1.setvalues(3, 4, 5);
v1.calvol();
}
}

Program no 4:- Write a java program in oops using parameterized


different methods and use them in main class.
Source Code Output
class area {
int len;
int breadth;
void setvalues(int w,
int h) {
len = w;
breadth = h;
}
void display(String s,
int a) {
System.out.println(s
+ a);
}
void calarea() {
System.out.println("Ar
ea = " + len * breadth);
}}
class A {
public static void
main(String args[])
{
area a = new area();
a.setvalues(5, 10);
a.display("Red", 5);
}
}

Program no 5:- Write a java program in oops using two reference for
one object.
Source Code Output
class area
{
int len;
int breath;
void calarea()
{
System.out.println("Area
="+len*breath);
}}
class A
{
public static void
main(String args[])
{
area a=new area();
a.len=5;
a.breath=8;
a.calarea();
area a1;
a1 = a;
a1.calarea();
a = null;
a1.calarea();
a=null;
a.calarea();
}}

Program no 6:- Write a java program in oops of using constructors


and also show constructor overloading.
Source Code Output
class box {
int a;
int b;
box() {

System.out.println("Construc
tor without parameter");
}
box(int w) {
a=w;

System.out.println("Construc
tor with one parameter");
System.out.println(a);
}
box(int w, int h) {
a=w;
b=h;

System.out.println("Construc
tor with two parameters");
System.out.println(a);
System.out.println(b);
}
void calbox(){
System.out.println(a*b);
}}
class C {
public static void
main(String args[]) {
box c = new box();
box c1 = new box(3);
box c2 = new box(4, 7);
c2.calbox();
}
}

Program no 7:- Write a java program in oops of method/function


overloading.
Source Code Output
class area
{
int h;
int w;
void setvalues(){
h=0;
w=0;
}
void setvalues(int x){
h=x;
w=x*2;
}
void setvalues(int x,int
y){
h=x;
w=y;
}
void calarea()
{
System.out.println("Area
="+l*w);
}}
class A
{
public static void
main(String args[])
{
area a=new area();
a.setvalues(4);
a.calarea();
a.setvalues(2,3);
a.calarea();
a.setvalues();
a.calarea();
}}

Program no 8:- Write a java program in oops of parameter passing


pass by value.
Source Code Output
class A{
int a;
int b;
void fun(int P,int Q){
P=P+2;
Q=Q+4;

System.out.println(P);

System.out.println(Q);
}
class main{
public static void
main(String arga[]){
A ob=new A();
int x=4;
int y=7;
ob.fun(x,y);
}
}

Program no 9:- Write a java program in oops of parameter passing


pass by Reference/ pass by object.
Source Code Output
class A
{int a;
int b;
void sum(){

System.out.println(a+b);
}
}
class B{
void fun(A obj){
obj.a=obj.a+2;
obj.b=obj.b+3;

System.out.println(obj.a)
;

System.out.println(obj.b)
;}}
class main{
public static void
main(String args[]){
A ob=new A();
ob.a=3;
ob.b=5;
B abc=new B();
abc.fun(ob);
}}

Program no 10:- Write a java program in oops of using static keyword


with different function and also using constructor .Also shows which
will appears first on screen and static property is a shared property.
Source Code Output
class b{
int a;
static int b;
b()
{
b=7;
System.out.println(“cons
tructor”);
}

static {
b=10;
System.out.println("1stat
ic");
}
void show()
{
b=6;

System.out.println("meth
od ");

System.out.println(a);

System.out.println(b );
}
static {
b=16;

System.out.println(“2stat
ic”);
}}
class H {
public static void
main(String[] args) {
b ob=new b();
ob.b=5;
ob.show();
}}

Program no 11:- Write a java program in oops of returning object


from function.
Source Code Output
class A{
int a;
int b;
void show()
{
System.out.println(a);
System.out.println(b);
}
}
class B{
A few()
{
A ob=new A();
ob.a=2;
ob.b=7;
return ob;
}}
class H {
public static void
main(String[] args) {
B obj=new B();
A ref=obj.few();
ref.show();
}}

Program no 12:- Write a java program in oops of inheritance.


Source Code Output
class A{
int a;
void show()
{
System.out.println(a);
System.out.println(“Clas
s A”);
}}
class B extends A{
int b
void show()
{
System.out.println(a);
System.out.println(b);
System.out.println(“Clas
s B”);
}}
class G {
public static void
main(String[] args) {
A ob=new A();
ob.a=2;
ob.show();
B obj=new B();
obj.b=4;
obj.show();

}}

Program no 13:- Write a java program in oops of multilevel


inheritance.
Source Code Output
class A{
int a;
void show()
{
System.out.println(a);
System.out.println(“Clas
s A”);
}}
class B extends A{
int b;
void show()
{
System.out.println(a);
System.out.println(b);
System.out.println(“Clas
s B”);
}}
class C extends A{
int c;
void show()
{
System.out.println(a);
System.out.println(c);
System.out.println(“Clas
s C”);
}}
class D extends C{
int d;
void show()
{
System.out.println(a);
System.out.println(c);
System.out.println(d);
System.out.println(“Clas
s D”);
}}

class G {
public static void
main(String[] args) {
A ob=new A();
ob.a=2;
ob.show();
D obj=new D();
obj.c=4;
obj.d=7;
obj.show();

}}

Program no 14:- Write a java program in oops of constructors using


Super keyword.
Source Code Output
class W {
public static void
main(String[] args) {
B ob =new B();
ob.show();
ob.showB();
}
}
class A{
int a,b,c;
A()
{
a=6;
b=3;
c=8;
System.out.println("a"+a
);
}
void show ()
{ System.out.println
("a"+a);
}}
class B extends A{
int d,e;
B(){
super();
System.out.println("a"+a
);
d=5;
e=3;
}
void showB(){
System.out.println("b"+e
);
}}

Program no 15:- Write a java program in oops of method overriding.


Source Code Output
class A{
int a;
void show()
{

System.out.println(a);
}}
class B extends A{
int b;
void show(){

System.out.println(a);

System.out.println(b);
}
}
class D{
public static void
main( String args[]) {
A obj = new A();
obj.a=4;
obj.show();
B obj2 = new
B();
obj2.a=6;
obj2.b=9;
obj2.show();
}
}
Program no 16:- Write a java program in oops of using abstract
classes with abstract and non abstract methods or functions.
Source Code Output
abstract class university{
void show(){
System.out.println("Univ
ersity of Education
Faisalabad");
}
abstract void work();
}
class comlab extends
university{
void work(){
System.out.println("Com
puter Work");
}
}
class playground extends
university{
void work(){
System.out.println("Stud
ents playing");
}
}
class main {
public static void
main(String args[]){
university u;
comlab c=new comlab();
playground p=new
playground();
u=c;
u.show();
u.work();
u=p;
u.work();
}
}

Program no 17:- Write a java program in oop of using interface class


and defining methods using in interface.
Source Code Output
interface A {
void show();
}

class B implements A {
public void show() {

System.out.println("Hell
o from interface A!");
}
}
class Main {
public static void
main(String[] args) {
A obj = new B();
obj.show();
}
}
Program no 18:- Write a java program in oops of using interface class
and defining methods using in interface. Class implements two
interfaces.
Source Code Output
interface A {
void show();
}
interface B{
void show1();
}
class C implements A,B
{
public void show() {

System.out.println("Hell
o from interface A!");
}
public void show1()
{

System.out.println("Broo
oo");
}
}

public class Main {


public static void
main(String[] args) {
C obj = new C();
obj.show();
obj.show1();
}
}

Program no 19:- Write a java program in oop of using interface class


and defining methods using in interface. Interface extends interface
and class extends class .interface implements class.
Source Code Output
interface A {
void show1();
}
interface B{
void show2();
}
interface C extends A,B
{

}
class D implements C{
public void show1()
{

System.out.println(“D");
}
public void show2(){

System.out.println("D2")
;
}
}

public class Main {


public static void
main(String[] args) {
D obj = new D();
obj.show2();
obj.show1();
}
}

Program no 20:- Write a java program in oops of aggregation(weak


association).
Source Code Output
class Vehicle {
String Model;
String Num;
Vehicle(String a,
String b) {
Model = a;
Num = b;
}
void findLocation() {

System.out.println("In
the Garage");
}
void
viewInformation() {

System.out.println(Mode
l);

System.out.println(Num)
;
}
}
class showroom {
void open() {
}
void close() {
}
void
returnvehicle(String
carno, String date) {
}
Vehicle
issuevehicle(String m,
String n, String p) {
Vehicle
requestedvehicle = new
Vehicle(m,n );
return
requestedvehicle;
}
}
class M {
public static void
main(String args[]) {
showroom L = new
showroom();
L.open();
Vehicle
receivedvehicle;
receivedvehicle =
L.issuevehicle("Mercede
s", "RE4372",
"Rs.30,000,000");
L.close();
L = null;

receivedvehicle.viewInfo
rmation();
}
}

Program no 21:- Write a java program in oops of composition(strong


association).
Source Code Output
class Arith
{
int a;
int b;
void setValues(int P,int
Q)
{
a = P;
b = Q;
}
int sum()
{
return (a+b);
}
void calcAvg1 (int P1,
int Q1)
{
setValues(P1,Q1);
int R;
R = sum();
System.out.println(R/
2.0);
}
}
class Calc{
Arith A;
Calc()
{
A = new Arith();
}
void calcAvg(int X,int
Y)
{
A.calcAvg1(X,Y);
}}
class M
{
public static void
main(String args[])
{
Calc C = new Calc();
C.calcAvg(2,3);
}
}

Program no 22:- Write a java program of simple GUI interface


Source Code Output
import java.awt.*;
import javax.swing.*;
class a{
a(){
JFrame F=new
JFrame("AWT Test");
Container z;
z=F.getContentPane();
z.setLayout(new
BorderLayout());
JLabel C1=new
JLabel("Label");
JTextField C2=new
JTextField("Textfield");
JButton C3=new
JButton();
F.setSize(200,200);
F.setVisible(true);
JPanel P=new JPanel();
P.setLayout(new
GridLayout(1,3));
JButton b1=new
JButton("Button1");
JButton b2=new
JButton("Button2");
JButton b3=new
JButton("Button3");
P.add(b1);
P.add(b2);
P.add(b3);
z.add(C1,BorderLayout.
WEST);
z.add(C2,BorderLayout.
EAST);
z.add(P,BorderLayout.S
OUTH);
}}
class c
{
public static void
main(String args[]){
a ob=new a();
}}

Program no 23:- Write a java program of simple GUI interface of


calculator.
Source Code Output
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class GUI {
GUI() {
JFrame F = new
JFrame();
Container Z;
Z=
F.getContentPane();
Z.setLayout(new
BorderLayout());
JLabel c1 = new
JLabel(" ");
JTextField c2 =
new JTextField();
JButton c3 = new
JButton("My
Calculator");
F.setSize(400, 500);
F.setVisible(true);
JPanel P = new
JPanel();
P.setLayout(new
GridLayout(4, 4));
JButton b1 = new
JButton("1");
JButton b2 = new
JButton("2");
JButton b3 = new
JButton("3");
JButton b4 = new
JButton("C");
JButton b5 = new
JButton("4");
JButton b6 = new
JButton("5");
JButton b7 = new
JButton("6");
JButton b8 = new
JButton("+");
JButton b9 = new
JButton("7");
JButton b10 = new
JButton("8");
JButton b11 = new
JButton("9");
JButton b12 = new
JButton("-");
JButton b13 = new
JButton("0");
JButton b14 = new
JButton(".");
JButton b15 = new
JButton("*");
JButton b16 = new
JButton("=");
P.add(b1);
P.add(b2);
P.add(b3);
P.add(b4);
P.add(b5);
P.add(b6);
P.add(b7);
P.add(b8);
P.add(b9);
P.add(b10);
P.add(b11);
P.add(b12);
P.add(b13);
P.add(b14);
P.add(b15);
P.add(b16);
Z.add(c1,
BorderLayout.NORTH);
Z.add(c3,
BorderLayout.SOUTH);
Z.add(P,
BorderLayout.CENTER)
;}
}

class GT {
public static void
main(String args[]) {
GUI ob = new
GUI();
}
}

Program no 24:- Write a java program of event driven programming


in which it shows hello after pressing button.
Source Code Output
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class A implements
ActionListener {
A() {
JFrame F = new
JFrame("AWT Test");
Container z;
z=
F.getContentPane();
z.setLayout(new
FlowLayout());
JTextField C2 =
new JTextField(20);
JButton C3 = new
JButton("My Button");
F.setSize(200, 200);
F.setVisible(true);
z.add(C2);
z.add(C3);

C3.addActionListener(th
is);
}

public void
actionPerformed(Action
Event e) {
if
(e.getActionCommand().
equals("My Button")) {
JOptionPane.showMessa
geDialog(null,
"HELLO");
}}
public static void
main(String args[]) {
A ob = new A();
}
}

Program no 25:- Write a java program of event driven programming


of calculator.
Source Code Output
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class abc1 implements
ActionListener
{
JFrame F;
Container C;
JTextField t1;
JTextField t2;
JLabel R;
JButton b1;
JButton b2;
abc1()
{
F = new JFrame();
C = F.getContentPane();
C.setLayout(new
FlowLayout());
t1 = new JTextField(20);
t2 = new JTextField(20);
R = new
JLabel("Result");
b1 = new
JButton("Plus");
b2 = new
JButton("Minus");
b1.addActionListener(thi
s);

b2.addActionListener(thi
s);
C.add(t1);
C.add(t2);
C.add(b1);
C.add(b2);
C.add(R);
F.setSize(400,400);
F.setVisible(true);
}
public void
actionPerformed(Action
Event E)
{ if
(E.getSource() == b1)
{
double A;
double B;
A=
Double.parseDouble(t1.g
etText());
B=
Double.parseDouble(t2.g
etText());
double Res = A + B;
R.setText("" + Res);
}
else if (E.getSource() ==
b2)
{
double A;
double B;
A=
Double.parseDouble(t1.g
etText());
B=
Double.parseDouble(t2.g
etText());
double Res = A - B;
R.setText("" + Res);
}
}
}
class abc
{
public static void
main(String args[])
{
abc1 a = new abc1();
}
}
Program no 26:- Write a java program of event driven programming
of a counter.
Source Code Output
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class a {
JFrame J;
JLabel c1;
JTextField c2;
JButton c3;
int count = 0;

a() {
J = new
JFrame("Counter
Program");
c1 = new
JLabel("Counter");
c2 = new
JTextField("0");
c3 = new
JButton("Count");

c3.addActionListener(ne
w ActionListener() {
public void
actionPerformed(Action
Event e) {
count++;

c2.setText(Integer.toStri
ng(count));
}
});

J.setLayout(new
FlowLayout());
J.add(c1);
J.add(c2);
J.add(c3);
J.setSize(200, 100);
J.setVisible(true);
}
}
class d {
public static void
main(String[] args) {
a ob = new a();
}
}

__________________________________________________________________________________

You might also like