KEMBAR78
Java Lab Manual Jwfiles PDF | PDF | Filename | Matrix (Mathematics)
0% found this document useful (0 votes)
91 views53 pages

Java Lab Manual Jwfiles PDF

The document contains code for three Java programs: 1. A program to calculate the nth Fibonacci number using recursive and non-recursive functions. 2. A program to calculate the nth Fibonacci number using recursion. 3. A program that takes a user input integer and prints all prime numbers up to that integer.

Uploaded by

sophy philo
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)
91 views53 pages

Java Lab Manual Jwfiles PDF

The document contains code for three Java programs: 1. A program to calculate the nth Fibonacci number using recursive and non-recursive functions. 2. A program to calculate the nth Fibonacci number using recursion. 3. A program that takes a user input integer and prints all prime numbers up to that integer.

Uploaded by

sophy philo
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/ 53

www.jntuworld.com www.jwjobs.

net

EXP: Date:
_______________________________________________________

AIM: write a java program that uses both recursive and

non-Recursive functions to print the nth value of the

Fibonacci sequence

PROGRAM CODE (without recursion):

import java.io.*;

import java.lang.*;

import java.util.*;

class fibonacii

public static int fibo(int n)

if(n<=2)

return(1);

else

int f1=1,f2=1,f3=0,i;

for(int i=3;i<=n;i++)

f3=f1+f2;

f1=f2;

f2=f3;

Rao And Naidu Engineering College Page 1

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
}

return(f3);

Public static void main(String args[])throws IOException

BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

System.out.println(“enter n value”);

int n=Integer.parseInt(br.readLine());

System.out.println(“the nth fibbinocci number

is”+fibbinocci.fibo(n));

Rao And Naidu Engineering College Page 2

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

Enter n value

the nth fibbinocci number is 8

Rao And Naidu Engineering College Page 3

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
PROGRAM CODE(with recursion):

import java.io.*;

import java.lang.*;

class fiborec

public static void main(String args[])throws IOException

BufferedReader br=new BuffererReader(new

InputStreamReader(System.in));

System.out.println(“ enter n value”);

int n=Integer.parseInt(br.readLine());

System.out.println(“the nth fibonacii number

is”+fibonacii.fibo(n));

public static int fibo(int n)

if (n<=2)

return(1);

else

(fibo(n-1)+fibo(n-2));

Rao And Naidu Engineering College Page 4

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

INPUT-OUTPUT:

enter n value

the nth fibbinocii number is 8

Rao And Naidu Engineering College Page 5

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

AIM: write a java program that prompts the user of an integer

and then printout all the prime numbers up to that

integer

PROGRAM CODE:

import java.io.*;

import java.lang.*;

class prime

public static void main(String args[ ])throws IOException

BufferedReader br-new BufferedReader(new

InputStreamReader(System.in));

System.out.println(“enter a number”);

int n=Integer.parseInt(br.readLine());

int c=0;

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

c=0;

for(int j=1;j<=I;j++)

if(i%j==0)

Rao And Naidu Engineering College Page 6

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
c++;

if(c==2)

System.out.println(“\n”+i);

Rao And Naidu Engineering College Page 7

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

INPUT-OUTPUT:

enter a number

10

Rao And Naidu Engineering College Page 8

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

AIM: write a java program that checks whether a given

String is a palindrome or not

PROGRAM CODE:

import java.io.*;

class palin

public static void main(String args[])throws IOException

{ BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

System.out.println(“enter a string”);

String str=br.readLine();

String temp=str;

StringBuffer sb=new StringBuffer(str);

sb.reverse();

str=sb.toString();

if(temp.equalsIgnoreCase(str))

System.out.println(temp+”is palindrome”);

else

System.out.println(temp+”is not palindrome”);

Rao And Naidu Engineering College Page 9

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

INPUT-OUTPUT:
enter a string

madam

madam is a palindrome

Rao And Naidu Engineering College Page 10

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

AIM: write a java program for sorting given list of names in

Ascending order

PROGRAM CODE:
import java.io.*;

import java.lang.*;

import java.util.*;

class sort

public static void main(String args[])

int k=args.length;

int i,j;

String a[] =new String[k];

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

a[i]=args[i];

System.out.println(“the given data is…”);

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

Sytsem.out.println(a[i]);

Rao And Naidu Engineering College Page 11

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(“the sorted data is”);

String t;

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

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

if((a[i].compareTo(a[j]))>0)

t=a[i];

a[i]=a[j];

a[j]=t;

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

Sytsem.out.println(a[i]);

Rao And Naidu Engineering College Page 12

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

INPUT-OUTPUT:

ah fr fg uj kl zd

the given data is…..

ah

fr

fg

uj

kl

zd

the sorted data is…

ah

fr

fg

uj

kl

zd

Rao And Naidu Engineering College Page 13

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

AIM: write a java program to check compatability for

Multiplication,if compatable multiply two matrices

And find its transpose

PROGRAM CODE:

import java.io.*;

import java.lang.*;

import java.util.*;

class matrix

Public static void main(String args[])

Scanner sc=new Scanner(System.in);

int a[][]=new int[5][5];

int b[][]=new int[5][5];

int c[][]=new int[5][5];

int I,j,k;

System.out.println(“enter no of rows and cols 1st matrix”);

int m=sc.nextInt();

int n=sc.nextInt();

System.out.println(“enter no of rows and cols for 2nd matrix”);

int l=sc.nextInt();

Rao And Naidu Engineering College Page 14

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
int p=sc.nextInt();

System.out.println(“enter a matrix elements”);

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

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

a[i][k]=sc.nextInt();

System.out.println(“a matrix elements are”);

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

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

System.out.println(a[i][k]+” “);

System.out.println();

System.out.println(“enter b matrix elements”);

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

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

b[i][k]=sc.nextInt();

System.out.println(“b matrix elements are”);

System.out.println(“the matrix elements are”);

Rao And Naidu Engineering College Page 15

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
for(i=0;i<m;i++)

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

c[i][k]=sc.nextLine();

System.out.println(“the transpose matrix”);

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

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

System.out.println(c[k][i]+” “);

System.out.println(“\n”);

}
}

Rao And Naidu Engineering College Page 16

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:
enter no.of rows n columns for 1st matrix

Enter no.of rows n columns for 2nd matrix

Enter a matrix elements

A matrix elements are.

1 3

4 2

Enter b matrix elements

B matrix elements are..

4 6

2 8

Rao And Naidu Engineering College Page 17

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
matrix elements are..

13

30

22

40

the transpose matrix

13

22

30

40

Rao And Naidu Engineering College Page 18

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write java program demonstrate a package.

PROGRAM CODE:

Package pack1;

public class addsub

private double d1,d2;

public addsub(double x,double y)

d1=x;

d2=y;

public void sum()

System.out.println(“sum=”+(d1+d2));

System.out.println(“sub=”+(d1-d2));

Rao And Naidu Engineering College Page 19

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
import pack1.*;

class user3

public static void main(String args[])

addsub as=new addsub(20,10);

as.sum();

Rao And Naidu Engineering College Page 20

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

sum=30.0

sub=10.0

Rao And Naidu Engineering College Page 21

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write program how runtime polymorphism is achieved

PROGRAM CODE:

class shape

protected int x,y;

shape (int x,int y)

this.x=x;

this.y=y;

void area1()

System.out.println(“area of rectangle is”);

void area2()

System.out.println(“area of triangle is”);

class rect extends shape

rect (int x,int y)

Rao And Naidu Engineering College Page 22

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
super(x,y);

void area1()

super.area1();

Sysem.out.println((x*y));

class triangle extends shape

triangle (int x,int y)

super(x,y);

void area2()

super.area2();

System.out.println((x*y)/2);

class mainc

Rao And Naidu Engineering College Page 23

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
public static void main(String args[])

rect r=new rect(10,20);

r.area1();

triangle t=new triangle(10,30);

t.area2();

Rao And Naidu Engineering College Page 24

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

area of rectangle is:

200

area of triangle is

150

Rao And Naidu Engineering College Page 25

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write java program using StringTokenizer class,which

reads a line of integer and then displays each integer

and sum of all integers.

PROGRAM CODE:

import java.io.*;

import java.lang.*;

import java.util.*;

class integ

public static void main(String args[] throwsIOException

BufferedReader br=new BufferedReader(new

InputStreamReader(System.in));

String str=br.readLine();

StringTokenizer st=new StringTokenizer(str,”,”);

int n=st.countTokens();

int sum=0;

System.out.println(“the entered numbers are…”);

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

int b= Integer.parseInt(st.nextToken());

Sum+=b;

Rao And Naidu Engineering College Page 26

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(b);

System.out.println(“sum=”+sum);

Rao And Naidu Engineering College Page 27

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

45,67,92

the entered numbers are….

45

67

92

sum=204

Rao And Naidu Engineering College Page 28

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program to read a filename from the user

then displays information about whether the file exists

whether the file is readable or writable the types of file

and the length of file in bytes and content of file using

FileInputStream class

PROGRAM CODE:

import java.io.*;

class files

public static void main(String args[]) throws IOException

String fname=args[0];

File f=new File(fname)

FileInputStream fis = new FileInputStream(“a.txt”);

System.out.println(“fileName:”f.getname());

System.out.println(“path:”+f.fetPath());

System.out.println(“absolute path:”+ f.getAbsolutepath());

System.out.println(“parent:”+f.canWrite());

System.out.println(“exists:”+s.exists());

if(f.exists())

System.out.println(“is writable:”+f.canWrite());

Rao And Naidu Engineering College Page 29

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(“is readable:”+f.canRead());

System.out.println(“isdirectory:”+f.isDirectory());

System.out.println(“file size inbytes:”+f.length());

Rao And Naidu Engineering College Page 30

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

filename:a.txt

path:a.txt

absolutepath:c:\document1\DELL\a.txt

parent:null

exists:true

is writable:true

is readable:true

is a directory:false

file size in bytes:60

Rao And Naidu Engineering College Page 31

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program displays no of charecters ,lines

And words in a text/text file

PROGRAM CODE:
import java.io.*;

class cwl

int ch;

boolean prev=true;

int charcount=0;

int wordcount =0;

int linecount=0;

FileInputStream fis=new FileInputStream(arg[0]);

while((ch=fis.read()!=-1)

if(ch!=‟ „) ++charcount;

if(!prev &&ch=‟ „) ++wordcount;

if(ch==‟ „)

prev=true;

else

if(ch==‟\n‟) ++linecount;

charcount -=linecount*2;

wordcount +=linecount;

Rao And Naidu Engineering College Page 32

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(“no of chars=”+charcount);

System.out.println(“no of words=”+wordcount);

System.out.println(“no of lines=”+linecount);

fis.close();

Rao And Naidu Engineering College Page 33

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

no of chars=48

no of words=8

no of lines=4

Rao And Naidu Engineering College Page 34

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program to demonstrate lifecycle of Thread

PROGRAM CODE:
import java.lang.Thread;

class A extends Thread

public void run()

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

if(i==1) yield();

System.out.println(“\t from threadA:i=”+i);

System.out.println(“exit from A”);

class B extends Thread

public void run()

for(int j=1;j<=5;j++)

Rao And Naidu Engineering College Page 35

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(“\t from thread b:j=”+j);

if(j==3) stop();

System.out.println(“exit from B”);

class C extends Thread

public void run()

for(int k=1;k<=5;k++)

System.out.println(“\t from thread c:k=”+k);

if(k==1)

try

sleep(1000);

catch(Exception e)

Rao And Naidu Engineering College Page 36

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
System.out.println(“exit from c”);

class threadlife

public static void main(String args[])

{ A threada= new A();

B threadb=new B();

C threadc=new C();

System.out.println(“start thread a”);

threada.start();

System.out.println(“start thread b”);

threadb.start();

System.out.println(“start thread c”);

threadc.start();

System.out.println(“end of main thread”);

Rao And Naidu Engineering College Page 37

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

start thread a

start thread b

from thread a :i=1

start thread c

exit from a

from thread b:j=1;

end of main thread

from thread a:i=2

from thread c:k=1

from thread b:j=2

exit from a

from thread b:j=3

from thread a:i=3

exit from a

from thread a:i=4

exit from a

from thread a:i=5

exit from a

from thread c:k=2

from thread c:k=3

from thread c:k=4

Rao And Naidu Engineering College Page 38

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
from thread c:k=5

exit from c

Rao And Naidu Engineering College Page 39

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program that correctly implements

Producer-consumer problem using the concept of

Inter Thread communication

PROGRAM CODE:

import java.io.*;

import java.lang.*;

class communicate

public static void main(String args[])throws IOException

producer obj1=new producer();

consumer obj2=new consumer(obj1);

Thread t1=new Thread(obj1);

Thread t2=new Thread(obj2);

t2.start();

t1.start();

class producer extends Thread

StringBuffer sb;

boolean dataprodover=false;

Rao And Naidu Engineering College Page 40

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
producer()

sb=new StringBuffer();

public void run()

synchronized(sb)

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

try

sb.append(i+”:”);;

Thread.sleep(100);

System.out.println(“appending”);

catch(Exception e)

sb.notify()

Rao And Naidu Engineering College Page 41

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
}

class consumer extends Thread

producer prod;

consumer(producer prod)

this.prod=prod;

public void run()

synchronized(prod.sb);

try

prod.sb.wait();

catch(Exception e)

System.out.println(prod.sb);

Rao And Naidu Engineering College Page 42

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

appending

appending

appending

appending

appending

appending

appending

appending

appending

appending

1:2:3:4:5:6:7:8:9:10:

Rao And Naidu Engineering College Page 43

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a Applet that displays content of a file

PROGRAM CODE:

import java.applet.*;

import java.awt.*;

import java.io.*;

public class fileapp extends Applet

int i,j,ch;

char ch1;

public void init( )

setBackground(Color.pink);

public void paint(Graphics g)

try

{ FileInputStream fis=new FileInputStream(“a.txt”);

int i=200,j=40;

while((ch=fis.read())!=-1)

ch1=(char)ch;

g.drawString(“ “+ch1,j,i);

Rao And Naidu Engineering College Page 44

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
j=j+8;

if(ch1==‟\n‟)

i=i+8;

j=40;

catch(FileNotFoundExeption f)

catch(IOExeption e)

<html>

<applet code=”fileapp.class” height=300 width=400>

</apple>

</html>

Rao And Naidu Engineering College Page 45

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________

AIM: write a program to create an abstract class named shape, that contains

an empty method noOfSides() provide 3 classes named Trepezoid,

Triangle and Hexogon, such that each one of classes contains method

noOfSides()

PROGRAM CODE:

import java.io.*;

import java.lang.*;

import java.util.*;

abstract class shape

public abstract void noOfSides();

class trepezoid extends shape

public void noOfSides()

System.out.println(“no of sides of tepezoid is 4”);

class triangle extends shape

Rao And Naidu Engineering College Page 46

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
public void noOfSides()

System.out.println(“no.of sides of triangle is 3”);

class hexagon extends shape

public void noOfSides()

{ System.out.println(“no of sides of hexagon is 6”);

class mainc

{ public static void main(String args[ ])

trapezoid t = new trapezoid();

t.noOfSides();

hexagon h=new hexagon();

h.noOfSides();

triangle t1=new triangle();

t1.noOfSides();

Rao And Naidu Engineering College Page 47

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
INPUT-OUTPUT:

no of sides of trepozoid is 4

no of sides of hexagon is 6

no of sides of triangle is 3

Rao And Naidu Engineering College Page 48

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program that allows user to drawlines, rectangle

and ovals

PROGRAM CODE:

import java.awt.*;

import java.applet.*;

public class myapp extends Applet

public void paint(Graphics g)

g.drawLine(10,10,50,50);

g.drawRect(10,60,40,30);

g.drawOval(30,60,20,10);

<html>

<applet code=”myapp.class” height=300 width=400>

</applet>

</html>

Rao And Naidu Engineering College Page 49

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
AIM: write a java program for handling mouse events

PROGRAM CODE:

import java.awt.*;

import java.applet.*;

import java.applet.event.*;

public class mouseevents extends Applet implements

mouseListener,mouseMotionListener

String msg=” ”;

int mouseX=0,mouseY=0;

public void init()

add mouseListener(this);

add mouseMotionListener(this);

public void mouseClicked(mouseEvent me)

mouseX=0;

mousey=10;

msg= “mouse clicked”;

repaint();

Rao And Naidu Engineering College Page 50

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
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)

Rao And Naidu Engineering College Page 51

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
{

mouseX=me.getX();

mouseY=me.getY();

msg=”up”;

repaint();

public void mouseDragged(mouseEvent me)

mouseX=me.getX();

mouseY=me.getY();

msg=”*”;

showStatus(“dragging 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);

Rao And Naidu Engineering College Page 52

www.jntuworld.com
www.jntuworld.com www.jwjobs.net

EXP: Date:
_______________________________________________________
}

<html>

<applet code=”mouseevents.class” height=300 width=400>

</applet>

</html>

Rao And Naidu Engineering College Page 53

www.jntuworld.com

You might also like