ajava file

28
EXPERIMENT-1 Program to print hello world class hello{ public static void main(String arg[]){ System.out.println("HELLO WOR LD!!!"); } } OUTPUT

Upload: shivani-aggarwal

Post on 27-May-2017

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ajava file

EXPERIMENT-1

Program to print hello world

class hello{

public static void main(String arg[]){

System.out.println("HELLO WOR LD!!!");

}

}

OUTPUT

Page 2: ajava file

EXPERIMENT-2

Program to find even or odd

import java.util.Scanner;

class iftest{

public static void main(String arg[])

{

int n;

System.out.print("Enter number to be checked");

Scanner in = new Scanner(System.in);

n = in.nextInt();

if(n % 2 == 0 )

System.out.println("Even number");

else

System.out.println("Odd number");

}

}

OUTPUT

Page 3: ajava file

EXPERIMENT-3

Program to print pattern using for loop

class star{

public static void main(String arg[]){

int i,j;

for(i=1; i<=4;i++)

{

for(j=1;j<=i;j++)

{

System.out.print("*");

}

System.out.print("\n");

}

}

}

OUTPUT

Page 4: ajava file

EXPERIMENT-4

Program to print series using while loop

class whiletest{

public static void main(String arg[])

{

int i=10;

while ( i> 0)

{

System.out.println("The value of i is" + i);

i--;

}

}

}

OUTPUT

Page 5: ajava file

EXPERIMENT-5

Program to calculate area

class Area{

int l,b;

void setRectData()

{

l=10;

b=20;

}

void setSquareData()

{

l=10;

b=10;

}

int area()

{

return (l*b);

}

public static void main(String[] args)

{

Area obj1=new Area();

Area obj2=new Area();

obj1.setRectData();

obj2.setSquareData();

System.out.println("Rectangle Data is"+obj1.area());

System.out.println("Square Data is"+obj2.area());

}}

Page 6: ajava file

OUTPUT

Page 7: ajava file

EXPERIMENT-6

Program to show type casting.

Class typecon{public static void main(String args[]){int a=10;byte b;b=(byte)a;System.out.println(b);}}

OUTPUT

Page 8: ajava file

EXPERIMENT-7

Program to search a number in array

import java.util.Scanner;

class Search

{

public static void main(String[] args)

{

int a[]={1,2,3,4,5};

int se;

int flag;

System.out.println("Array is");

for(int j=0;j<=4;j++)

{

System.out.println(a[j]);

}

System.out.println("Enter the element to be searched");

Scanner s=new Scanner(System.in);

se= s.nextInt();

for(int i=0;i<=4;i++)

{

if(se==a[i])

{System.out.println("Found");

break;}

else

{

System.out.println("Not Found");

Page 9: ajava file

break;}

}

}

}

OUTPUT

EXPERIMENT-8

Page 10: ajava file

Program in java using constructor

class rectsquare

{ int length,breadth;

rectsquare()

{ length=10;

breadth=10; }

rectsquare(int l, int b)

{ length=l;

breadth=b; }

void area()

{ System.out.println(length*breadth); }}

class dim

{

public static void main(String args[])

{

rectsquare obj1=new rectsquare();

obj1.area();

rectsquare obj2=new rectsquare(10,20);

obj2.area();

}

}

OUTPUT

Page 11: ajava file

EXPERIMENT-9

Page 12: ajava file

Program in java to pass object through function

class pass

{

int a,b;

pass(int x,int y)

{ a=x;

b=y; }

void check (pass obj)

{ if(a==obj.a && b==obj.b)

{ System.out.println("object equal"); }

else

{ System.out.println("not equal"); } } }

class chkobj

{ public static void main(String args[])

{

pass obj1=new pass(10,2);

pass obj2=new pass (20,30);

obj2.check(obj1);

obj1.check(obj2);

} }

OUTPUT

Page 13: ajava file

EXPERIMENT-10

Program in java using THIS keyword

Page 14: ajava file

class rectsquare

{

int length,breadth;

rectsquare()

{ length=10;

breadth=10; }

rectsquare(int length, int breadth)

{ this.length=length;

this.breadth=breadth; }

void area()

{ System.out.println(length*breadth); } }

class demo

{ public static void main(String args[])

{ rectsquare obj1=new rectsquare();

obj1.area();

rectsquare obj2=new rectsquare(10,20);

obj2.area();

}}

OUTPUT

Page 15: ajava file

EXPERIMENT-11

Program in java using FINAL keyword

Page 16: ajava file

class demo{int a,b;final void sum(int x,int y)

{ a=x;b=y;System.out.println(a+b);} }class xyz extends demo{ void sum(int a,int b){ System.out.println(a+b);} }class abc{ public static void main(String args[]){ xyz obj=new xyz();obj.sum(10,20); } }

OUTPUT

EXPERIMENT-12

Program in java to show inner outer class

Page 17: ajava file

class Outer {

int outer_x = 100;

void test() {

Inner inner = new Inner();

inner.display();

}

class Inner {

void display() {

System.out.println("display: outer_x = " + outer_x);}}

}

class InnerC {

public static void main(String args[]) {

Outer outer = new Outer();

outer.test();}}

OUTPUT

EXPERIMENT-13

Program in java to show inheritance

class A {

Page 18: ajava file

int i, j;

void showij() {

System.out.println("i and j: " + i + " " + j);

}

}

class B extends A {

int k;

void showk() {

System.out.println("k: " + k);

}

void sum() {

System.out.println("i+j+k: " + (i+j+k));

}

}

class SimpleInheritance {

public static void main(String args[]) {

A superOb = new A();

B subOb = new B();

superOb.i = 10;

superOb.j = 20;

System.out.println("Contents of superOb: ");

superOb.showij();

System.out.println();

subOb.i = 7;

subOb.j = 8;

Page 19: ajava file

subOb.k = 9;

System.out.println("Contents of subOb: ");

subOb.showij();

subOb.showk();

System.out.println();

System.out.println("Sum of i, j and k in subOb:");

subOb.sum();

}

}

EXPERIMENT-14

Program in java to show interfaces

public class Interfaces implements Animal{

Page 20: ajava file

public void eat(){

System.out.println("Mammal eats");

}

public void travel(){

System.out.println("Mammal travels");

}

public int noOfLegs(){

return 0;

}

public static void main(String args[]){

Interfaces m = new Interfaces();

m.eat();

m.travel();

}

}

OUTPUT

Page 21: ajava file
Page 22: ajava file

EXPERIMENT-15

Program in java to show Threads

public class Threads implements Runnable

{

public static void main (String[] args)

{

System.out.println("This is currently running on the main thread, " +

"the id is: " + Thread.currentThread().getId());

Threads worker = new Threads();

Thread thread = new Thread(worker);

thread.start();

}

@Override

public void run()

{

System.out.println("This is currently running on a separate thread, " +

"the id is: " + Thread.currentThread().getId());

}

}

OUTPUT

Page 23: ajava file

EXPERIMENT-16

Page 24: ajava file

Program in java to show Exception

import java.io.*;

public class Exception{

public static void main(String args[]){

try{

int a[] = new int[2];

System.out.println("Access element three :" + a[3]);

}catch(ArrayIndexOutOfBoundsException e){

System.out.println("Exception thrown :" + e);

}

System.out.println("Out of the block");

}

}

OUTPUT