67
1 Practical:-1 AIM:-Create Simple java program to demonstrate how to run java program using CMD. class Test { publicstaticvoid main(String[] args) { System.out.println ("Built my site"); } } Output:-

Practical:-1 - Amazon S3 · Practical:-1 AIM:-Create Simple java program to demonstrate how to run java program using CMD. ... Write a program in Java to demonstrate use of this keyword

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

1

Practical:-1

AIM:-Create Simple java program to demonstrate how to run java

program using CMD.

class Test { publicstaticvoid main(String[] args) { System.out.println ("Built my site"); } }

Output:-

2

Practical:- 2

AIM: Write a program in Java to generate first n prime numbers.

class Prime { publicstaticvoid main(String[] args) { int n=50,flag=0; System.out.println ("1 "); System.out.println("2 "); for(int i=3;i<n+1;i++) { for(int j=2;j<i;j++) { if(i%j==0) { flag=1; } } if(flag==0) { System.out.println(i+" "); } flag=0; } } }

Output:-

3

Practical:-3

AIM:-Write a program in Java to find maximum of three numbers

using conditional operator.

class Maximum { publicstaticvoid main(String[] args) { int a=30,b=20,c=10; if(a>=b) { if(a>=c) { System.out.println("The maximum no.:"+a); } elseif(b>=c) { System.out.println("The maximum no.:"+b); } else { System.out.println("The maximum no.:"+c); } } } }

Output:

4

Practical:-4

AIM:Write a program in Java to find second maximum of n numbers

without using arrays.

class Smax { publicstaticvoid main(String[] args) { int max=Integer.parseInt(args[0]); int smax=Integer.parseInt(args[0]); int temp=0; int n=args.length; for(inti=0;i<n;i++) { temp=Integer.parseInt(args[i]); if(temp>=max) { if(temp>=smax) { smax=max; max=temp; } else { smax=temp; } } } System.out.println("The second max no. is:"+smax); } }

Output:-

5

6

Practical:-5

AIM: Write a program in Java to reverse the digits of a number using

while loop.

import java.util.Scanner; class Reverse { publicstaticvoid main(String[] args) { Scanner ob=new Scanner(System.in); int a,flag; System.out.println("Enter any Value:"); a=ob.nextInt(); while(a!=0) { flag=a%10; a=a/10; System.out.println(flag); } } }

Output:-

7

Practical: 6

AIM: Write a program in Java to convert number into words & print

it.

import java.util.Scanner;

class nointoword

{

public static void num(int a)

{

switch(a)

{

case 0:

{

System.out.println("Zero");

break;

}

case 1:

{

System.out.println("One");

break;

}

case 2:

{

System.out.println("Two");

break;

}

8

case 3:

{

System.out.println("Three");

break;

}

case 4:

{

System.out.println("Four");

break;

}

case 5:

{

System.out.println("Five");

break;

}

case 6:

{

System.out.println("Six");

break;

}

case 7:

{

System.out.println("Seven");

break;

9

}

case 8:

{

System.out.println("Eigtht");

break;

}

case 9:

{

System.out.println("Nine");

break;

}

}

}

public static void main(String[] args)

{

Scanner ob=new Scanner(System.in);

int m=0,a,flag;

System.out.println("Enter any Value:");

a=ob.nextInt();

while(a!=0)

{

flag=a%10;

a=a/10;

m=m*10+flag;

10

}

a=0;

while(m!=0)

{

flag=m%10;

m=m/10;

num(flag);

}

}

}

Output:-

11

Practical:-7

AIM:-WAP to demonstrate the use of byte wrapper classes.

class ByteClass

{

public static void main(String[] args)

{

Byte b;

b = new Byte("100");

byte bt;

bt = b.byteValue();

String str = "Primitive byte value of Byte object " + b + " is " + bt;

System.out.println( str );

}

}

Output:-

12

Practical:-8

AIM: WAP to demonstrate the use of short wrapper classes.

public class ShortClass

{

public static void main(String[] args) {

short sval = 50;

Short sobj = new Short(sval);

short shortValue = sobj.shortValue();

System.out.println("short value of given Short is = " + shortValue);

}

}

Output:-

13

Practical:- 9

AIM: WAP to demonstrate the use of Integer wrapper classes.

class IntegerClass

{

public static void main(String[] args)

{

Integer obj = new Integer(15);

int i = obj.intValue();

System.out.println("Value of i = " + i);

}

}

Output:

14

Practical: -10

AIM: WAP to demonstrate the use of long wrapper classes.

public class LongClass

{

public static void main(String[] args)

{

Long obj = new Long(77906);

long l = obj.longValue();

System.out.println("Value of l = " + l);

}

}

Output:

15

Practical: -11

AIM: WAP to demonstrate the use of Float wrapper classes.

public class FloatClass

{

public static void main(String[] args)

{

Float f = new Float("4.5");

System.out.println(f);

System.out.println("Value = " + f.valueOf(2.75f));

}

}

Output:

16

Practical: -12

AIM: WAP to demonstrate the use of Double wrapper classes.

public class DoubleClass

{

public static void main(String[] args)

{

Double d = new Double("6.5");

String str = "55";

System.out.println(d);

represented by str */

System.out.println("Value = " + d.valueOf(str));

}

}

Output:

17

Practical:-13

AIM:-WAP to demonstrate the use of character wrapper classes.

public class CharacterClass

{

public static void main(String[] args)

{

Character c1, c2;

char ch1 = 'i';

char ch2 = 65;

c1 = Character.valueOf(ch1);

c2 = Character.valueOf(ch2);

String str1 = "Character value of " + ch1 + " is " + c1;

String str2 = "Character value of " + ch2 + " is " + c2;

System.out.println( str1 );

System.out.println( str2 );

}

}

Output:

18

Practical:- 14

AIM: WAP to demonstrate the use of boolean wrapper classes.

public class BooleanClass

{

public static void main(String[] args)

{

Boolean b1, b2;

String s1 = null;

String s2 = "false";

static method is called using class name

assign result of valueOf method on s1, s2 to b1, b2

b1 = Boolean.valueOf(s1);

b2 = Boolean.valueOf(s2);

String str1 = "Boolean instance of string " + s1 + " is " + b1;

String str2 = "Boolean instance of string " + s2 + " is " + b2;

System.out.println( str1 );

System.out.println( str2 );

}

}

Output:

19

Practical:- 15

AIM: Write a program to multiply two 3x3 matrices.

import java.util.Scanner;

publicclass Matrix

{

publicstaticvoid main(String[] args)

{

Scanner ob1=new Scanner(System.in);

int a[][]=newint[3][3];

int b[][]=newint[3][3];

int Multiply[][]=newint[3][3];

int i,j,k,sum=0;

System.out.println("Enter a values of First matrix.");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

a[i][j]=ob1.nextInt();

}

}

System.out.println("Enter a values of Second matrix.");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

b[i][j]=ob1.nextInt();

}

}

for (i=0;i<3;i++)

{

for (j=0;j<3;j++)

{

for (k=0;k<3;k++)

{

sum=sum+a[i][k]*b[k][j];

}

Multiply[i][j]=sum;

sum=0;

}

}

System.out.println("After multiply the Matrix is:");

for(i=0;i<3;i++)

{

20

for (j=0;j<3;j++ )

{

System.out.println(Multiply[i][j]+"\t");

}

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

}

}

Output:-

21

Practical:- 16

AIM: Write a static block which will be executed before main( )

method in a class.

public class StaticBlock

{

static int a=5;

static int b;

static int c;

static void method(int x)

{

System.out.println("x="+x);

System.out.println("a="+a);

System.out.println("b="+b);

System.out.println("c="+c);

}

static

{

System.out.println("first block");

b=a+4;

System.out.println("b="+b);

}

static

{

System.out.println("second block");

b=b*2;

}

public static void main(String[] args)

{

method(5);

}

}

22

Output:-

23

Practical:-17

AIM: Write a program in Java to demonstrate use of this keyword.

Whether this can access the private members of the class or not.

public class point

{

int x,y;

void init (int x,int y)

{

this.x=x;

this.y=y;

}

void disp()

{

System.out.println("x="+x);

System.out.println("y="+y);

}

}

class ans

{

public static void main(String args[])

{

point p=new point();

p.init(5,4);

p.disp();

}

24

}

Output:

25

Practical:-18

AIM:-Write a program in Java to develop overloaded constructor.

Also develop the copy constructor to create a new object with the state

of the existing object.

class salary

{

double basic;

double da;

double hra;

salary(double b,double d,double h)

{

basic=b;

da=basic*d;

hra=basic*h;

}

salary()

{

basic=0;

da=0;

hra=0;

}

salary(double b)

{

basic=b;

da=0;

26

hra=0;

}

double computesalary()

{

return basic+da+hra;

}

}

class basic

{

public static void main(String args[])

{

salary e1=new salary(5000,0.1,0.2);

salary e2=new salary();

salary e3=e1;

double empsalary;

empsalary=e1.computesalary();

System.out.println("Net salary of e1 is:"+empsalary);

empsalary=e2.computesalary();

System.out.println("Net salary of e2 is:"+empsalary);

empsalary=e3.computesalary();

System.out.println("Net salary of e3 is:"+empsalary);

}

}

27

Output:-

28

Practical:-19

AIM: Write a program in Java to demonstrate the use of private

constructor and also write a method which will count the number of

instances created using default constructor only.

import java.io.*;

class abc

{

static int count;

abc()

{

count++;

System.out.println("objects"+count);

}

}

class pb

{

public static void main(String args[])

{

abc a1=new abc();

abc a2=new abc();

}

}

Output:-

29

30

Practical:-20

AIM:-Write a program in Java to demonstrate the use of 'final'

keyword in the field declaration. How it is accessed using the objects.

class salary

{

int basic,da;

salary(int b,int d)

{

basic=b;

da=basic*d/100;

}

final void computesalary()

{

System.out.println("net salary of superclass netsal is"+(basic+da));

}

}

class grossalary extends salary

{

int hra;

grossalary(int b,int d,int h)

{

super(b,d);

hra=basic*h/100;

}

31

void computegrossalary()

{

System.out.println("Gross salary from subclass GrossSalary is"+(basic+da+hra));

}

}

class basic

{

public static void main(String args[])

{

grossalary e1=new grossalary(5000,10,20);

e1.computesalary();

}

}

Output:-

32

Practical:-21

AIM:- WAP to pass parameter in method using pass by value.

class operation

{

int data=12;

void change(int data)

{

data=data+24;

}

public static void main(String args[])

{

operation op=new operation();

System.out.println("before change "+op.data);

op.change(288);

System.out.println("after change"+op.data);

}

}

Output:-

33

Practical:-22

AIM:- WAP to pass parameter in method using pass by reference.

class Operation1

{

int data= 50;

void change(Operation1 op)

{

op.data=op.data+100;

}

public static void main(String args[])

{

Operation1 op = new Operation1();

System.out.println("before change"+op.data);

op.change(op);

System.out.println("after change"+op.data);

}

}

Output:-

34

Practical:-23

AIM:- WAP to Return values from methods of different class to

different class.

35

Practical:-24

AIM:-WAP to demonstrate inheritance in java.

class a

{

int x=24;

void display()

{

System.out.println("x is:"+x);

}

}

class b extends a

{

int x=12;

void display()

{

System.out.println("super class x is:"+super.x);

System.out.println("sub x is:"+x);

}

}

class d

{

public static void main(String args[])

{

b B=new b();

36

B.display();

}

}

Output:-

37

Practical:-25

AIM:- WAP to demonstrate use of super keyword.

class A

{

static int x;

}

public class B extends A

{

int x;

public void display()

{

System.out.println("value of x in A:" +super.x);

System.out.println("value of x in B:" +x);

}

public static void main(String args[])

{

A aa=new A();

aa.x=512;

B bb=new B();

bb.x=2491;

bb.display();

}

}

Output:-

38

Practical:-26

AIM:-WAP to demonstrate multiple/hierarchical Inheritance in java.

class a

{

void methodA()

{

System.out.println("class a method a");

}

}

class b extends a

{

void methodA()

{

System.out.println("child class b is overloading inherrited method A");

}

void methodB()

{

System.out.println("class b method b");

}

}

class c extends a

{

void methodA()

{

39

System.out.println("child class c is overloading inherrited method A");

}

void methodC()

{

System.out.println("class c method c");

}

}

class d extends c

{

void methodD()

{

System.out.println("class d method d");

}

public static void main(String args[])

{

d obj1=new d();

obj1.methodD();

obj1.methodA();

}

}

40

Output:-

41

Practical:-27

AIM:-WAP to demonstrate multi-level inheritance in java.

class x

{

public void methodx()

{

System.out.println("class X method");

}

}

class y extends x

{

public void methody()

{

System.out.println("class Y method");

}

}

class z extends y

{

public void methodz()

{

System.out.println("class Z method");

}

public static void main(String args[])

{

z obj=new z();

42

obj.methodx();

obj.methody();

obj.methodz();

}

}

Output:-

Practical:-28

43

AIM:-WAP to demonstrate hybrid inheritance in java.

Practical:-29

AIM:-WAPCreate a class to find out whether the given year is leap

year or not. (Use inheritance for this program)

Practical:-30

AIM: Write an application that illustrates how to access a hidden

variable. Class A declares a static variable x. The class B extends

44

and declares an instance variable x. display( ) method in B displays

both of these variables.

class a

{

static int x=96;

void display()

{

System.out.println("static x is:"+x);

}

}

class b extends a

{

int x=12;

void display()

{

System.out.println("super class static x is:"+super.x);

System.out.println("sub x is:"+x);

}

}

class c

{

public static void main(String args[])

{

b B=new b();

B.display();

45

}

}

Output:-

46

Practical:-31

AIM:-Write a program in Java in which a subclass constructor

invokes the constructor of the super class and instantiate the values.

class a

{

public a(String str)

{

System.out.println("base class constructor="+str);

}

}

class b extends a

{

public b(String str)

{

super(str);

System.out.println("sub class constructor="+str);

}

}

class s

{

public static void main(String args[])

{

b ob1=new b("called");

}

}

Output:-

47

Practical:-32

AIM:-Write an application that illustrates method overriding in the

same package and different packages. Also demonstrate accessibility

rules in inside and outside packages.

48

Practical:-33

AIM:- Describe abstract class called Shape which has three subclasses

say Triangle, Rectangle, Circle. Define one method area()in the

abstract class and override this area() in these three subclasses to

calculate for specific object i.e. area() of Triangle subclass should

calculate area of triangle etc. Same for Rectangle and Circle.

abstract class shape

{

double dim1,dim2,radius;

abstract double area();

}

class triangle extends shape

{

triangle(double d1, double d2)

{

dim1=d1;

dim2=d2;

}

double area()

{

System.out.println("area of triangle:");

return((dim1*dim2)/2);

}

}

class rectangle extends shape

{

rectangle(double d1, double d2)

{

dim1=d1;

dim2=d2;

}

double area()

{

System.out.println("area of rectangle:");

return(dim1*dim2);

}

}

class circle extends shape

{

circle(double d1)

{

49

radius=d1;

}

double area()

{

System.out.println("area of circle:");

return((22*radius*radius)/7);

}

}

class abstractclassdemo

{

public static void main(String args[])

{

triangle t1=new triangle(12,48);

rectangle r1=new rectangle(6,4);

circle c1=new circle(22);

shape s1;

s1=t1;

System.out.println(s1.area());

System.out.println();

s1=r1;

System.out.println(s1.area());

System.out.println();

s1=c1;

System.out.println(s1.area());

}

}

Output:-

50

Practical:-34

AIM:- Write a program in Java to demonstrate use of final class.

class FinalClassDemo

{

protected int a=512;

protected int b=22;

}

final class Subclass extends FinalClassDemo

{

Subclass()

{

System.out.println("the final class");

System.out.println("this class cannot be inherited");

System.out.println("its the final class of inherited classes");

}

void add()

{

int c=a+b;

System.out.println("\n the addition is :"+c);

}

}

class mainclass

{

public static void main(String args[])

{

51

Subclass obj=new Subclass();

obj.add();

}

}

Output:-

52

Practical:-35

AIM:- Write a program in Java to develop user defined exception for

'Divide by Zero' error.

class MyException extends Exception

{

private int ex;

MyException(int b)

{

ex=b;

}

public String toString()

{

return "MyException["+ ex +"] is less than zero";

}

}

class Test

{

static void divide(int a,int b) throws MyException

{

if(b<0)

{

throw new MyException(b);

}

else

{

53

System.out.println(a/b);

}

}

public static void main(String[] args)

{

try

{

divide(-10,0);

}

catch(MyException me)

{

System.out.println(me);

}

}

}

Output:-

54

Practical:-36

AIM:- Write a program in Java to demonstrate multiple try block and

multiple catch exception.

class MultipleTryDemo

{

public static void main(String[] args)

{

try

{

try

{

System.out.println("going to divide");

int b=81/0;

}

catch(ArithmeticException e)

{

System.out.println(e);

}

try

{

int a[]=new int[5];

a[5]=4;

}

catch(ArrayIndexOutOfBoundsException e)

{

55

System.out.println(e);

}

System.out.println("other statements");

}

catch(Exception e)

{

System.out.println("handled");

}

System.out.println("normal flow");

}

}

Output:-

56

2)

class MultipleCatchDemo

{

public static void main(String[] args)

{

int num1=10;

int num2=0;

int result=0;

int a[]=new int[5];

try

{

a[0]=0;

a[1]=1;

a[2]=2;

a[3]=3;

a[4]=4;

a[5]=5;

result=num1/num2;

System.out.println("result of division:"+result);

}

catch(ArithmeticException e)

{

System.out.println("Err: Divided by zero");

}

catch(ArrayIndexOutOfBoundsException e)

57

{

System.out.println("Err: Array out of Bound");

}

}

}

Output:-

58

Practical:-37

AIM:-Write an small application in Java to develop Banking

Application in which user deposits the amount Rs 8000.00 and then

start withdrawing of Rs 512.00, Rs 250.00 and it throws exception

"Not Sufficient Fund" when user withdraws Rs. 500 hereafter.

class BankingDemo

{

float fund;

public void deposit(float amount)

{

fund=amount;

}

public void withdraw(float money) throws Exception

{

float newfund=fund-money;

if(newfund<500)

throw new Exception("Not Sufficient fund");

else

fund=newfund;

System.out.println("Balance after withdraw:"+fund);

}

public static void main(String args[])

{

BankingDemo d=new BankingDemo();

d.deposit(8000);

try

59

{

float money=512;

System.out.println("Withdrawing amount:" +money);

d.withdraw(money);

money=250;

System.out.println("Withdrawing amount :"+money);

d.withdraw(money);

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

}

Output:-

60

Practical:-38

AIM:- Write a program that executes two threads. One thread

displays “Thread1” every 2,000 milliseconds, and the other displays

hread2” every 4,000 milliseconds. Create the threads by extending the

Thread class.

Import java.util.*;

class MyThread extends Thread

{

volatile Calendar cal = Calendar.getInstance();

MyThread(String s)

{

super(s);

start();

}

public void run()

{

For(int i=0;i<5;i++)

{

System.out.println(Thread.currentThread().getName() +cal.getTime());

try

{

if(Thread.currentThread().getName().equals(“Thread1”))

{

Thread.sleep(1200);

}

else

Thread.sleep(4000);

}

catch(Exception e){}

}

}

}

public class MultiThreadDemo

{

public static void main(String args[])

{

System.out.println(“Thread Name:”+Thread.currentThread().getName());

MyThread m1=new MyThread(“Thread1”);

MyThread m2=new MyThread(“Thread2”);

}

61

}

Output:-

62

Practical:-39

AIM:-Write a program that executes two threads. One thread will

print the even numbers and the another thread will print odd umbers

from 1 to 50.

class OddEvenThreads

{

public static void main(String args[])

{

OddEven o1=new OddEven("odd number thread",1);

OddEven o2=new OddEven("even number thread",2);

o1.start();

o2.start();

}

}

class OddEven extends Thread

{

private int number;

public OddEven(String name,int p_number)

{

super(name);

this.number=p_number;

}

public void run()

{

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

{

System.out.println(getName()+ this.number);

if(getName().equals("even number thread") && i%2==0)

{

System.out.println("even thread" +i);

}

else if(getName().equals("odd number thread") && i%2==1)

{

System.out.println("odd thread" +i);

}

}

}

}

Output:-

63

64

Practical:-40

AIM:-Write a program in Java to demonstrate use of synchronization

of threads when multiple threads are trying to update ommon

variable.

class Printing

{

synchronized void printnumber(int n)

{

System.out.println("Start");

for(int j=n;j>0;j--)

{

try

{

if(j==n/2)

Thread.sleep(100);

}

catch(InterruptedException e)

{

;

}

System.out.print(" "+j);

}

System.out.println("End");

}

}

class Threadserve

implements Runnable

{

int n;

Printing pt;

Thread th;

Threadserve(Printing p,int x)

{

n=x;

pt=p;

th=new Thread(this);

th.start();

}

public void run()

{

pt.printnumber(n);

}

65

}

public class SynchronizedThread

{

public static void main(String args[])

{

Printing p=new Printing();

Threadserve ts1=new Threadserve(p,12);

Threadserve ts2=new Threadserve(p,8);

Threadserve ts3=new Threadserve(p,6);

}

}

Output:-

66

Practical:-41

AIM:- Write a program in Java to create, write, modify, read

operations on a Text file.

import java.io.*;

public class FileDemo

{

public static void main(String args[])

{

try

{

FileOutputStream fout=new FileOutputStream("f.txt");

System.out.println("File created!");

String s="Built my site";

byte b[]=s.getBytes();

fout.write(b);

fout.close();

System.out.println("Writing complete!");

}

catch(Exception e)

{

System.out.println(e);

}

try

{

FileInputStream fin=new FileInputStream("f.txt");

int i;

while((i=fin.read())!= -1)

System.out.println((char)i);

fin.close();

System.out.println("Reading complete!");

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Output:-

67