Java File
Java File
Java File
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"); } }
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); } }
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); } }
{ 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(); } }
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); } }
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); } }
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()); } }
{ 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"); } } }
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."); } }