Programs Using Class and Objects
|
class Test { void display() { System.out.print("\"Hello World!\""); } public static void main(String[] a) { Test t = new Test(); t.display(); } }OUTPUT :
"Hello World!"
import java.util.Scanner; class Employee { int empID,deptID,salary; String empName,bloodGroup; void setEmployeeDetail() { Scanner s=new Scanner(System.in); System.out.print("Enter empId: "); empID=Integer.parseInt(s.nextLine()); System.out.print("Enter Name: "); empName = s.nextLine(); System.out.print("Enter Dept. Id: "); deptID=Integer.parseInt(s.nextLine()); System.out.print("Enter Blood Group: "); bloodGroup= s.nextLine(); System.out.print("Enter Salary: "); salary=Integer.parseInt(s.nextLine()); } void printEmployeeDetails() { System.out.println("EMP. ID - "+empID+ "\nEMP NAME - "+empName+ "\nDEPT ID - "+deptID+ "\nBLOOD GROUP - "+bloodGroup+ "\nSALARY - "+salary); } public static void main(String[] a) { Employee e = new Employee(); e.setEmployeeDetail(); e.printEmployeeDetails(); } }OUTPUT :
Enter empId: 12 Enter Name: Alok Enter Dept. Id: 5 Enter Blood Group: A Enter Salary: 30000 EMP. ID - 12 EMP NAME - Alok DEPT ID - 5 BLOOD GROUP - A SALARY - 30000
|
class Temp { float convert(int c) { float f = (float)9.0/5*c; return f+32; } public static void main(String[] a) { Temp t = new Temp(); System.out.println("Celc.\tFahren."); for(int i=1; i<=10; i++) { float x = t.convert(i); System.out.println(i+"\t"+x); } } }OUTPUT :
Celc. Fahren. 1 33.8 2 35.6 3 37.4 4 39.2 5 41.0 6 42.8 7 44.6 8 46.4 9 48.199997 10 50.0
|
import java.util.Scanner; class Area { final double PI = 3.14; void areaOfCircle(int r) { double ar = PI*r*r; System.out.println("Circle Area= "+ar); } void areaOfCylinder(int r, int h) { double ar = 2*PI*r*h; System.out.print("Cylinder Area= "+ar); } public static void main(String[] a) { Area ar = new Area(); Scanner s=new Scanner(System.in); System.out.print("Enter Circle Radius: "); int r = s.nextInt(); System.out.print("Enter Cylinder Radius: "); int r2 = s.nextInt(); System.out.print("Enter Cylinder Height: "); int h = s.nextInt(); ar.areaOfCircle(r); ar.areaOfCylinder(r2,h); } }OUTPUT :
Enter Circle Radius: 7 Enter Cylinder Radius: 7 Enter Cylinder Height: 10 Circle Area= 153.86 Cylinder Area= 439.6
|
import java.util.Scanner; class Test { public int factorial(int x) { int f = 1; for(int i = 1; i<=x; i++) { f = f*i; } return f; } } class Demo { public static void main(String[] a) { Test t = new Test(); Scanner s=new Scanner(System.in); System.out.print("Enter a Num: "); int x = s.nextInt(); int fact = t.factorial(x); System.out.print("Result = "+fact); } }OUTPUT :
Enter a Num: 6 Result = 720
|
import java.util.Scanner; class Test { public static float fact(int x) { int f = 1; for(int i = 1; i<=x; i++) { f = f*i; } return f; } public static void main(String[] a) { Scanner s=new Scanner(System.in); System.out.print("Enter N: "); int n = s.nextInt(); System.out.print("Enter R: "); int r = s.nextInt(); float res=fact(n)/(fact(n-r)*fact(r)); System.out.print("Result = "+res); } }OUTPUT :
Enter N: 5 Enter R: 3 Result = 10.0
|
class Rectangle { int l, b; Rectangle() { l = 5; b = 3; } Rectangle(int l, int b) { this.l = l; this.b = b; } void area() { System.out.println("Area- "+(l*b)); } } class Main { public static void main(String[] a) { Rectangle r =new Rectangle(); r.area(); Rectangle r2 = new Rectangle(20,10); r2.area(); } }OUTPUT :
Area- 15 Area- 200
import java.util.Scanner; class Test { int x; Test(int num){ x = num ; } int getReverse() { int temp, rev =0; while(x != 0) { temp = x%10; rev = rev *10+temp; x = x/10; } return rev; } public static void main(String[] a) { Scanner s=new Scanner(System.in); System.out.print("Enter Num: "); int x = s.nextInt(); Test t = new Test(x); int res = t.getReverse(); System.out.print("Reverse = "+res); } }OUTPUT :
Enter Num: 15465 Reverse = 56451
|
class Demo { int a; Demo(int a) { this.a = a; } } class Test { int x = 20; void square(Demo d) { d.a = d.a*d.a; } } class Main{ public static void main(String[] a) { Demo d = new Demo(10); Test t = new Test(); System.out.println("Before Call- "+d.a); t.square(d); System.out.print("After Call- "+d.a); } }OUTPUT :
Before Call- 10 After Call- 100
|
class Test { int a = 5; static int b = 5; void display() { a++; b++; System.out.println("A = "+a); System.out.println("B = "+b); } public static void main(String[] a) { Test t = new Test(); t.display(); Test t2 = new Test(); t2.display(); Test t3 = new Test(); t3.display(); } }OUTPUT :
A = 6 B = 6 A = 6 B = 7 A = 6 B = 8
|
class Counter { static int count = 0; void counter(){ count++; } void display() { System.out.println("Total time- "+count); } public static void main(String[] a) { Counter c = new Counter(); c.counter(); c.counter(); Counter c2 = new Counter(); c2.counter(); c2.counter(); c2.display(); } }OUTPUT :
Total time-4