Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

CONTINENTAL COLLEGE OF HIGHER STUDIES

Practical File

Programming Using

JAVA
Bachelor of Computer Applications

Submitted to:
Mr. Arpreet Singh

Submitted by:
Name Roll No :Manpreet Singh :202 :BCA

Signature:

Course

Semester :5th

INDEX Sr.No. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. To print Hello Construction of Command line arguments Creation of Pyramid using Labeled Loops(Break/Continue) Illustration of increment and decrement operator Usage of Instance variables Working of Type Casting Operators Illustration of implementation of Wrapper Classes Program to find the factorial of a number Method Overloading Constructor Overloading Method Overriding Inheritance Program to how Packages are created and imported Abstract Classes and Abstract Methods Program to implement an Interfaces Exception Handling Using Try Catch Exception Handling Using Throw, Throws Multithreading - Implementing Runnable Interface Synchronization in Multithreading Using Suspend(), Resume(), isAlive() Program Remarks

1. To print Hello.
Class HELLO { public static void main(String[] args) { System.out.print("hello APP"); System.out.println("helloWORLD APP"); } }

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<count) { string = args[i]; i=i+1; System.out.println(i+ ": " + "java is " + string + " ! "); } } }

3. Creation of Pyramid using Labeled Loops (Break/Continue).


classcontinuebreak { public static void main(String args[]) { loop1: for(inti=1;i<100;i++) { System.out.println(" "); if(i>=10) break; for(int j=1;j<100;j++) { System.out.println("*"); if(j==i) continue loop1; } } System.out.println("Termination by break"); } }

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

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{}

6. Working of Type Casting Operators.


class Casting { public static void main(String args[]) { float sum; inti; sum=0.0f; for(i=1;i<=10;i++) { sum=sum+1/(float)i; System.out.println("i=" +i); System.out.println("Sum=" +sum); } } }

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

classexpresswrap { public static void main(String args[]) { //declaration and initialization int a=10,b=5,c=8,d=2; float x=6.4f,y=3.0f; // order of evaluation int answer1=a*b+c/d; int answer2=a*(b+c)/d; //type conversion float answer3=a/c; float answer4=(float)a/c; float answer5 =a/y; //module operation int answer6=a%c; float answer7=x%y; //logic operation

boolean bool1=a>b&&c>d; boolean bool2=a<b&&c<d; boolean bool3=a<b||c>d; boolean bool4 =!(a-b==c); System.out.println("\n order of evaluation"); System.out.println("a*b+c/d = "+answer1); System.out.println("a*(b+c)/d = "+answer2); System.out.println("\n type conversion"); System.out.println("a/c = "+answer3); System.out.println("(float)a/c = "+answer4); System.out.println("a/y = "+answer5); System.out.println("\n module operation"); System.out.println("a%c = "+answer6); System.out.println("x%y = "+answer7); System.out.println("\n logic operation"); System.out.println("a>b&&c>d = "+bool1); System.out.println("a<b&&c<d = "+bool2); System.out.println("a<b||c>d = "+bool3); System.out.println("!(a-b==c = "+bool4); } }

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

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

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

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 } }

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)

{ 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(); } }

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;

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

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<=n;i++) s=s*i; return s; } publicint square(int n) { return n*n; } }

Code one level above (directory mypack). importmypack.mathmetics; class J28 { public static void main(String a[]) { inti; mathmeticsobj=new mathmetics(); i=obj.fact(5); System.out.println("Factorial of 5 is:"+i); i=obj.square(12); System.out.println("Square of 12 is:"+i); } }

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[]) {

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

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

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."); } }

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

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

catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); } }

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[])

{ 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"); } } }

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 {

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."); } }

You might also like