java file.docx
TRANSCRIPT
-
8/14/2019 java file.docx
1/37
CONTINENT L COLLEGE OF HIGHER STUDIES
Practical FileProgramming
UsingJ V
Bachelor of Computer pplications
Submitted to:
Mr. Arpreet Singh
Signature:
Submitted by:Name :Manpreet Singh
Roll No :202Course :BCA
Semester :5th
-
8/14/2019 java file.docx
2/37
INDEX
Sr.No. Program Remarks
1. To print Hello
2. Construction of Command line arguments
3. Creation of Pyramid using Labeled Loops(Break/Continue)
4. Illustration of increment and decrement operator
5. Usage of Instance variables
6. Working of Type Casting Operators
7. Illustration of implementation of Wrapper Classes
8. Program to find the factorial of a number
9. Method Overloading
10. Constructor Overloading
11. Method Overriding
12. Inheritance
13. Program to how Packages are created and imported
14. Abstract Classes and Abstract Methods
15. Program to implement an Interfaces
16. Exception Handling Using Try Catch
17. Exception Handling Using Throw, Throws
18. Multithreading - Implementing Runnable Interface
19. Synchronization in Multithreading
20. Using Suspend(), Resume(), isAlive()
-
8/14/2019 java file.docx
3/37
1. To print Hello.
Class HELLO{
public static void main(String[] args)
{System.out.print("hello APP");System.out.println("helloWORLD APP");
}}
-
8/14/2019 java file.docx
4/37
2. Construction of Command line arguments.
classcomlinetests{
public static void main(String args[]){
int count,i=0;String string ;count = args.length;System.out.println("number of arguments = " +count);while (i
-
8/14/2019 java file.docx
5/37
3. Creation of Pyramid using Labeled Loops (Break/Continue).
classcontinuebreak
{
public static void main(String args[])
{loop1:
for(inti=1;i=10)
break;
for(int j=1;j
-
8/14/2019 java file.docx
6/37
4. Illustration of increment and decrement operator.
classincdecreament{
public static void main (String args[])
{ int m=10,n=20;System.out.println("m="+m);System.out.println("n="+n);System.out.println("++m="+ ++m);System.out.println("n++="+ n++);System.out.println("m="+m);System.out.println("n="+n);
}}
-
8/14/2019 java file.docx
7/37
5. Usage of Instance variables.
classInstanceofDemo
{
public static void main(String args[])
{Parent obj1 = new Parent();
Parent obj2 = new Child();
System.out.println("obj1 instanceof Parent"+(obj1 instanceof Parent));
System.out.println("obj1 instanceof Child"+(obj1 instanceof Child));
System.out.println("obj1 instanceofMyinterface="+(obj1
instanceofMyinterface));
System.out.println("obj2 instanceof Parent"+(obj2 instanceof Parent));
System.out.println("obj2 instanceof Child"+(obj2 instanceof Child));
System.out.println("obj2 instanceofMyinterface"+(obj2
instanceofMyinterface));
}
}
class Parent{}
class Child extends Parent implements Myinterface{}
interfaceMyinterface{}
-
8/14/2019 java file.docx
8/37
6. Working of Type Casting Operators.
class Casting
{
public static void main(String args[])
{float sum;
inti;
sum=0.0f;
for(i=1;i
-
8/14/2019 java file.docx
9/37
7. Working of Type Casting Operators.
classtypewrap{
public static void main(String args[])
{ System.out.println("varibles create");char c = 'x';byte b = 50;short s = 1996;inti = 123456789;long l1 = 1234567654321l;float f1 = 3.142f;float f2 = 1.2e-5f;double d2 = 0.000000987;System.out.println("c=" + c);
System.out.println("b=" + b);System.out.println("s=" + s);System.out.println("i=" + i);System.out.println("l1=" + l1);System.out.println("f1=" + f1);System.out.println("f2=" + f2);System.out.println("d2=" + d2);System.out.println(" ");System.out.println("types converted");short s1 = (short)b;short s2 = (short)i; /*produces incorrect result */
float n1 = (float)l1;int m1 = (int)f1; /*fractional part is lost */System.out.println("(short)b=" + s1);System.out.println("(short )i =" + s2);System.out.println("(float)l1 =" + n1);System.out.println("(int)f2 =" + m1);
}}
-
8/14/2019 java file.docx
10/37
classexpresswrap{
public static void main(String args[]){
//declaration and initializationint a=10,b=5,c=8,d=2;float x=6.4f,y=3.0f;
// order of evaluationint answer1=a*b+c/d;int answer2=a*(b+c)/d;
//type conversionfloat answer3=a/c;float answer4=(float)a/c;float answer5 =a/y;
//module operationint answer6=a%c;float answer7=x%y;
//logic operation
-
8/14/2019 java file.docx
11/37
boolean bool1=a>b&&c>d;boolean bool2=a
-
8/14/2019 java file.docx
12/37
8. Program to find the factorial of a number.
class factorial{
int fact(int n)
{ int result;if(n==1)return 1;result = fact(n-1)*n;return result;
}}class f{
public static void main(String args[]){
factorial f=new factorial();System.out.println("factorial of 3 is"+f.fact(3));System.out.println("factorial of 4 is"+f.fact(4));System.out.println("factorial of 5 is"+f.fact(5));System.out.println("factorial of 6 is"+f.fact(6));System.out.println("factorial of 7 is"+f.fact(7));System.out.println("factorial of 8 is"+f.fact(8));System.out.println("factorial of 9 is"+f.fact(9));System.out.println("factorial of 10 is"+f.fact(10));
}
-
8/14/2019 java file.docx
13/37
-
8/14/2019 java file.docx
14/37
9. Method Overloading.
class Rectangle
{
intlength,width; // Declaration of variables
voidgetData(int x, int y){
length=x;
width=y;
}
intrectArea() // Definition of another method
{
int area=length*width;
return(area);
}
}
classRectArea // Class with main method
{
public static void main(String args[])
{
int area1,area2;
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.length=15;rect2.width=10;
area1=rect1.length*rect1.width;
rect1.getData(15,10);
area1=rect1.rectArea();
rect2.getData(20,12); //accessing method
area2=rect2.rectArea();
System.out.println("Area1 = " +area1);
System.out.println("Area2 = " +area2);
}
}
-
8/14/2019 java file.docx
15/37
-
8/14/2019 java file.docx
16/37
10.Constructor Overloading (parameterized Constructor &
explicit constructor).
class Room
{
intlength,breadth;
Room(int x, int y)
{
length=x;
breadth=y;
}
Room(int x)//constructor 2
{
length=breadth=x;
}int area()
{
return (length*breadth);
}
}
classRoomArea
{
public static void main(String args[])
{
Room room1=new Room(15,10);Room room2=new Room(8);
int area1=room1.area();
int area2=room2.area();
System.out.println("Area1=" +area1);
System.out.println("Area2=" +area2);
}
}
-
8/14/2019 java file.docx
17/37
-
8/14/2019 java file.docx
18/37
11.Method Overriding.
class A
{
inti,j;
A (int a, int b){
i=a;
j=b;
}
void show() //display
{
System.out.println(" i and j " + j + " " + j);
}
}
class B extends A
{
int k;
B(inta,intb,int c)
{
super(a,b);
k=c;
}
void show()
{System.out.println(" K: " + k);
}
}
class override
{
public static void main(String args[])
{
B sub=new B(1,2,3);
sub.show(); //this calls show() in B
}
}
-
8/14/2019 java file.docx
19/37
-
8/14/2019 java file.docx
20/37
12.Inheritance (Multiple Inheritance, Hierarchical Inheritance).
class Person
{
String name;
int age;voidgetData(String a, int b)
{
name=a;
age=b;
}
void show()
{
System.out.println("Name=" +name);
System.out.println("Age=" +age);
}
}
class Teacher extends Person
{
String qual;
floatsal;
voidgetqual(String s)
{
qual=s;
}voidgetsal(float a)
{
sal=a;
}
void display()
{
System.out.println("Name of the teacher=" +name);
System.out.println("Age of the teacher=" +age);
System.out.println("Qualification of the teacher=" +qual);
System.out.println("Salary of the teacher=" +sal);
}
}
classGuestTeacher extends Teacher
{
floatppl;
voidgetppl(float a)
-
8/14/2019 java file.docx
21/37
{
ppl=a;
}
void display()
{
System.out.println("Name of the teacher=" +name);
System.out.println("Age of the teacher=" +age);
System.out.println("Qualification of the teacher=" +qual);
System.out.println("Salary of the teacher=" +sal);
}
}
classTestinheritance
{
public static void main(String args[])
{Person prs = new Person();
Teacher tech = new Teacher();
GuestTeachergt = new GuestTeacher();
tech.getData("Sushil",30);
tech.getqual("MCA");
tech.getsal(12000.00f);
tech.display();
gt.getData("Anil",25);
gt.getqual("MCA");
gt.getsal(8000.00f);gt.display();
}
}
-
8/14/2019 java file.docx
22/37
Application to single Inheritance.
class Room{
int length;
int breadth;
Room(int x , int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
classRoomArea extends Room // inheriting room
{
int height;
-
8/14/2019 java file.docx
23/37
RoomArea(int x, int y, int z)
{
super(x,y); //pass value to super class
height = z;
}
int volume()
{
return(length*breadth*height);
}
}
classInherTest
{
public static void main(String args[])
{
RoomArea room1= new RoomArea(14,12,10);int area1=room1.area(); //super class method
int volume1=room1.volume(); //base class method
System.out.println(" Area 1 = " +area1);
System.out.println(" volume = " +volume1);
}
}
-
8/14/2019 java file.docx
24/37
13.Program to how Packages are created and imported.
Code inside package(directory) mypack.
packagemypack;
public class mathmetics{
publicint fact(int n)
{
int s=1;
if(n!=0)
for(inti=1;i
-
8/14/2019 java file.docx
25/37
-
8/14/2019 java file.docx
26/37
14.Abstract Classes and Abstract Methods.
abstract class Figure
{
double dim1;
double dim2;Figure(double a, double b)
{
dim1 = a;
dim2 = b;
}
abstract double area();
}
class Rectangle extends Figure
{
Rectangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Rectangle.");
return dim1 * dim2;
}
}class Triangle extends Figure
{
Triangle(double a, double b)
{
super(a, b);
}
double area()
{
System.out.println("Inside Area for Triangle.");
return dim1 * dim2 / 2;
}
}
class J30
{
public static void main(String args[])
{
-
8/14/2019 java file.docx
27/37
Rectangle r = new Rectangle(9, 5);
Triangle t = new Triangle(10, 8);
Figure figref;
figref = r;
System.out.println("Area is " + figref.area());
figref = t;
System.out.println("Area is " + figref.area());
}
}
-
8/14/2019 java file.docx
28/37
15.Program to implement an Interfaces.
interface A
{
void meth1();
void meth2();}
interface B extends A
{
void meth3();
}
classMyClass implements B
{
public void meth1()
{
System.out.println("Implement meth1().");
}
public void meth2()
{
System.out.println("Implement meth2().");
}
public void meth3()
{
System.out.println("Implement meth3().");
}}
class J31
{
public static void main(String arg[])
{
MyClassob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}
-
8/14/2019 java file.docx
29/37
-
8/14/2019 java file.docx
30/37
16.Exception Handling Using Try Catch.
class J34
{
public static void main(String args[])
{try
{
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
}
catch(ArithmeticException e)
{
System.out.println("Divide by 0: " + e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
-
8/14/2019 java file.docx
31/37
17.Exception Handling Using Throw, Throws.
class J35
{
static void throwOne() throws IllegalAccessException
{System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try
{
throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
-
8/14/2019 java file.docx
32/37
18.Multithreading - Implementing Runnable Interface.
classNewThread implements Runnable
{
Thread t;
NewThread(){
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
public void run()
{
try
{
for(inti = 5; i> 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}System.out.println("Exiting child thread.");
}
}
class J37
{
public static void main(String args[])
{
newNewThread();
try
{
for(inti = 5; i> 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
-
8/14/2019 java file.docx
33/37
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
-
8/14/2019 java file.docx
34/37
19.Synchronization in Multithreading.
Class Callme
{
void call(String msg)
{System.out.print("[" + msg);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println("]");
}
}
class Caller implements Runnable
{
String msg;
Callme target;
Thread t;
public Caller(Callmetarg, String s)
{target = targ;
msg = s;
t = new Thread(this);
t.start();
}
public void run()
{
synchronized(target)
{
target.call(msg);
}
}
}
class J39
{
public static void main(String args[])
-
8/14/2019 java file.docx
35/37
{
Callme target = new Callme();
Caller ob1 = new Caller(target, "Hello");
Caller ob2 = new Caller(target, "Synchronized");
Caller ob3 = new Caller(target, "World");
try
{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e)
{
System.out.println("Interrupted");
}}
}
-
8/14/2019 java file.docx
36/37
20.Using Suspend(), Resume(), isAlive().
Class NewThread implements Runnable
{
String name;
Thread t;NewThread(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start();
}
public void run()
{
try
{
for(inti = 5; i> 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
class J40
{
public static void main(String args[])
{
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
try {
-
8/14/2019 java file.docx
37/37
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: "+ ob1.t.isAlive());
System.out.println("Thread Two is alive: "+ ob2.t.isAlive());
System.out.println("Thread Three is alive: "+ ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
}