0% found this document useful (0 votes)
22 views

Java Practicals (Mine)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Java Practicals (Mine)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

Practical No: 3

Q2. Write a program to make the ude of logical operators.


import java.util.*;
class ifdemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter 3 numbers: ");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b&&a>c)
{
System.out.println("A is the greatest");
}
else if(b>a&&b>c)
{
System.out.println("B is the greatest");
}
else if(c>a&&c>b)
{
System.out.println("C is the greatest");
}

else
{
System.out.println("All numbers are equal");

}
}
}
Output:
Practical No: 3
Q3.Write a program to check no is even or odd.
import java.util.*;

class ademo

public static void main(String args[])

int num;

Scanner sc=new Scanner(System.in);

System.out.println("Enter Number");

num=sc.nextInt();

if(num%2==0)

System.out.println("Number is Even");

else

System.out.println("Number is Odd");

}
Output:
Practical No:4
Q2.Write a program to check switch case statement using character
datatype.
import java.util.*;

class switchdemo

public static void main(String args[])

Scanner sc=new Scanner(System.in);

char c;

System.out.println("Enter any character");

c=sc.next().charAt(0);

switch(c)

case'a':System.out.println("It is a vowel");

break;

case'e':System.out.println("It is a vowel");

break;

case'i':System.out.println("It is a vowel");

break;

case'o':System.out.println("It is a vowel");

break;

case'u':System.out.println("It is a vowel");

break;

default:System.out.println("It is a consonant");
break;

Output:
Practical No: 5
Q1.Write any program using if condition with for loop.
class Nestedif

public static void main(String args[])

int i;

System.out.print("Even numbers from 1 to 50");

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

if(i%2==0)

System.out.println(i);

}
Output:
Practical No: 5
Q2.Write any program to display pyramids of stars/patterns using
increment.
import java.util.*;

class demo

public static void main(String args[])

int i,j;

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

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

System.out.print("*");

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

}
Output:
Practical No:6
Q2.Write a program to display number 1 to 50 using do while loop.
class ademo

public static void main(String args[])

int no=1;

do

System.out.print("\t"+no);

no++;

while(no<=50);

}
Output:
Practical No:7&8
Q3.Write a program to implicitly typecast lower range data type to
larger storage size data type.
import java.util.*;

class impdemo

public static void main(String args[])

Scanner sc=new Scanner(System.in);

double d;

float f;

System.out.println("Enter Float number");

f=sc.nextFloat();

d=f;

System.out.println("Float value="+f);

System.out.println("Double value="+d);

}
Output:
Practical No:9
Q2.Write a program to convert variable of basic datatypes and
shows result of explicit typecasting.
import java.util.*;

class expdemo

public static void main(String args[])

int i;

double d;

Scanner sc=new Scanner(System.in);

System.out.println("Enter double value");

d=sc.nextDouble();

i=(int)d;

System.out.println("Double is "+d);

System.out.println("Integer is "+i);

}
Output:
Practical No:11&12
Write a program to show the use of all methods of String class.
import java.util.*;

class tdemo

public static void main(String args[])

Scanner sc= new Scanner(System.in);

String s= new String("Nesp Bhandup");

int i= s.length();

System.out.println("Length=" +i);

String s1= s.toUpperCase();

System.out.println("Uppercase="+s1);

String s2= s.toLowerCase();

System.out.println("Lowercase="+s2);

char c=s.charAt(7);

System.out.println("charAt 7th index" +c);

String s3= s.replace('n','d');

System.out.println("After replace=" +s3);

String s4= s.trim( );

System.out.println("After trim" +s4);

String s5=sc.nextLine();

boolean d=(s.equals(s5));

System.out.println(d);

String s6= new String ("NESP");


String s7= sc.nextLine();

boolean b=(s.equalsIgnoreCase(s7));

System.out.println(b);

String s8= new String ("Bhandup");

String s9= s.concat(s8);

System.out.println(s9);

String s10=s.substring(2);

System.out.println(s10);

String s11= s.substring(1,2);

System.out.println(s11);

int a= s.indexOf('a', 4);

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

}
Output:
Practical No:11 & 12
Q1.Write a program to implement all methods of StringBuffer class.
import java.util.*;

class buffer

public static void main(String args[])

StringBuffer sb= new StringBuffer("Navjeevan Polytechnic");

sb.delete(5,7);

System.out.println("The delete function is = "+ sb);

sb.deleteCharAt(5);

System.out.println("The delete at specific position is = "+ sb);

sb.substring(5, 12);

System.out.println("The substring function is = "+ sb);

sb.insert(9,"Education");

System.out.println("The insert function is = "+ sb);

sb.append("Bhandup");

System.out.println("The append function is = "+ sb);

sb.setCharAt(3,'a');

System.out.println("The setcharat function is = "+ sb);

sb.replace(1,3,"tech");

System.out.println("The replace function is = "+ sb);

}
Output:-
Practical No:14
Q1.write a program to use a different methods of vector class.
import java.util.*;

class vdemo

public static void main(String args[])

Vector v=new Vector();

v.addElement("Nesp");

v.addElement("Bhandup");

v.addElement(new Integer(10));

v.addElement(new Integer(20));

v.addElement(new Float(23.1f));

v.addElement(new Float(1.23f));

System.out.println(v.size());

System.out.println(v);

v.removeElement("Nesp");

System.out.println(v);

v.removeElementAt(2);

System.out.println(v);

System.out.println("Size of vector="+v.size());

System.out.println("capacity of vector="+v.capacity());

System.out.println("Element present at index 2="+v.elementAt(2));

System.out.println(v);
v.addElement("Neha");

v.addElement("Apurva");

System.out.println(v);

v.insertElementAt("JPR",0);

System.out.println(v);

v.set(1,"Nesp");

System.out.println(v);

System.out.println("capacity of vector="+v.capacity());

System.out.println("Size of vector="+v.size());

System.out.println("First element of vector="+v.firstElement());

System.out.println("last element of vector="+v.lastElement());

System.out.println(v.indexOf("Nesp"));

System.out.println(v);

v.addElement("Navjeevan Polytechnic");

System.out.println(v.indexOf("e",7));

System.out.println(v);

if(v.isEmpty())

System.out.println("Vector is empty");

else

System.out.println("vector is not empty");

System.out.println(v);
if(v.contains("Neha"))

System.out.println("Present");

else

System.out.println("Absent");

System.out.println(v);

}
Output:-
Practical No:15&16
Q3.Write a program to convert Integer object value into primitive
datatype byte,short,and double value.
class wdemo

public static void main(String args[])

Integer i=new Integer(30);

byte b=i.byteValue();

System.out.println("byte conversion of integer="+b);

float f=i.floatValue();

System.out.println("float conversion of integer="+f);

double d=i.doubleValue();

System.out.println("double conversion of integer="+d);

long l=i.longValue();

System.out.println("long conversion of integer="+l);

short s=i.shortValue();

System.out.println("short conversion of integer="+s);

}
Output:-
Practical No:17
Q3.Develop a program to extend „dog‟ from‟animal‟ to override
„move()‟ method using super keyword.
class Animal

void move()

System.out.println("animal class");

class Dog extends Animal

void move()

super.move();

System.out.println("dog class");

class demo

public static void main(String args[])

Dog d=new Dog();

d.move();

}}
Output:
Practical No:18
Q1.Develop a program to implement the multilevel inheritance .
class Car

String cartype;

Car(String c)

cartype=c;

class Brand extends Car

String Brandname;

int speed;

Brand(String c,String a,int s)

super(c);

Brandname=a;

speed=s;

class Model extends Brand

String Modelname;

int price;
Model(String c,String a,int s,String e,int p)

super(c,a,s);

Modelname=e;

price=p;

void display()

System.out.println("Cartype="+cartype);

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

System.out.println("Speed="+speed);

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

System.out.println("Price="+price);

class superdemo

public static void main(String args[])

Model m= new Model("Sedan","Honda",150,"Honda city",3600000);

m.display();

}
Output:
Practical No: 19
Q2.Develop a program to find area of rectangle and circle using
interfaces.
import java.util.*;

interface Shape

double e=3.14;

void area();

class Rectangle implements Shape

Scanner sc=new Scanner(System.in);

int l;

int b;

int z;

void get()

System.out.println("Enter the length");

l=sc.nextInt();

System.out.println("Enter the breath");

b=sc.nextInt();

z=l*b;

public void area ()

{
System.out.println("The area of rectangle is"+z);

class Circle implements Shape

double r;

double a;

float s;

Scanner sc=new Scanner(System.in);

void accept()

System.out.println("Enter the value of radius");

r=sc.nextInt();

a=e*r*r;

public void area()

System.out.println("The area of circle is"+a);

class interdemo

public static void main(String args[]

Circle c= new Circle();


c.accept();

c.area();

Rectangle r=new Rectangle ();

r.get();

r.area();

Output:
Practical No:21 &22
Q2.Create three threads and run these threads according to set priority.

INPUT
class thread1 extends Thread

public void run()

for(int i=1;i<=10;i++)

System.out.println("Thread 1= " + i);

class thread2 extends Thread

public void run()

for (int i=10;i>=1;i--)

System.out.println("Thread 2= " + i );

class thread3 extends Thread

{
public void run()

for(int i=1;i<=10;i++)

if ( i% 2==0)

System.out.println("Thread 3= " + i );

class Thdemo

public static void main(String args[])

thread1 t1=new thread1();

thread2 t2=new thread2();

thread3 t3=new thread3();

t1.setPriority(1);

t2.setPriority(6);

t3.setPriority(10);

t1.start();

t2.start();

t3.start();

}}
OUTPUT:
Practical No.23,24&25
Q1.Develop a program to accept a password from the user and throw
“Authentication Failure” exception if the password is incorrect.

INPUT
import java.util.*;

class myException extends Exception

myException(String s)

super(s);

class excepdemo

public static void main(String args[])

String pswd="NESP-BHANDUP";

Scanner sc=new Scanner(System.in);

System.out.println("Enter password");

String s=sc.nextLine();

try

if(s.equals(pswd))

System.out.println("Correct Password");
}

else

throw new myException("Authentication Failure");

catch(myException e)

System.out.println(e);

OUTPUT
Practical No:26&27
Q1.Define an Exception called “NotMatchException” that is thrown
when a string is not equal to”India”. Write a program that uses this
exception.
import java.util.*;

class NotMatchException extends Exception

NotMatchException(String s)

super(s);

class matchdemo

public static void main(String args[])

String s1="India";

Scanner sc=new Scanner(System.in);

System.out.println("Enter String");

String s2=sc.nextLine();

try

if(s2.equals(s1))

System.out.println("String matched");
}

else

throw new NotMatchException("String not matched");

catch(NotMatchException me)

System.out.println(me);

}
Output:
Practical no:28

Q1.Develop a basic applet to display “Welcome to the world of


applet”.

import java.awt.*;
import java.applet.*;
/*<applet code="apdemo.class" width=500 height=500></applet>*/
public class apdemo extends Applet
{
public void paint(Graphics g)
{
g.drawString("Welcome to the world of applet",40,80);
}
}

Output:-
Practical No:28

2. develop a program to implement all methods of applet.

import java.awt.*;
import java.applet.*;
/*<applet code="appdemo.class" width=500 height=500></applet>*/
public class appdemo extends Applet
{
public void init()
{
System.out.println("init()");
}
public void start()
{
System.out.println("start()");
}
public void paint(Graphics g)
{
System.out.println("Paint()");
}
public void stop()
{
System.out.println("stop()");
}
public void destroy()
{
System.out.println("destroy()");
}
}
Output:
Practical No:28
Q3.Develop a program using control loops in applets.
import java.awt.*;

import java.applet.*;

/*<applet code="appletdemo.class" width=500 height=500></applet>*/

public class appletdemo extends Applet

public void paint(Graphics g)

int x=15;

int y=20;

for(int i=1;i<=10;i++)

g.drawString(i+" ",x,y);

x=x+10;

}
Output:-
Practical No:29
Q1.Develop a program to draw a polygon.
import java.awt.*;

import java.applet.*;

/*<applet code="appdemo.class" width=500 height=500></applet>*/

public class appdemo extends Applet

public void paint(Graphics g)

int x[]={200,240,100,200};

int y[]={90,180,200,90};

int n=3;

g.drawPolygon(x,y,n);

}}
Output:
Practical No:29
Q2.Develop an applet for drawing a human face.
import java.awt.*;

import java.applet.*;

/*<applet code="appdemo.class" width=500 height=500></applet>*/

public class appdemo extends Applet

public void init()

setBackground(Color.black);

public void paint(Graphics g)

g.setColor(Color.pink);

g.fillOval(100,40,360,450);

g.setColor(Color.black);

g.fillOval(180,150,50,50);

g.fillOval(320,150,50,50);

int x[]={270,290,250,270};

int y[]={250,270,270,250};

int n=3;

g.setColor(Color.red);

g.drawPolygon(x,y,n);

g.setColor(Color.red);

g.fillArc(175, 200, 200, 200, 180, 180);


}

Output:
Practical No:29
Q Write a program to implement an applet to draw basic animated
shapes.
import java.awt.*;

import java.applet.*;

/*<applet code="mdemo.class" width=600 height=600></applet>*/

public class mdemo extends Applet

public void paint(Graphics g)

//face

g.setColor(Color.CYAN);

g.fillOval(20,5,400,450);

g.setColor(Color.white);

g.fillOval(70,55,310,400);

g.setColor(Color.black);

g.drawOval(70,55,310,400);

//eyes

g.setColor(Color.white);

g.fillOval(100,40,120,190);

g.fillOval(220,40,120,190);

g.setColor(Color.black);

g.drawOval(100,40,120,190);

g.drawOval(220,40,120,190);

g.fillOval(170,150,30,40);
g.fillOval(240,150,30,40);

//nose

g.setColor(Color.red);

g.fillOval(180,205,80,80);

//mouth

g.setColor(Color.red);

g.fillArc(125,230,195,195,180,180);

g.setColor(Color.black);

g.drawLine(270,245,360,220);

g.drawLine(270,270,360,270);

g.drawLine(270,295,360,320);

g.drawLine(160,245,70,220);

g.drawLine(160,270,70,270);

g.drawLine(160,295,70,320);

//bell

g.setColor(Color.red);

g.fillRect(120,430,210,30);

g.setColor(Color.yellow);

g.fillOval(180,440,80,80);

g.setColor(Color.black);

g.fillOval(210,490,20,20);

g.drawLine(180,475,260,475);

g.drawLine(180,480,260,480);

}
Output:
Practical No:30
Q3.Develop a program of the following.
a.Square Inside a Circle

import java.awt.*;

import java.applet.*;

/*<applet code="pdemo.class" width=500 height=500></applet>*/

public class pdemo extends Applet

public void paint(Graphics g)

g.drawLine(100,100,300,100);

g.drawLine(100,300,300,300);

g.drawLine(100,100,100,300);

g.drawLine(300,300,300,100);

g.fillOval(50,60,300,280);

}
Output:
Practical No:30
Q3.Develop a program of the following.
b.Circle Inside a Square

import java.awt.*;

import java.applet.*;

/*<applet code="vdemo.class" width=500 height=500></applet>*/

public class vdemo extends Applet

public void paint(Graphics g)

g.drawLine(100,100,330,100);

g.drawLine(100,300,330,300);

g.drawLine(100,100,100,300);

g.drawLine(330,300,330,100);

g.setColor(Color.pink);

g.fillOval(100,100,230,200);

}
Output:
Practical No:31&32
Q1. Develop a program to copy characters from one file to another.
Input:-
import java.io.*;

class demo

public static void main(String arg[]) throws Exception

int i=0;

try

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

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

while(i!=-1)

i=fin.read();

fout.write(i);

fin.close();

fout.close();

catch(Exception e)

System.out.println(e);

} }}
Output:-
Practical No:31&32
Q3. Develop a program to display the content of file supplied as command line
arguments.
Input:-

import java.io.*;

class demo

public static void main(String arg[]) throws Exception

int i=0;

try

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

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

while(i!=-1)

i=fin.read();

fout.write(i);

fin.close();

fout.close();

catch(Exception e)

System.out.println(e); } }}
Output:

You might also like