Showing posts with label Bluej. Show all posts
Showing posts with label Bluej. Show all posts

Display the longest word in a sentence Bluej

This program was compiled and checked on Bluej
This program accepts a sentence from the user and displays the longest word and its length.

 SOURCE CODE :

import java.io.*;//package
class longestword//class name longest word
{
static String str,word;
static int l,b,a,counter,l2;
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);
longestword()//default constructor
    {
        l=l2=0;
str="";
b=0;
counter=0;

    }
void inputword()throws IOException
{
System.out.println("Enter a sentence.....");
str=br.readLine();//store the sentence in str
l=str.length();//store the length of the sentence in l
str=str+" ";//add blank space to the sentece
}
void compute()throws IOException
    {
        for(a=0;a<=l;a++)
        {
            if(str.charAt(a)==' ')//to extract each word
            {
               
              
               l2=(str.substring(b,a)).length();//to store the length of a word
              
              
                if(l2>=counter)//compare length
                {
                   
                    word=str.substring(b,a);//to store the longest word
                    counter=l2;//store the frequency of the longest word
                }
               b=a+1;
               l2=0;
                  
            }
        }
    }
    void display()throws IOException
    {
        System.out.println("the longest word int the given sentence is..."+word);//to display the longest word
         System.out.println("frequency of the longest word is..."+counter);//display the frequency of the longest word
   
        }
        public static void main(String args[])throws IOException
        {
            longestword ob= new longestword();//creating object
            ob.inputword();
            ob.compute();
            ob.display();
        }
}

Output:


This program was compiled and checked on Bluej

Display the longest consecutive word in a word

SOURCE CODE:
import java.io.*;
class lgscons
{
public static void main(String args[])throws IOException
{
String strc[]=new String[1000];
int arrc[]=new int[10000];
InputStreamReader read= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(read);
String str;
int i=0;
int j=0;
int d=0;
int c=0;
int u =0;
int a=0;
int temp=0;
int terminator=0;
int counter=1;
int highest=0;
System.out.println("Enter a word IN SAme case.....");
str=br.readLine();
int l=str.length();
str=str+" ";

if((int)str.charAt(a+1)-(int)str.charAt(a)==1)
    {
for(a=0;a<l;a++)
{

if((int)str.charAt(a+1)-(int)str.charAt(a)==1)
{

c=a;
d=a;
do
{


while(((int)str.charAt(d+1)-(int)str.charAt(d)==1)&& d>=a)
    {
counter++;
d++;
if(((int)str.charAt(d+1)-(int)str.charAt(d)!=1)&& d>a)
{
terminator=1;
break;
}


    }
   
}
while(terminator!=1);
arrc[i++]=counter;
strc[j++]=str.substring(c,d+1);
a=d;
d=0;
c=0;
counter=1;
terminator=0;
}//if inner loop
}//for loop
temp=arrc[0];
for(u=0;u<j;u++)
{
if(temp<arrc[u]){
highest=u;

}
}
System.out.println("the largest cosecutive word is....."+strc[highest]);
}//outer if loop..
else
System.out.print("Wrong input.......");
}
}

OUTPUT: 



Find the power of a number using recursive technique

 SOURCE CODE:

import java.io.*;
class poweris
{//compliled by gulzar
static int n,m;//instance variables
static double p;//instance variables
poweris()//default constructor
{
n=0;
m=0;
p=0.0;
}
public static int power(int n,int m)
{
if(m==0)
return 1;
else
return (n*power(n,m-1));
}
public static void findresult()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(read);
System.out.println("enter the no....");
n=Integer.parseInt(br.readLine());//store the number
System.out.println("enter the power....");
m=Integer.parseInt(br.readLine());//store the power
int f=0;int g=0;
if(n<0){
n=-n;
g=1;
}
if(m<0){
m=-m;
f=1;}
p=power(n,m);
if(f==1)
p=1/p;;
if(g==1)
p=-p;
}
void printresult()
{
System.out.println("The power is ...."+p);
}
public static void main(String args[])throws IOException
{
poweris ob=new poweris();
ob.findresult();
ob.printresult();
}
}



OUTPUT:



Factorial of a number using recursive technique

SOURCE CODE:
import java.io.*;
class factorial
{
static int n,f,x;
factorial()
{
x=1;  
n=0;
f=1;
}
int fact(int n)
{
if(n>0)//this programme is compiled by gulzar
return n*(fact(n-1));
else
return 1;
}
void getnumber(int x)
{
f=fact(x);
System.out.println("the factorial is...."+f);
}
public static void main()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(read);
factorial ob=new factorial();
System.out.println("enter the no....");
n=Integer.parseInt(br.readLine());
ob.getnumber(n);
}
}
}

OUTPUT :



Reverse a string using Recursive Technique

Source Code

import java.io.*;
public class Revstr//class name Revstr
{//program compiled by Gulzar
static String str,revst;//instance variables
Revstr()//default constructor
{
str="";
revst="";
}
public static void getstr()throws IOException
{
InputStreamReader read = new InputStreamReader
(System.in);
BufferedReader br=new BufferedReader(read);
System.out.println("Enter the string");//accept a string
str=br.readLine();//store the string in str
}
public static void recReverse(int len)throws IOException
{
char ch=' ';
if(len>=0)
{
ch=str.charAt(len);//extract each character from the string
revst=revst+ch;//store the reversed string
recReverse(--len);//to decrease the value of len by 1
}
}
public static void check()throws IOException
{
System.out.println("Original string...."+str);//display the string
System.out.println("Reversed string...."+revst);//disaply the reversed string
}
public static void main(String args[])throws IOException
{
Revstr ob=new Revstr();
ob.getstr();
ob.recReverse(str.length()-1);
ob.check();
}//program compiled by Gulzar
}

OUTPUT: