Friday, September 13, 2019

BlueJ Program on Finding Longest And Shortest Words From A Sentence




Write a Program to enter a sentence and find the longest and shortest words from the sentence. In both cases the first word (longest or shortest) will be displayed.

 import java.io.*;
class Words
{
    String str1,str2,maxs,mins;
    int max,min;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       private void show() throws Exception
      {
         System.out.print("\nEnter the sentence:");
         str1=br.readLine().trim();
         str1=str1+" ";
         max=0;
         min=str1.length();
         while(true)
         {
              int i=str1.indexOf(' ');
              if (i<0 o:p="">
              break;
              str2=str1.substring(0,i);
              str1=str1.substring(i+1);
              if(str2.length()>max)
              {
                   max=str2.length();
                   maxs=str2;
                }
                else if(str2.length()
              {
                   min=str2.length();
                   mins=str2;
                }
            }
            System.out.println("Longest Word Is:"+maxs);
            System.out.println("Shortest Word Is:"+mins);
               
        
}
      
        public static void main(String args[]) throws Exception
        {
             Words ob=new Words ();
             ob.show();
            }
        }
       
        Sample Input and Output
       
        Enter the sentence: I am a proud Indian
        Longest Word Is: Indian
        Shortest Word Is: a

Monday, September 9, 2019

Take Three Numbers And Display The Numbers In Descending Order Using Nested Ifs






import java.util.*;
class Search
{
    int a,b,c;
    Scanner sc=new Scanner(System.in);
public void assign()
{   
System.out.print("\nEnter 1st Number:");
a=sc.nextInt();
System.out.print("\nEnter 2nd Number:");
b=sc.nextInt();
System.out.print("\nEnter 3rd Number:");


c=sc.nextInt();
}
public void display()
{
    if(a>b && a>c)
    {          
        System.out.println("\nHighest = "+a);
        if(c>b)
        {
           System.out.println("\nSecond Highest = "+c);
           System.out.println("\nLowest = "+b);
        }
        else // else of if(c>b)
        {
           System.out.println("\nSecond Highest = "+b);
           System.out.println("\nLowest = "+c);
        }
    }
    else // else of if(a>b && a>c)
    {
        if(b>a && b>c)
    {          
        System.out.println("\nHighest = "+b);
        if(c>a)
        {
           System.out.println("\nSecond Highest = "+c);
           System.out.println("\nLowest = "+a);
        }
        else // else of if(b>a && b>c)
        {
           System.out.println("\nSecond Highest = "+a);
           System.out.println("\nLowest = "+c);
        }
    }
    else // else of if(b>a && b>c)
    {
       System.out.println("\nHighest = "+c);
        if(b>a)
        {
           System.out.println("\nSecond Highest = "+b);
           System.out.println("\nLowest = "+a);
        }
        else
        {
           System.out.println("\nSecond Highest = "+a);
           System.out.println("\nLowest = "+b);
        }
  
} // end of  else of if(b>a && b>c)
} // end of if(b>a && b>c)
}
    public static void main(String args[])
    {
        Search ob=new Search();
        ob.assign();
        ob.display();
   }
 }

Related Post: BlueJ Programs on Number

Wednesday, September 4, 2019

Changing The Vowel Characters With * In A Sentence






import java.util.*;
class Search
{
    int i,len;
    String str1,str2="";
    Scanner sc=new Scanner(System.in);
public void take()
{    
    System.out.print("\nEnter the sentence: ");
     str1=sc.nextLine();        
 }
    public void show()
    {
        char ch;
        len=str1.length();
        for(i=0;i< len;i++)
        {
             ch=str1.charAt(i);
             switch(ch)
             {


                  case 'A':
                  case 'a':
                  case 'E':
                  case 'e':
                  case 'I':
                  case 'i':
                  case 'O':
                  case 'o':
                  case 'U':
                  case 'u':
                  ch='*';
                }
                str2=str2+ch;
            }
        
        System.out.println("\nModified Sentence: "+str2);
    }
      
    public static void main(String args[])
    {
        Search ob=new Search();
        ob.take();
        ob.show();
   }
 }


Related Post: BlueJ Programs on String/Sentence

BlueJ Program To Count Frequency Of A Word In A Sentence. Input The Sentence And A Word






The sentence must be terminated with a full stop (.)

Sample Input:

Enter any sentence: This is my India and India is heaven for me.
Enter any Word: India
Frequency of the searched word: 2

import java.util.*;
class Search
{
    String str1,str2,str;
    Scanner sc=new Scanner(System.in);
public void assign()
{    
System.out.print("\nEnter any sentence:");
str1=sc.nextLine();
System.out.print("\nEnter any Word:");


str2=sc.nextLine();
}
public void display()
{
    int len;
    int c=0;
    len=str1.length();
    if(str1.charAt(len-1)!='.')
    {
         System.out.println("Not followed the rule...");
         return;
    }
    str1=str1.substring(0,len-1);
    str1=str1 + " ";
     while(true)
     {
         int i=str1.indexOf(' ');
         if(i< 0)
         break;
         str=str1.substring(0,i);
         str1=str1.substring(i+1);
         if(str.equals(str2))
         c++;
      }
   
    System.out.print("\nFrequency of the word = " + c);
  
}

    public static void main(String args[])
    {
        Search ob=new Search();
        ob.assign();
        ob.display();
   }


BlueJ Program On Sentence To Count Frequency Of All Type Characters






Write a program to accept a sentence and show the frequency of uppercase, lowercase, special characters and digits

Sample Input: Jamshedpur Mango Dimna Road:810012
Sample Output: No of uppercase characters: 4
No of lowercase characters: 20
No of special characters: 4
No of digits: 6




import java.util.Scanner;
class Star
{
String str1;
int i,len,c1,c2,c3,c4;
char ch;
Scanner sc=new Scanner(System.in);
public void read()
{
   System.out.print("\nEnter the sentence: ");
   str1=sc.nextLine();
len=str1.length();
for(i=0;i< len;i++)
{
  ch=str1.charAt(i);
  if(Character.isUpperCase(ch))
  c1++;
  else if(Character.isLowerCase(ch))
  c2++;
  else if(Character.isDigit(ch))
  c3++;
  else
  c4++;
}
  System.out.println("\nNo of Uppercase Characters: " + c1);
  System.out.println("\nNo of Lowercase Characters: " + c2);
  System.out.println("\nNo of Special Characters: " + c4);
  System.out.println("\nNo of Digits: " + c3);
}
public static void main(String args[])
{
 Star ob=new Star();
 ob.read();
  }
}


Related Post: BlueJ Programs on String/Sentence

BlueJ Program To Find Word With Maximum And Minimum Vowels From A Sentence






Write a java program to enter a sentence and show the words with maximum and minimum vowels in it. In case of numbers of words with same feature, the first one will be displayed.
import java.util.Scanner;
class Star
{
String str1,s,maxs,mins;
int maxl=0,minl,j;
Scanner sc=new Scanner(System.in);
public void take()
{
System.out.print("\nEnter the sentence:");
str1=sc.nextLine().trim();
str1=str1+ " ";


minl=str1.length();
while(true)
{
 int i=str1.indexOf(' ');
 if(i< 0)
 break;
 s=str1.substring(0,i);
 j=countVowels(s);
 if(j >=maxl)
 {
     maxl=j;
     maxs=s;
    }
    if(j< minl)
 {
     minl=j;
     mins=s;
    }
    str1=str1.substring(i+1);
}
 System.out.println("\nWord With Maximum Vowels = "+maxs);
 System.out.println("\nWord With Minumum Vowels = "+mins);
}
public int countVowels(String s)
{

s=s.toUpperCase();
int i,len,c=0;
len=s.length();
for(i=0;i< len;i++)
{
 switch(s.charAt(i))
 {
      case 'A':
      case 'E':
      case 'I':
      case 'O':
      case 'U':
      c++;
      break;
    }
}
return c;
}
public static void main(String args[])
{
 Star ob=new Star();
 ob.take();
}
}

Sample Input and Output

Enter the sentence: This is a test on vowels counting
Word With Maximum Vowels = counting
Word With Minumum Vowels = This

Related Post: BlueJ Programs on String/Sentence


Delete Duplicate Elements From An Array





import java.util.*;
class Max
{
Scanner sc=new Scanner(System.in);
int a[],b[];
int n,i,j,x;
public void take()
{
System.out.print("\nHow many Elements to Store: ");


n=sc.nextInt();
a=new int[n];
b=new int [n];
for(i=0;i< n;i++)
{
  System.out.print("\nEnter Value: "); 
  a[i]=sc.nextInt();
}
System.out.println("\nArray Elements As Follows.");
for(i=0;i< n;i++)
{
  System.out.print(" "+a[i]); 
 }
}
public void elimination()
{
     x=0;
     for(i=0;i< n;i++)
     {
          for(j=0;j< x;j++)
          {
              if(a[i]==b[j])
              break;
            }
            if(j==x)
            b[x++]=a[i];
        }
        System.out.println("\nArray Elements After Deletion of Duplicate Value");
for(i=0;i< x;i++)
{
  System.out.print(" "+b[i]); 
 }
}
public static void main(String args[])
{
Max ob=new Max();
ob.take();
ob.elimination();
}
}

Sample Input Output

How many Elements to Store: 4

Enter Value: 2

Enter Value: 3

Enter Value: 3

Enter Value: 12

Array Elements As Follows.
 2 3 3 12
Array Elements After Deletion of Duplicate Value
 2 3 12


Related Post: BlueJ Programs on Numeric Array

Sort an array and display the elements in descending order along with its original index






import java.util.*;
class Search
{
    int i,j,temp,n;
    int arr[],arrindex[];
    Scanner sc=new Scanner(System.in);
public void take()
{    
    System.out.print("\nHow many elements to store: ");
     n=sc.nextInt();
     arr=new int[n];
     arrindex=new int [n];
     for(i=0;i< n;i++)
     {
          System.out.print("\nValue: ");
          arr[i]=sc.nextInt();
          arrindex[i]=i; // storing the index of the entered value
     }
 }
    public void show()


    {
        System.out.println("\nEntered Value");
        for(i=0;i< n;i++)
        System.out.print(" "+arr[i]);
        for(i=0;i< n-1;i++)
        {
             for(j=i+1;j< n;j++)
             {
                 if(arr[i]< arr[j])
                 {
                      temp=arr[i];
                      arr[i]=arr[j];
                      arr[j]=temp;
                      // Values Interchanged
                      temp=arrindex[i];
                      arrindex[i]=arrindex[j];
                      arrindex[j]=temp;
                      // indexes Interchanged
                    }
                }
            }
        System.out.println("\nSorted Elements with Original Indexes");
        for(i=0;i< n;i++)
        System.out.println(arr[i] + " " + arrindex[i]);
    }
      
    public static void main(String args[])
    {
        Search ob=new Search();
        ob.take();
        ob.show();
   }
 }


Related Post: BlueJ Programs on Numeric Array

Subscribe via email

Enter your email address:

Delivered by FeedBurner