Java prog upto week 8

Post on 10-May-2015

102 views 6 download

Tags:

Transcript of Java prog upto week 8

Week-1

EXERCISE-1:

1a ) Aim: The Fibonacci values in the sequence are 1 and 1. Every subsequent value 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 value in the fibbonacci sequence.

Program:*/

import java.lang.*;

import java.util.*;

class Fibonacci

{

public static void main(String args[])

{

int a=0,b=1,c;

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

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int i=3;

System.out.println("Fibonacci series for n is");

System.out.print(a+" "+b+" ");

while(i<=n)

{

c=a+b;

a=b;

b=c;

System.out.print(c+" ");

i++;

}

}

}

Output:

Enter n value

5

Fibonacci series for n is

0 1 1 2 3

1b) Recursion program:

import java.lang.*;

import java.util.*;

class FibonacciRec

{

public static void main(String args[])

{

int a=0,b=1;

Recurssion fr= new Recurssion();

Scanner sc = new Scanner(System.in);

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

int n= sc.nextInt();

System.out.println("Fibonacci series for n is");

System.out.print(" "+a+" "+b+" ");

fr.recurssion(n,a,b);

}

}

class Recurssion

{

int i=3;

public void recurssion(int n,int a ,int b)

{

int c;

if(i<=n)

{

c=a+b;

a=b;

b=c;

System.out.print(c+" ");

i++;

recurssion(n,a,b);

}

}

}

Output:

Enter n value

4

Fibonacci series for n is

0 1 1 2

Week-2

2) Aim: Write a java program that prompts the user for an integer and the prints out all prime numbers upto that integer.

Program:*/

import java.io.*;

import java.lang.*;

import java.util.*;

class Prime

{

public static void main(String args[])

{

int i,j,n,count=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter n value:");

n=sc.nextInt();

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

{

count=0;

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

{

if(i%j==0)

count++;

}

if(count==2)

System.out.println("\t"+i);

}

}

}

Output:

Enter n value:

10

2

3

5

7

Week-3

3a) Aim: Write a java program that checks whether a given string is a palindrome or not. Ex: MADAM is a palindrome.*/

Program:

import java.util.*;

public class Palan

{

public static void main(String a[])

{

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

String s=new Scanner(System.in).next();

for(int i=0,j=s.length()-1;i<=j;i++,j--)

{

if(s.charAt(i)==s.charAt(j));

else

{

System.out.println("Not Palindrome");

System.exit(1);

}

}

System.out.println("Palindrome");

}

}

Output:

Enter any string

MADAM

Palindrome

Enter any string

KAUSHIK

Not Palindrome

3b) Aim: Write a java program for sorting a given list of names in ascending order.

Program:

import java.util.*;

class Sortst

{

public static void main(String a[])

{

System.out.println("How many string u want to enter");

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

String st[]=new String[n+1];

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

{

System.out.println("Enter string "+(i+1));

st[i]=sc.next();

}

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

{

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

{

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

{

String t=st[i];

st[i]=st[j];

st[j]=t;

}

}

}

System.out.println("Strings in order are :\n\n");

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

System.out.println(st[i]);

}

}

Output:

Enter string 1

Kaushik

Enter string 2

College

Enter string 3

Engineering

Enter string 4

Courses

Strings in order are:

College

Courses

Engineering

Kaushik

Week-4

4a) Aim : write java program that reads a file name from the user, then display information about whether the file exists, whether the file is readable, whether the file is writable, the type of file and the length of the file in bytes

Program:*/

import java.io.*;

import java.util.*;

class Fileattr

{

public static void main(String args[])

{

File f1=new File(args[0]);

System.out.println(f1.exists()?"file exists":"does not exist");

System.out.println("Filename:"+f1.getName());

System.out.println("Filepath:"+f1.getAbsolutePath());

System.out.println(f1.isDirectory()?"is a directory":"is a simple file");

System.out.println(f1.canRead()?"can be read":"can not be read");

System.out.println(f1.canWrite()?"can be write":"can not be write");

System.out.println("length of file in bytes:"+f1.length());

}

}

Output:

File exists

Filename:First.txt

Filepath:D:\\First.txt

Is a simple file

Can be read

Can be write

Length of file in bytes:55

4b) Aim: write a java program that reads a file and displays the file on the screen, with a line number before each line.

Program:*/

import java.io.*;

public class ReadFile

{

public static void main(String[] args)

{

try

{

FileReader input = new FileReader(args[0]);

BufferedReader bufRead = new BufferedReader(input);

String line;

int count = 0;

line = bufRead.readLine();

count++;

while (line != null)

{

System.out.println(count+": "+line);

line = bufRead.readLine();

count++;

}

bufRead.close();

}

catch (ArrayIndexOutOfBoundsException e)

{

System.out.println("Usage: java ReadFile filename\n");

}

catch (IOException e)

{

e.printStackTrace();

}

}

}

Output:

D:\>java Readfile first.txt

1:This is my file

2:Kaushik engineering college

3:Visakhapatnam

Week-5

5) Aim: Write a java program that displays the number of characters, lines and words in a text file.

Program:*/

import java.io.*;

import java.util.*;

import java.lang.*;

public class ClwinText

{

private static void linecount(String fName, BufferedReader in) throws IOException

{

long numChar = 0;

long numLine=0;

long numWords = 0;

String line;

do{

line = in.readLine();

if (line != null)

{

numChar += line.length();

numWords += wordcount(line);

numLine++;

}

}while(line != null);

System.out.println("File Name: " + fName);

System.out.println("Number of characters: " + numChar);

System.out.println("Number of words: " + numWords);

System.out.println("Number of Lines: " + numLine);

}

private static void linecount(String fileName)

{

BufferedReader in = null;

try

{

FileReader fileReader = new FileReader(fileName);

in = new BufferedReader(fileReader);

linecount(fileName,in);

}

catch(IOException e)

{

e.printStackTrace();

}

}

private static long wordcount(String line)

{

long numWords = 0;

int index = 0;

boolean prevWhiteSpace = true;

while(index < line.length())

{

char c = line.charAt(index++);

boolean currWhiteSpace = Character.isWhitespace(c);

if(prevWhiteSpace && !currWhiteSpace)

{

numWords++;

}

prevWhiteSpace = currWhiteSpace;

}

return numWords;

}

public static void main(String[] args)

{

long numChar = 0;

long numLine=0;

String line;

try

{

if (args.length == 0)

{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

line = in.readLine();

numChar = line.length();

if (numChar != 0)

{

numLine=1;

}

System.out.println("Number of characters: " + numChar);

System.out.println("Number of words: " + wordcount(line));

System.out.println("Number of lines: " + numLine);

}

else

{

for(int i = 0; i < args.length; i++)

{

linecount(args[i]);

}

}

}

catch(IOException e){

e.printStackTrace();

}

}

}

Output:

File Name:ClwinText.java

Number of characters:1928

Number of words:246

Number of lines:91

Week -6

6a) Aim: write a java program that:

i)implements stack ADT.

Program:

import java.io.*;

import java.util.*;

class Stack1

{

int top=-1,st[]=new int[5];

void push(int el)

{

st[++top]=el;

}

int pop()

{

return(st[top--]);

}

void display()

{

System.out.println("\nStack elements from top to bottom\n");

for(int i=top;i>=0;i--)

System.out.println(st[i]);

}

boolean isFull()

{

return(top==5-1);

}

boolean isEmpty()

{

return(top==-1);

}

}

class Stack

{

public static void main(String a[])

{

Scanner sc=new Scanner(System.in);

Stack1 s=new Stack1();

int el=0,ch=1;

while(ch!=4)

{

System.out.println("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");

System.out.println("ENTER YOUR CHOICE");

ch=sc.nextInt();

switch(ch)

{

case 1:if(s.isFull())

System.out.println("\nstack is full");

else

{

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

el=sc.nextInt();

s.push(el);

}break;

case 2:if(s.isEmpty())

System.out.println("\nstack is empty");

else

{

el=s.pop();

System.out.println("\nDeleted element = "+el);

}break;

case 3:if(s.isEmpty())

System.out.println("\nstack is empty");

else

s.display();

break;

case 4:break;

default:System.out.println("\nEnter correct choice");

}

}

}

}

Output:

1.PUSH

2.POP

3.DISPLAY

4.EXIT

Enter your choice

1

Enter element

52

Enter your choice

1

Enter element

45

Enter your choice

3

Stack elements from top to bottom

45

52

Enter your choice

2

Deleted element=45

Enter your choice

4

6b) Aim: converts infix expression into postfix form.

Program:

import java.io.*;

import java.util.*;

public class InfixToPostfix

{

private Stack theStack;

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

{

BufferedReader A = new BufferedReader (new InputStreamReader(System.in));

String mathex;

System.out.println("enter a mathematical expresion:" );

mathex = A.readLine();

String input = null;

String output;

InfixToPostfix theTrans = new InfixToPostfix(mathex);

output = theTrans.doTrans();

System.out.println("infix is : " + mathex + '\n');

System.out.println("postfix is : " + output + '\n');

}

String input;

String output = "";

public InfixToPostfix(String in) {

input = in;

int stackSize = input.length();

theStack = new Stack(stackSize);

}

public String doTrans() {

for (int j = 0; j < input.length(); j++) {

char ch = input.charAt(j);

switch (ch) {

case '+':

case '-':

gotOper(ch, 1);

break;

case '*':

case '/':

gotOper(ch, 2);

break;

case '(':

theStack.push(ch);

break;

case ')':

gotParen(ch);

break;

default:

output = output + ch;

break;

}

}

while (!theStack.isEmpty()) {

output = output + theStack.pop();

}

return output;

}

public void gotOper(char opThis, int prec1)

{

while (!theStack.isEmpty()) {

char opTop = theStack.pop();

if (opTop == '(') {

theStack.push(opTop);

break;

}

else {

int prec2;

if (opTop == '+' || opTop == '-')

prec2 = 1;

else

prec2 = 2;

if (prec2 < prec1)

{

theStack.push(opTop);

break;

}

else

output = output + opTop;

}

}

theStack.push(opThis);

}

public void gotParen(char ch){

while (!theStack.isEmpty()) {

char chx = theStack.pop();

if (chx == '(')

break;

else

output = output + chx;

}

}

class Stack {

private int maxSize;

private char[] stackArray;

private int top;

public Stack(int max) {

maxSize = max;

stackArray = new char[maxSize];

top = -1;

}

public void push(char j) {

stackArray[++top] = j;

}

public char pop() {

return stackArray[top--];

}

public char peek() {

return stackArray[top];

}

public boolean isEmpty() {

return (top == -1);

}

}

}

Output:

Enter a mathematical expression

A+b*c

Infix is:a+b*c

Postfix is:abc*+

Enter a mathematical expression

(a+b)*(c/d)*e-f

Infix is:(a+b)*(c/d)*e-f

Postfix is:ab+cd/e*f-

Week -7

7) Aim: Write a Java program that creates three threads. First thread displays “Good Morning”

every one second, the second thread displays “Hello”every two seconds and the third

thread displays “Welcome”every three seconds.

Program:*/

class A extends Thread

{

synchronized public void run()

{

try

{

while(true)

{

sleep(1000);

System.out.println("good morning");

}

}

catch(Exception e)

{ }

}

}

class B extends Thread

{

synchronized public void run()

{

try

{

while(true)

{

sleep(2000);

System.out.println("hello");

}

}

catch(Exception e)

{ }

}

}

class C extends Thread

{

synchronized public void run()

{

try

{

while(true)

{

sleep(3000);

System.out.println("welcome");

}

}

catch(Exception e)

{ }

}

}

class ThreadDemo

{

public static void main(String args[])

{

A t1=new A();

B t2=new B();

C t3=new C();

t1.start();

t2.start();

t3.start();

}

}

Output:

good morning

good morning

hello

good morning

welcome

hello

good morning

good morning

Week -8

8) Aim: Sample Program to implement package

package Demo;

public class A

{

public void display()

{

System.out.println("Hello Vinay");

}

}

import Demo.*;

public class packageDemo

{

public static void main(String args[])

{

A ob=new A();

ob.display();

}

}

Output:

D:\>cd demo

D:\Demo>javac A.java

D:\Demo>cd..

D:\>javac packageDemo.java

D:\>java packageDemo

Hello world