Вы находитесь на странице: 1из 9

SIMPLE JAVA PROGRAM

class sample { public static void main(String S[]) { System.out.println("JAVA LAB"); } }

OUTPUT:
JAVA LAB

SIMPLE JAVA PROGRAM WITH TWO CLASSES


class a { int l,m,n; int tot() { return l+m+n; } } class bb { public static void main(String args[]) { int ans; a obj=new a();//creates an object obj obj.l=60;// assigns a value to variable l obj.m=70;// assigns a value to varaible m obj.n=50;// assigns a value to variable n ans=obj.tot(); System.out.println("The total is:"+ans); } }

OUTPUT:
The total is:180

JAVA PROGRAM USING INHERITANCE


class room { int length; int breadth; room(int x,int y) { length=x; breadth=y; } int area() { return (length*breadth); } } class bedroom extends room { int height; bedroom(int x,int y,int z) { super(x,y); height=z; } int volume() { return(length*breadth*height); } } // pass values to superclass // Inheriting room

class inhertest { public static void main(String args[]) { bedroom room1=new bedroom(14,12,10); int area1=room1.area; int volume1=room1.volume(); System.out.println(Area=+area1); System.out.println(Volume=+volume1); } } // superclass method // baseclass method

OUTPUT:

Area1=168 Volume1=1680

JAVA PROGRAM USING POLYMORPHISM

class calc { public int multiply(int a,int b) { System.out.println("Values of a and b are:"+a+" & "+b); System.out.println("multiplying two integer values:"); return a*b; } public double multiply(double a,double b) //performing same operation usin diff inputs(polymorphism) { System.out.println("Values of a and b are:"+a+" & "+b); System.out.println("multiplying two double values:"); return a*b; } } class poly { public static void main(String S[]) { calc c=new calc();//creating object System.out.println(c.multiply(5,10)); System.out.println(c.multiply(1000.6,1000.5)); } }

OUTPUT:
Values of a and b are:5 & 10 multiplying two integer values: 50 Values of a and b are:1000.6 & 1000.5 multiplying two double values: 1001100.3

PROGRAM-1: JAVA PROGRAM USING STATIC METHOD


class staticdemo { static int a=50; static int b=49; static void square() { System.out.println("Square value is:"+(a*a)); System.out.println("square value is:"+(b*b)); } } class staticmeth { public static void main(String S[]) { System.out.println("Understanding the usage of static methods"); staticdemo.square(); } } //calling static method System.out.println("end of the program"); // static variable // static variavle // defining static method

OUTPUT:
Understanding the usage of static methods Square value is:2500 square value is:2401 End of the program

PROGRAM-2: JAVA PROGRAM FOR STATIC VARIABLE

class emp { static int id=1; int a,b; static void id() { System.out.println("Object numer:"+id); id++; } emp() { System.out.println("ID of objects:"); } int add(int x,int y) { x=a; y=b; return x+y; } } class s { public static void main(String S[]) { int v,w; emp obj1=new emp(); obj1.a=10; //object creation //constructor // static method // static variable

obj1.b=200; emp.id(); v=obj1.add(100,200); System.out.println("The sum is:"+v); emp obj2=new emp(); obj2.a=40; obj2.b=40; emp.id(); w=obj2.add(30,40); System.out.println("The sum is:"+w); } }

OUTPUT:
ID of objects: Object numer:1 The sum is:210 ID of objects: Object numer:2 The sum is:80

Вам также может понравиться