Java Practicals (Mine)
Java Practicals (Mine)
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
int num;
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
char c;
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
int i;
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
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
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
double d;
float f;
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
int i;
double d;
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
int i= s.length();
System.out.println("Length=" +i);
System.out.println("Uppercase="+s1);
System.out.println("Lowercase="+s2);
char c=s.charAt(7);
String s5=sc.nextLine();
boolean d=(s.equals(s5));
System.out.println(d);
boolean b=(s.equalsIgnoreCase(s7));
System.out.println(b);
System.out.println(s9);
String s10=s.substring(2);
System.out.println(s10);
System.out.println(s11);
}
Output:
Practical No:11 & 12
Q1.Write a program to implement all methods of StringBuffer class.
import java.util.*;
class buffer
sb.delete(5,7);
sb.deleteCharAt(5);
sb.substring(5, 12);
sb.insert(9,"Education");
sb.append("Bhandup");
sb.setCharAt(3,'a');
sb.replace(1,3,"tech");
}
Output:-
Practical No:14
Q1.write a program to use a different methods of vector class.
import java.util.*;
class vdemo
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(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(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(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
byte b=i.byteValue();
float f=i.floatValue();
double d=i.doubleValue();
long l=i.longValue();
short s=i.shortValue();
}
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");
void move()
super.move();
System.out.println("dog class");
class demo
d.move();
}}
Output:
Practical No:18
Q1.Develop a program to implement the multilevel inheritance .
class Car
String cartype;
Car(String c)
cartype=c;
String Brandname;
int speed;
super(c);
Brandname=a;
speed=s;
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
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();
int l;
int b;
int z;
void get()
l=sc.nextInt();
b=sc.nextInt();
z=l*b;
{
System.out.println("The area of rectangle is"+z);
double r;
double a;
float s;
void accept()
r=sc.nextInt();
a=e*r*r;
class interdemo
c.area();
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
for(int i=1;i<=10;i++)
System.out.println("Thread 2= " + i );
{
public void run()
for(int i=1;i<=10;i++)
if ( i% 2==0)
System.out.println("Thread 3= " + i );
class Thdemo
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.*;
myException(String s)
super(s);
class excepdemo
String pswd="NESP-BHANDUP";
System.out.println("Enter password");
String s=sc.nextLine();
try
if(s.equals(pswd))
System.out.println("Correct Password");
}
else
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.*;
NotMatchException(String s)
super(s);
class matchdemo
String s1="India";
System.out.println("Enter String");
String s2=sc.nextLine();
try
if(s2.equals(s1))
System.out.println("String matched");
}
else
catch(NotMatchException me)
System.out.println(me);
}
Output:
Practical no:28
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
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.*;
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.*;
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.*;
setBackground(Color.black);
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);
Output:
Practical No:29
Q Write a program to implement an applet to draw basic animated
shapes.
import java.awt.*;
import java.applet.*;
//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.*;
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.*;
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
int i=0;
try
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
int i=0;
try
while(i!=-1)
i=fin.read();
fout.write(i);
fin.close();
fout.close();
catch(Exception e)
System.out.println(e); } }}
Output: