Final Java LabPrgs
Final Java LabPrgs
import java.io.DataInputStream;
import java.io.IOException;
class Student
{
int regno,m1,m2,m3;
String name;
DataInputStream obj = new DataInputStream(System.in);
void getdata() throws IOException
{
System.out.println("\n*************************");
System.out.println("Enter Student Information");
System.out.println("*************************\n");
void show()
{
System.out.println("\nTotal : "+total);
System.out.println("Percenatge : "+percent);
}
}
class Single_Inheritance
{
public static void main(String args[])throws IOException
{
Calculate c1 = new Calculate();
c1.getdata();
c1.display();
c1.compute();
c1.show();
}
}
3) Java Program to sort n elements using array(Lab Program 3)******
import java.io.*;
class Sort
{
public static void main(String args[])throws IOException
{
int n,temp=0;
DataInputStream obj=new DataInputStream(System.in);
System.out.println("Enter the number of elements you want to
sort");
n=Integer.parseInt(obj.readLine());
int[] array=new int[20];
System.out.println("Enter the elements of the array");
for(int i=0;i<n;i++)
array[i]=Integer.parseInt(obj.readLine());
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
System.out.println("Sorted Elements are:");
for(int i=0;i<n;i++)
System.out.println(array[i]);
}
}
4) Java Program to implement constructor overloading by passing different
number of parameter of different types. (Lab Program 4***)
class Triangle
{
int x,y;
double z;
Triangle()
{
System.out.println("DEFAULT CONSTRUCTOR");
x=1;
y=2;
z=3;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i)
{
System.out.println("ONE PARAMETERIZED CONSTRUCTOR");
x=i;
y=i;
z=i;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i,double j)
{
System.out.println("TWO PARAMETERIZED CONSTRUCTOR");
x=i;
y=i;
z=j;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
Triangle(int i,int j,float k)
{
System.out.println("THREE PARAMETERIZED CONSTRUCTOR");
x=i;
y=j;
z=k;
System.out.println("x="+x+"\ty="+y+"\tz="+z);
}
double perimeter()
{
return(x+y+z);
}
}
class ConstructOverload
{
public static void main(String args[])
{ double res;
Triangle t1=new Triangle();
res=t1.perimeter();
System.out.println("Perimeter of Triangle (default values) = "+res+"\n");
int n=s[1].length();
System.out.println("\nThe string "+s[1]+" consist of "+n+"
characters");
boolean b=s[0].equals(s[1]);
if(b)
System.out.println("\nString "+s[0]+" equals to "+s[1]);
else
System.out.println("\nString "+s[0]+" not equals to "+s[1]);
b=s[0].equalsIgnoreCase(s[1]);
if(b)
System.out.println("String "+s[0]+" equals to string "+s[1]+"
by ignoring case");
else
System.out.println("String "+s[0]+" not equals to "+s[1]+" by
ignoring case");
n=s[2].compareTo(s[3]);
if(n<0)
System.out.println("String "+s[2]+" is less than "+s[3]);
else if(n==0)
System.out.println("String "+s[2]+" equals to string "+s[3]);
else
System.out.println("String "+s[2]+" is greater than string
"+s[3]);
n=s[1].indexOf('v');
System.out.println("\nIndex of v in "+s[1]+" is "+n);
char c=s[1].charAt(3);
System.out.println("\nCharacter at 4th position of string "+s[1]+" is
"+c);
String y=s[1].concat(s[2]);
System.out.println("\nThe string "+s[1]+" & "+s[2]+" after
concatination is "+y);
String z=y.substring(3,8);
System.out.println("\nThe substring of "+y+" from 4th position to
9th position is "+z);
v.addElement("One");
v.addElement("Two");
v.addElement("Three");
System.out.println("\nVector List: "+v);
System.out.println("Size= "+v.size());
System.out.println("Capacity ="+v.capacity());
v.insertElementAt("zero",0);
v.insertElementAt("BCA",3);
v.insertElementAt("Four",5);
v.insertElementAt("BSc",6);
v.insertElementAt(20,7);
System.out.println("\nVector List: "+v);
System.out.println("size="+v.size());
System.out.println("capacity=" +v.capacity());
v.removeAllElements();
System.out.println("\nAfter removing all elements:\n"+v);
System.out.println("size="+v.size());
System.out.println("capacity=" +v.capacity());
}
}
7) Java program to demonstrate concept of interface.(LAB PROGRAM 7)
import java.util.Scanner;
interface Area // Interface defined
{
final static float PI= 3.142F;
float compute ();
void display(float s);
}
class InterfacePrg
{
public static void main(String args[])
{
Rectangle rect = new Rectangle(10,20);
Circle cir = new Circle(10);
float x;
boolean flag=true;
Area a=null; // Interface object
Scanner sc=new Scanner(System.in);
System.out.println("\nTo calculate the Area ");
System.out.println("\nEnter the choice: R - Rectangle C - Circle");
String s=sc.nextLine();
if(s.equals("R"))
a = rect;
else if(s.equals("C"))
a=cir;
else
flag=false;
if(flag)
{
x=a.compute();
a.display(x);
}
else
System.out.println("Wrong Choice");
}
}
8) Java Program to demonstrate concept of creating, accessing and using a
package. (LAB PROGRAM 8)
class ThreadPriority
{
public static void main(String args[])throws IOException
{
Thread t=Thread.currentThread();
System.out.println("\nCurrent Thread Running is: "+t);
t.setName("Main thread");
t.setPriority(t.getPriority()-2);
System.out.println("After name and priority change of Main thread:" +t);
import java.awt.*;
import java.applet.*;
/*
<applet code="Banner.class" width=1000 height=1000>
</applet>
*/
public class Banner extends Applet implements Runnable
{
String msg=" JSS SMI College ";
Thread t;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.blue);
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
try
{
repaint();
Thread.sleep(200);//delays each thread by 250ms
//shifts the first character of banner text to the last position
msg=msg.substring(1)+msg.charAt(0);
}
catch(InterruptedException e)
{
}
}
}
public void paint(Graphics g)
{
Font fl=new Font("Times New Roman", Font.BOLD,45);
g.setFont(fl);
g.drawString(msg,300,300);
}
}
13) Java program to count number of strings, integers and float values through
command line arguments.(Lab program 13)******
class CmdLine
{
public static void main(String args[])
{
int cs=0,ci=0,cf=0;
for(int i=0;i<args.length;i++)
{
try
{
int j= Integer.parseInt(args[i]);
ci++;
}
catch(NumberFormatException e)
{
String s=args[i];
try
{
float f=Float.parseFloat(s);
cf++;
}
catch(NumberFormatException n)
{
cs++;
}
}
}
System.out.println("String:"+cs+"\nIntegers:"+ci+"\nFloat:"+cf);
}
}
14) Java program to accept a message from the keyboard and display the number
of words and non alphabetical characters.(Lab Program 14)****
import java.util.Scanner;
class Count
{
public static void main(String args[])
{
String s;
int i,alph=1,nonalph=0;
char ch;
Scanner sc=new Scanner(System.in);
System.out.println("\nEnter the message");
s=sc.nextLine();
String s2=s.trim();
System.out.println(s2.length());
if(s2.length()==0)
{
alph=0;
}
else
{
for(i=0;i<s2.length();i++)
{
ch=s2.charAt(i);
if(ch==' ')
alph++;
else if(Character.isLetter(ch)==false)
nonalph++;
}
}
System.out.println("\nNumber of Words="+alph);
System.out.println("\nNumber of Non Alphabetical Characters = "+nonalph);
}
}
OUTPUT
16) Java program to demonstrate concept of event handling.
(LAB PROGRAM 16)*****
import java.applet.Applet;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;
/*<applet code="MouseEventprg.class" height=500 width=1000></applet>*/
public class MouseEventprg extends Applet implements MouseListener
{
String msg="";
int x=50,y=100;
public void init()
{
setBackground(Color.PINK);
setForeground(Color.BLUE);
Font f=new Font("Arial",Font.BOLD,26);
setFont(f);
addMouseListener(this);
}//end of init
}//end of paint
}//end of class
17) Java program to demonstrate different types of fonts.
(LAB PROGRAM 17)*****
import java.awt.*;
import java.applet.*;
/*<applet code = "FontDemo.class" height = 500 width =800></applet>*/
public class FontDemo extends Applet
{
Font f1,f2,f3,f4;
public void init()
{
setForeground(Color.BLUE);
f1 = new Font("Algerian",Font.BOLD |Font.ITALIC,40);
f2 = new Font("Bookman Old Style",Font.BOLD,24);
f3 = new Font("Cooper Black",Font.ITALIC,28);
f4 = new Font("Times New Roman",Font.PLAIN,32);
}
g.setFont(f2);
g.drawString("1) Object Oriented, Robust & Secure",60,100);
g.setFont(f3);
g.drawString("2) Portable & Platform Independent",60,130);
g.setFont(f4);
g.drawString("3) Multithreaded",60,160);
}
}
18) Java program to create an applet to tokenize the strings.
(LAB PROGRAM 18)*****
import java.awt.*;
import java.applet.*;
import java.util.StringTokenizer;
/*
<applet code="StrToken.class" width=400 height=500></applet>
*/
public class StrToken extends Applet
{
public void init()
{
setBackground(Color.pink);
setForeground(Color.blue);
}
public void paint(Graphics g)
{
try
{
Font fl=new Font("Bookman Old Style",Font.BOLD,45);
g.setFont(fl);
StringTokenizer st = new StringTokenizer("This is a java
programming lab"," ");
while(st.hasMoreTokens())
{
for(int i=10;i<500;i+=50)
{
g.drawString(st.nextToken(),20,i+60);
}
}
}
catch(Exception e){}
}
}
19) Java program to design a simple calculator using java applets.
(LAB PROGRAM 19)**
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code = "Calculator.class" width=500 height=500>
</applet>
*/
public class Calculator extends Applet implements ActionListener
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1,b2,b3,b4;
t1=new TextField(20);
t1.setBounds(120,20,60,20);
add(t1);
l3=new Label("Result");
l3.setBounds(10,60,100,20);
add(l3);
l4=new Label();
l4.setBounds(120,60,100,20);
add(l4);
b1=new Button("Add");
b1.setBounds(10,80,30,30);
add(b1);
b2=new Button("Sub");
b2.setBounds(40,80,30,30);
add(b2);
b3=new Button("Mul");
b3.setBounds(70,80,30,30);
add(b3);
b4=new Button("Div");
b4.setBounds(100,80,30,30);
add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Double num1=Double.parseDouble(t1.getText());
Double num2=Double.parseDouble(t2.getText());
if(ae.getSource()==b1)
{
Double value=num1+num2;
l4.setText(""+value);
}
if(ae.getSource()==b2)
{
Double value=num1-num2;
l4.setText(""+value);
}
if(ae.getSource()==b3)
{
Double value=num1*num2;
l4.setText(""+value);
}
if(ae.getSource()==b4)
{
Double value=num1/num2;
l4.setText(""+value);
}
}
}
20) Java Program to implement static and dynamic stack using interface using
abstract class. (LAB PROGRAM 20)*****
import java.util.*;
interface stackop
{
void push(int item);
void pop();
}
abstract class FixedStack implements stackop
{
int stk[ ];
int tos;
void getdata(int size)
{
stk=new int[size];
tos=-1;
int a=stk.length;
System.out.println("Fixed Stack Length="+a);
}
public void push(int item)
{
if(tos==stk.length-1)
{
System.out.println("Stack is full.\n");
}
else
System.out.println(stk[++tos]=item);
}
public void pop()
{
if(tos==-1)
{
System.out.println("All elements are popped out!Stack
underflows");
}
else
System.out.println(stk[tos--]);
}
}
class DynStack extends FixedStack implements stackop
{
DynStack(int size)
{
stk=new int[size];
tos=-1;
int a=stk.length;
}
public void push(int item)
{
if(tos==stk.length-1)
{
System.out.println(("Stack is full. Increasing the capacity."));
int t[ ]=new int[stk.length * 2]; //double size
for(int i=0;i<stk.length;i++)
t[i]=stk[i];
stk=t;
int a=stk.length;
System.out.println("Length(Double Size)="+a);
System.out.println(stk[++tos]=item);
}
else
System.out.println(stk[++tos]=item);
}
public void pop()
{
if(tos==-1)
{
System.out.println("Stack underflows");
}
else
System.out.println(stk[tos--]);
}
}
class StackTest extends FixedStack
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
int n=3;
System.out.println("The size of Fixed Stack:"+n);
System.out.print("\nEnter the size of Dynamic Stack:");
int p=sc.nextInt();
DynStack ds=new DynStack(p);
mystk=ds;
System.out.println("\nDynamic Stack Operations:");
System.out.println("Initial Length of dynamic stack="+p);
System.out.println("Inserting Elements into stack are:");
for(int i=0;i<(p+2);i++)
mystk.push(i);
System.out.println("\nDynamic length Stack Contents are");
for(int i=0;i<(p+2);i++)
mystk.pop();
}
}