practical 1 date - krunalbhardwaj.files.wordpress.com€¦ · venus international college of...
TRANSCRIPT
Venus International College Of Technology | Object Oriented Programming With java 1
Practical – 1 Date :
Statement: Write a program to find a factorial of given number by user.
Program:
class Pract1 { public static void main(String s[]) { int n,a1; n=Integer.parseInt(s[0]); A a = new A(); a1 = a.show(n); System.out.println("Factorial of "+n+" is = "+a1); } } class A { public int show(int n) { int fact = 1; if ( n < 0 ) { System.out.println("Number should be non-negative."); return 0; } else if(n==1) return 1; else { fact = n*show(n-1); } return fact; } }
Venus International College Of Technology | Object Oriented Programming With java 2
Output:
Venus International College Of Technology | Object Oriented Programming With java 3
Practical – 2 Date :
Statement: Write a program to perform Addition, Subtraction, Multiplication
and Division.
Program:
class Pract2 { public static void main(String s[]) { int a,b,a1,a2; float a3,a4; a=Integer.parseInt(s[0]); b=Integer.parseInt(s[1]); A ob = new A(); a1 = ob.add(a,b); a2 = ob.sub(a,b); a3 = ob.mul(a,b); a4 = ob.div(a,b); } } class A { public int add(int a, int b) { int ad=a+b; System.out.println("Addition of "+a+" and "+b+" is : "+ad); return ad; } public int sub(int a, int b) { int su=a-b; System.out.println("Subtraction of "+a+" and "+b+" is : "+su); return su; } public float mul(int a, int b) { float mu=a*b; System.out.println("Multiplication of "+a+" and "+b+" is : "+mu); return mu; } public float div(int a, int b) { float di=a/b; System.out.println("Division of "+a+" and "+b+" is : "+di); return di; } }
Venus International College Of Technology | Object Oriented Programming With java 4
Output:
Venus International College Of Technology | Object Oriented Programming With java 5
Practical – 3 Date :
Statement: Write a program to perform Method Overloading program.
Program:
class Pract3 { public static void main(String s[]) { int n; double a,b,a1,a2; A ob = new A(); System.out.println("Choose the below option to find AREA & Enter Value"); System.out.println("--------------------------------------------------------------------"); System.out.println("1. Squre "); System.out.println("2. Circle "); n=Integer.parseInt(s[0]); switch(n) { case 1: { a=Integer.parseInt(s[1]); a1 = ob.area(a); break; } case 2: { a=Integer.parseInt(s[1]); a2 = ob.area(a,3.14); break; } default : { System.out.println(" --------- Wrong Choise ---------- "); break; } } } }
Venus International College Of Technology | Object Oriented Programming With java 6
class A { public double area(double a) { double sa=a*a; System.out.println("Area of Squre is : "+sa); return sa; } public double area(double a, double b) { double ca=b*a*a; System.out.println("Area of Circle is : "+ca); return ca; } }
Venus International College Of Technology | Object Oriented Programming With java 7
Output:
Venus International College Of Technology | Object Oriented Programming With java 8
Practical – 4 Date :
Statement: Write a program to find area of shape using Inheritance.
Program:
class c1 { public double circle(double r, double p) { double ca=p*r*r; System.out.println("Area of Circle are :: "+ca); return ca; } } class c2 extends c1 { double square(double r) { double sa=r*r; System.out.println("Area of Square are :: "+sa); return sa; } } class Pract4 { public static void main(String s[]) { int n; double a, a1, a2; c2 ob = new c2(); System.out.println("Choose the below option for find Area"); System.out.println("------------------------------------------------"); System.out.println("1. Circle"); System.out.println("2. Square"); n=Integer.parseInt(s[0]); switch(n) { case 1: { a=Integer.parseInt(s[1]); a1=ob.circle(a,3.14); break; }
Venus International College Of Technology | Object Oriented Programming With java 9
case 2: { a=Integer.parseInt(s[1]); a2=ob.square(a); break; } default : { System.out.println("----- Wrong Choice -----"); break; } } } }
Venus International College Of Technology | Object Oriented Programming With java 10
Output:
Venus International College Of Technology | Object Oriented Programming With java 11
Practical – 5 Date :
Statement: Write a program to find area of shape using Super Constructor.
Program:
class c1 { c1(double r) { double sa=r*r; System.out.println("Area of Square are :: "+sa); } } class c2 extends c1 { c2(double r, double p) { super(r); double ca=p*r*r; System.out.println("Area of Circle are :: "+ca); } } class SuperCons { public static void main(String s[]) { double a=Double.parseDouble(s[0]); c2 ob =new c2(a,3.14); } }
Venus International College Of Technology | Object Oriented Programming With java 12
Output:
Venus International College Of Technology | Object Oriented Programming With java 13
Practical – 6 Date :
Statement: Write a program to find area of shape using Interface.
Program:
interface c1 { void circle(double r, double p); void square(double r); } class i1 implements c1 { public void circle(double r, double p) { double ca=p*r*r; System.out.println("Area of Circle are :: "+ca); } public void square(double r) { double sa=r*r; System.out.println("Area of Square are :: "+sa); } } class Pract6 { public static void main(String s[]) { i1 ob = new i1(); ob.circle(2,4); ob.square(5); } }
Venus International College Of Technology | Object Oriented Programming With java 14
Output:
Venus International College Of Technology | Object Oriented Programming With java 15
Practical – 7 Date :
Statement: Write a program to find area of shape using Package.
Program:
package area; class c1 { public double circle(double r, double p) { double ca=p*r*r; System.out.println("Area of Circle are :: "+ca); return ca; } public double square(double r) { double sa=r*r; System.out.println("Area of Square are :: "+sa); return sa; } } public class Pract7 { public static void main(String s[]) { c1 ob = new c1(); ob.circle(2,4); ob.square(5); } }
Venus International College Of Technology | Object Oriented Programming With java 16
Output:
Venus International College Of Technology | Object Oriented Programming With java 17
Practical – 8 Date :
Statement: Write a program to handling error divide by 0 using Exception
Handling.
Program:
class Pract8 { public static void main(String a[]) { try { for(int i=5;i>=0;i--) { System.out.println(10/i); } } catch(Exception ex) { System.out.println("Exception Message: "+ex.getMessage()); ex.printStackTrace(); } System.out.println("After for loop..."); } }
Venus International College Of Technology | Object Oriented Programming With java 18
Output:
Venus International College Of Technology | Object Oriented Programming With java 19
Practical – 9 Date :
Statement: Write a program to print Hello Java using Applet.
Program:
import java.awt.*; import java.applet.*; public class Pract9 extends Applet { public void paint(Graphics g) { Font font = new Font("Lucida Calligraphy",Font.BOLD,30); g.setFont(font); FontMetrics fm = g.getFontMetrics(); g.setColor(Color.BLACK); int y = fm.getHeight(); g.drawString("Hello Java",10,y); } }
HTML CODE:
<HTML> <HEAD> </HEAD> <BODY> <div > <APPLET CODE="Pract9.class" WIDTH="300" HEIGHT="200"> </APPLET> </div> </BODY> </HTML>
Venus International College Of Technology | Object Oriented Programming With java 20
Output:
Venus International College Of Technology | Object Oriented Programming With java 21
Practical – 10 Date :
Statement: Write a program to Show Thread.
Program:
class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} } System.out.println("DONE! " + getName()); } } class Pract10 { public static void main (String args[]) { new SimpleThread("Hello").start(); new SimpleThread("Java").start(); } }
Venus International College Of Technology | Object Oriented Programming With java 22
Output: