Saturday, April 25, 2009

JAVA lab programs for JNTU II BTech II Sem

Program : 01

/*Write a java program to print all the real solutions of the equation ax2+bx+c=0. Read a, b, c and use the quadratic formula. If discriminant b2-4ac is negative display a message stating that there are no solutions.*/



import java.lang.*;
class roots
{
public static void main(String args[ ])
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = ((b*b)-(4*a*c));
if(d > 0)
{
System.out.println(“Roots are real and distinct”);
}
if(d==0)
{
System.out.println(“Roots are real and equal”);
}
if (d < 0)
{
System.out.println(“There are no solution”);
}
}
}








OUTPUT – Program :01





















Program : 02

/*The Fibonacci sequence is defined by the following rule:
The first two values in the sequence are 1 and 1. Every subsequent values is the sum of the two values preceding it. Write a java program that uses both recursive and non recursive functions to print the nth values in the Fibonacci sequence*/


import java.lang.*;
class fibo
{
public static void main(String args[ ])
{
int a, b, n, c, i;
n = Integer.parseInt(args[0]);
a = -1;
b = 1;
i = 1;
System.out.println(“*************Fibonacci Series*************”);
do
{
c = a + b;
System.out.println(c);
a = b;
b = c;
i = i+1;
}
while(i <= n);
}
}









OUTPUT –Program : 02














Program : 03

/*Write a java program that prompts the user for an integer and then prints out all prime numbers up to that integer.*/

import java.io.*;
class prime
{
public static void main(String args[ ])
{
int i, j, n, c;
n = Integer.parseInt(args[0]);
for(i =2; i <= n; i++)
{
c = 0;
for(j =2; j < i; j++)
{
if(i%j == 0)
c+=1;
}
If(c == 0)
System.out.println(i+ “is prime”);
}
}
}














OUTPUT – Program : 03























Program : 04

/*Write a java program to multiply two given matrices.*/

import java.io.*;
class matmul
{
public static void main(String args[ ])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter row size");
int m = Integer.parseInt(br.readLine( ));
System.out.println("Enter column size");
int n = Integer.parseInt(br.readLine());
int a[ ][ ]=new int[m][n];
int b[ ][ ]=new int[m][n];
int c[ ][ ]=new int[m][n];
System.out.println("enter the elements for matirx a:");
for(int i = 0; i < m; i++)
for(int j = 0; j< n; j++)
a[i][j] = Integer.parseInt(br.readLine( ));
System.out.println("enter the elements for matrix b:");
for(int i= 0; i < m; i++)
for(int j = 0; j< n ; j++)
b[i][j] = Integer.parseInt(br.readLine( ));
for(int i = 0;i< m;i++)
{
for(int j =0;j < n;j++)
{
for(int k =0;k < n;k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("Multiplication of a and b elements is:");
for(int i=0;i< m;i++)
{
for(int j=0; j< n;j++)
{
System.out.println(c[i][j] +"\t");
}
System.out.println( );
}
}
}


OUTPUT – Program : 04








Program : 05

/*Write a java program that reads a line of integers, and then displays each integer, and sum of all the integers*/

import java.io.*;
import java.lang.*;
class array
{
public static void main(String args[ ])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size=20, sum=0;
System.out.println(“Enter the size of array:”);
Size = Integer.parseInt(br.readLine( ));
int a[ ] = new int[size];
System.out.println(“enter list of integers”);
for(int i = 0; i < size; i++)
a[i] = Integer.parseInt(br.readLine( ));
for(int i = 0; i < size ; i++)
sum = sum + a[i];
System.out.println(“Entered no’s are:”);
for(int i = 0;i < size; i++)
System.out.println(a[i]);
System.out.println(“sum = “+sum);
}
}












OUTPUT – Program : 05























Program : 06

/*Write a java program that checks whether a given string is a palindrome or not.*/

import java.io.*;
class palindrome
{
public static void main(String args[ ])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter any string:”);
String S1 = br.readLine( );
String S2 = new String( );
StringBuffer S3 = new StringBuffer(S1);
StringBuffer S4 = S3.reverse( );
System.out.println(S1);
System.out.println(S4);
String S5 = S4.toString( );
if(S1.equals(S5))
System.out.println(“PALINDROME”);
else
System.out.println(“NOT A PALINDROME”);
}
}















OUTPUT – Program : 06






















Program – 07

/*Write a java program for sorting a given list of names in ascending order.*/

import java.io.*;
class alphabet
{
public static void main(String args[ ])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter how many Strings:”);
int n = Integer.parseInt(br.readLine( ));
String x[ ] = new String[n];
System.out.println(“Enter “ +n+ “Strings:”);
for(int i = 0;i< n; i++)
{
x[i] = br.readLine( );
}
String S = new String( );
for(int i = 0; i< n; i++)
{
for(int j = 0;j< n; j++)
{
if(x[i].compareTo(x[j] ) < 0)
{
S = x[i];
x[i] = x[j];
x[j] = S;
}
}
}
System.out.println(“Strings in alphabetical Order”);
for(int i = 0; i < n; i++)
System.out.println(x[i]);
}
}



OUTPUT – Program : 07





















Program : 08
/* Write a java program to Implement Stack – ADT.*/

import java.io.*;
class stack
{
int S[ ] = new int[10];
int top;
stack( )
{
top = -1;
}

void push(int item)
{
if(top == 9)
System.out.println(“Stack is Overflow”);
else
S[++top] = item;
}

int pop( )
{
if(top < 0)
{
System.out.println(“Stack is underflow”);
return 0;
}
else
return S[top--];
}
}

class stacktest
{
public static void main(String args[ ])
{
stack MS1 = new stack( );
stack MS2 = new stack( );
for(int i = 0; i< 10;i++)
{
MS1.push(i);
}
for(int i = 10;i < 20; i++)
{
MS2.push(i);
}
System.out.println(“Elements in Stack1”);
for(int i = 0;i < 10; i++)
{
System.out.println(MS1.pop( ));
}
System.out.println(”Elements in stack2”);
for(int i = 10; i< 20; i++)
{
System.out.println(MS2.pop( ) );
}
}
}



















OUTPUT – Program : 08
















Program : 09
/*Write a java program for implementing Constructors.*/
(i) Default Constructor.
import java.io.*;
class Add
{
int a, b;
Add( )
{
System.out.println(“Default Constructor”);
a = 10;
b = 20;
}
void sum( )
{
System.out.println(“a + b=”+(a+b));
}
}
class addition
{
public static void main(String args[ ])
{
Add test = new Add( );
test.sum( );
}
}

OUTPUT- Program : 09(i)
(ii)Argument Constructor
import java.io.*;
class triangle
{
double base;
double height;
triangle(double b, double h)
{
base = b;
height = h;
}
double area( )
{
return (0.5*base*height);
}
}
class triangle1
{
public static void main(String args[ ])
{
double area1;
triangle t1 = new triangle(10,20);
triangle t2 = new triangle(20,30);
area1 = t1.area( );
System.out.println(“Area of t1=” +area1);
area1 = t2.area( );
System.out.println(“Area of t2=” +area1);
}
}
OUTPUT – Program : 09(ii)
(iii)Copy Constructor

import java.io.*;
class copy
{
int a, b;
public copy(int a1, int b1)
{
a = a1;
b = b1;
}
public copy(copy c)
{
a = c.a;
b = c.b;
}
void view( )
{
System.out.println(“a =”+a+”b = “+b);
}
}
class copytest
{
public static void main(String args[ ])
{
copy test = new copy(10, 15);
System.out.println(“First Constructor”);
test.view( );
copy test1 = new copy(test);
System.out.println(“Second Constructor”);
test1.view( );
}
}







OUTPUT – Program : 09(iii)


























Program : 10

/*Write a java program to implement Simple Inheritances.*/

import java.io.*;
class base
{
int x, y;
void viewxy( )
{
System.out.println(“x = “+x+”y = ” +y);
}
void baseAdd( )
{
System.out.println(“x + y = ” +(x+y));
}
}
class derived extends base
{
int z;
void viewz( )
{
System.out.println(“z = “ +z);
}
void derivedsum( )
{
System.out.println(“x + y + z=” +(x+y+z));
}
}
class simpleinheritance
{
public static void main(String args[ ])
{
base b = new base( );
derived d = new derived( );
b.x = 1;
b.y = 2;
System.out.println(“Base class values”);
b.viewxy( );
b.baseAdd( );
System.out.println(“Derived class”);
d.x = 4;
d.y = 5;
d.z = 6;
d.viewxy( );
d.viewz( );
d.derivedsum( );
}
}

OUTPUT – Program – 10















Program : 11

/*Write a java program to Implement Interfaces.*/

import java.io.*;
interface shape
{
double pi = 3.14;
double volume(double a, double b);
}
class sphere implements shape
{
public double volume(double a, double b)
{
return(4.0/3.0*pi*a*a*a);
}
}
class cone implements shape
{
public double volume(double a, double b)
{
return((1.0/3.0)*pi*a*a*b);
}
}
class testinterface
{
public static void main(String args[ ])
{
sphere s = new sphere( );
cone c = new cone( );
shape s1;
s1 = s;
System.out.println(“Volume of the sphere = “+s1.volume(1.0,0));
System.out.println(“Volume of the cone = “+s1.volume(1.0,1.0));
}
}




OUTPUT – Program : 11

































Program : 12

/*Write a java program implementing Exception Handling.*/

import java.io.*;
class ownException extends Exception
{
ownException(String msg)
{
super(msg);
}
}
class Test
{
Public static void main(String args[ ])
{
int marks = 101;
try
{
if(marks > 100)
{
throw new ownException(“Mark is greater than 100”);
}
}

catch(ownException e)
{
System.out.println(“Exception caught”);
System.out.println(e.getMessage( ));
}

finally
{
System.out.println(“End of the Program”);
}
}
}


OUTPUT – Program : 12



























Program : 13

/*Write a java program on threads.*/

import java io.*;
public class tables
{
public static void main(String args[ ])
{
System.out.println(“Multiplicaion tables using threads”);
new tablethree( ).start( );
new tablefive( ).start( );
new tableseven( ).start( );
}
}
class tablethree extends thread
{
public void run( )
{
System.out.println(“Multiplication Table THREE : “);
for(int i = 1;i <= 10 ; i++)
System.out.println(i+ ”x” + ”3=” +(i*3));
}
}
class tablefive extends thread
{
publlic void run( )
{
System.out.println(” Multiplication Table FIVE”);
for(int i =1 ;i <= 10; i++)
System.out.println(i+ ”x” + ”5=” +(i*5));
}
}
class tableseven extends thread
{
public void run( )
{
System.out.println(”Multiplication Table SEVEN:”);
for(int i = 1; i <= 10; i++)
System.out.println(i+ ”x” + ”7=” +(i*7));
}
}

OUTPUT – Program : 13











Program : 14

/*Write a java program to Implement MOUSE Events. */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*


*/
public class mm1 extends Applet
implements MouseListener, MouseMotionListener
{
String msg = " ";
int mouseX = 0, mouseY=0;
public void init( )
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX = 0;
mouseY =10;
msg = "Mouse Clicked";
repaint( );
}
public void mouseEntered(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse Entered";
repaint( );
}
public void mouseExited(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "mouse Exited";
repaint( );
}
public void mousePressed(MouseEvent me)
{
mouseX = me.getX( );
mouseY = me.getY( );
msg = "Down";
repaint( );
}
public void mouseReleased(MouseEvent me)
{
mouseX = me.getX( );
mouseY = me.getY( );
msg = "Up";
repaint( );
}
public void mouseDragged(MouseEvent me)
{
mouseX = me.getX( );
mouseY = me.getY( );
msg = " * ";
showStatus("Dragged mouse at" + mouseX + ", " + mouseY);
repaint( );
}
public void mouseMoved(MouseEvent me)
{
showStatus("Moving mouse at " + me.getX( ) + ", " + me.getY( ) );
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}






OUTPUT – Program : 14


































































Program : 15

/*Write a java program that Implements KEYBOARD Events.*/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class SimpleKey extends Applet implements KeyListener
{
String msg = " ";
int X = 10, Y = 20;
public void init( )
{
addKeyListener ( this ) ;
requestFocus( );
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar( );
repaint( );
}
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}



OUTPUT – Program : 15




Applet Viewer :






















Program : 16

/*Write a java program on life – Cycle of Applet*/

import java.awt.*;
import java.applet.*;
public class lifecycle extends Applet
{
Public void init( )
{
String out;
public void init( )
{
setBackground(Color.blue);
setForeground(Color.yellow);
System.out.println(“init”);
out = “init -> “ ;
}
public void start( )
{
System.out.println(“start”);
out+= “start ->”;
}
public void stop( )
{
System.out.println(“stop”);
}
public void destroy( )
{
System.out.println(“destroy”);
}
public void paint(Graphics g)
{
out+=”paint->”;
g.drawString(out, 100, 50);
}
}


/*
*/


OUTPUT – Program : 16



Applet Viewer:









Program : 17

/*Write a java program for a Calculator using AWT Components.*/

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class calc extends Applet implements ActionListener
{
Label l1,l2;
TextField t1,t2,t3;
Button add1,sub,mul,div;
public void init( )
{
l1 = new Label("First No:");
add(l1);
l2 = new Label("Second No:");
add(l2);
t1 = new TextField(10);
add(t1);
t2 = new TextField(10);
add(t2);
add1 = new Button("+");
add(add1);
add1.addActionListener(this);
sub = new Button("-");
add(sub);
sub.addActionListener(this);
mul = new Button("*");
add(mul);
mul.addActionListener(this);
div = new Button("/");
add(div);
div.addActionListener(this);
t3 = new TextField(10);
add(t3);
}


public void actionPerformed(ActionEvent e)
{
if(e.getSource( ) == add1)
{
int sum = Integer.parseInt(t1.getText( )) + Integer.parseInt(t2.getText( ));
t3.setText(String.valueOf(sum));
}
if(e.getSource( ) == sub)
{
int sum = Integer.parseInt(t1.getText( )) - Integer.parseInt(t2.getText( ));
t3.setText(String.valueOf(sum));
}
if(e.getSource( ) == mul)
{
int sum = Integer.parseInt(t1.getText( )) * Integer.parseInt(t2.getText( ));
t3.setText(String.valueOf(sum));
}
if(e.getSource( ) == div)
{
int sum = Integer.parseInt(t1.getText( )) / Integer.parseInt(t2.getText( ));
t3.setText(String.valueOf(sum));
}
}
}
/*
*/

OUTPUT – Program : 17











Program : 18

/*Write a java program on clipping.*/

import java.awt.*;
import java.applet.*;
public class rec extends Applet
{
public void paint(Graphics g)
{
g.setClip(50,50,150,150);
for(int i = 0; i < 300; i+=20)
for(int j = 0; j < 300; j +=20)
g.drawOval(i,j,25,25);
setBackground(Color.blue);
setForeground(Color.white);
}
}
/*
*/


OUTPUT – Program : 18
















Program : 19

/*Write a java program for Different Layouts.*/
(i)Border layout
import java.awt.*;
class bl extends Frame
{
bl(String s)
{
super(s);
setSize(300,140);
add(new Button(“North”),BorderLayout.NORTH);
add(new Button(“East”),BorderLayout.EAST);
add(new Button(“South”),BorderLayout.SOUTH);
add(new Button(“West”),BorderLayout.WEST);
add(new Button(“Center”),BorderLayout.CENTER);
setVisible(true);
}
}
class test2
{
public static void main(String args[ ])
{
bl b = new bl(“Border layout”);
}
}
OUTPUT – Program : 19





(ii) Grid Layout
import java.awt.*;
class gl extends Frame
{
gl(String s)
{
super(s);
setSize(300,140);
setLayout(new GridLayout(3,4));
for(int i=1; i<=12;i++)
add(new Button(“Button No” +i));
setVisible(true);
}
}
class test1
{
public static void main(String args[ ]){
gl g=new gl(“Grid Layout”);
}
}

OUTPUT – Program : 19(ii) – Grid Layout

(iii) Flow Layout
import java.awt.*;
class bf extends Frame
{
bf(String s)
{
super(s);
setSize(300,140);
setLayout(new FlowLayout( ));
for(int i=1 ; i<= 9;i++)
add(new Button (“Button No” +i));
setVisible(true);
}
}
class test3
{
public static void main(String args[ ])
{
bf b = new bf(“flow Layout”);
}
}

OUTPUT – Program : 19(iii) – Flow Layout





Program : 20

/*Write a java program for different Components.*/
(i)Label
import java.awt.*;
import java.applet.*;
public class label extends Applet
{
public void init( )
{
setLayout(new GridLayout(3,1));
setBackground(Color.yellow);
Label l1 = new Label("BSIT");
Label l2 = new Label("IT",Label.CENTER);
Label l3 = new Label("Java",Label.LEFT);
add(l1);
add(l2);
add(l3);
}
}
/*
*/

OUTPUT – Program : 20(i)







(ii) Text box
import java.awt.*;
import java.applet.*;
public class text extends Applet
{
public void init( )
{
setLayout(new GridLayout(2,2));
Label l1 = new Label(“Username:”,Label.CENTER);
TextField t1 = new TextField( );
Label l2 = new Label(“password”,Label.CENTER);
TextField t2 = new TextField( );
t2.setEchoChar(‘#’);
add(l1);
add(t1);
add(l2);
add(t2);
}
}
/*
*/

OUTPUT – Program : 20(ii)






(iii) CheckBox

import java.applet.*;
import java.awt.*;
public class check extends Applet
{
public void init( )
{
setLayout(new GridLayout(3,1));
Checkbox c1 = new Checkbox("Tamil",true);
Checkbox c2 = new Checkbox("English");
Checkbox c3 = new Checkbox("Maths");
add(c1);
add(c2);
add(c3);
}
}
/*
*/


OUTPUT – Programs : 20(iii)







(iv) Choice

import java.awt.*;
import java.applet.*;
public class choice extends Applet
{
public void init( )
{
Choice color = new Choice( );
color.add("Red");
color.add("Green");
color.add("Blue");
color.add("Cyan");
add(new Label("Normal Appearance"));
add(color);
}
}
/*
*/

OUTPUT – Program : 20(iv)









(v) CheckBox Group

import java.awt.*;
import java.applet.*;
public class checkbg extends Applet
{
public void init( )
{
setLayout(new GridLayout(3,1));
CheckboxGroup cbg = new CheckboxGroup( );
Checkbox c1 = new Checkbox("B.Tech",cbg,true);
Checkbox c2 = new Checkbox("M.Tech",cbg,false);
Checkbox c3 = new Checkbox("M.C.A",cbg,false);
add(c1);
add(c2);
add(c3);
}
}
/*
*/

OUTPUT – Program : 20(v)








(vi) Frame

import java.awt.*;
class frame
{
public static void main(String args[ ])
{
Frame f = new Frame("This is my Frame");
f.setBackground(Color.red);
f.setSize(300,100);
f.setVisible(true);
System.out.println("Return to DOS prompt and Press Control+C for quit");
}
}

OUTPUT – Program :20(vi)













(vii) Button

import java.awt.*;
class frameb
{
public static void main(String args[ ])
{
Button b;
Frame f =new Frame("This is my Button Frame");
f.setSize(250,150);
b = new Button("Press Me");
f.add(b);
f.setLocation(200,200);
f.setVisible(true);
System.out.println("Return to DOS prompt and Press Control+C for quit");
}
}


OUTPUT – Program : 20(vii)








(viii) Menu Bar

import java.awt.*;
public class mymenu
{
public static void main(String arg[ ])
{
Frame f = new Frame("The is test for Menu");
MenuBar main = new MenuBar( );
f.setMenuBar(main);
Menu filemenu = new Menu("File");
Menu editmenu = new Menu("Edit");
Menu helpmenu = new Menu("Help");
main.add(filemenu);
main.add(editmenu);
main.add(helpmenu);
MenuItem new1 = new MenuItem("New");
MenuItem open = new MenuItem("Open");
MenuItem close = new MenuItem("Close");
MenuItem line = new MenuItem("-");
CheckboxMenuItem print = new CheckboxMenuItem("Print");
MenuItem exit = new MenuItem("Exit");
filemenu.add(new1);
filemenu.add(open);
filemenu.add(close);
filemenu.add(line);
filemenu.add(print);
filemenu.add(exit);
MenuItem cut = new MenuItem("Cut");
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
MenuItem undo = new MenuItem("Undo");
editmenu.add(cut);
editmenu.add(copy);
editmenu.add(paste);
editmenu.addSeparator( );
editmenu.add(undo);
undo.setEnabled(false);
Menu more = new Menu("More");
helpmenu.add(more);
more.add("commands");
more.add("about");
f.setSize(200,200);
f.setVisible(true);
}
}

OUTPUT – Program : 20(viii)

1 comment: