Final U
Final U
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.
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 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();
}}
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();
}
}
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);
}
}
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);
}}
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();
}}
}}
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();
}}
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();
}
}
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");
}
}
}
class D implements C{
public void show1()
{
System.out.println(“D");
}
public void show2(){
System.out.println("D2")
;
}
}
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();
}
}
class GT {
public static void
main(String args[]) {
GUI ob = new
GUI();
}
}
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();
}
}
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();
}
}
__________________________________________________________________________________