BlueJ: Time in words

/** * The following class time displays the time in words. (eg 5:45 will be displayed as Quater to Six) * @author (Gulzar Ahmed)  */


SOURCE CODE WRITTEN IN BLUEJ


import java.io.*;
class time
{
static int hour,minute;
time()//default constructor
{
hour=minute=0;
}
String arr_hour[]={"","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","One"};
String arr_na[]={"","Quater","Half","Quater"};
String teens[]={"","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninety"};
String end_with_zero[]={"","Ten","Twenty","Thirty","Fourty","Fifty"};
void accept()throws IOException
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(read);     
System.out.print("HOUR :");
hour=Integer.parseInt(br.readLine());
System.out.print("MINUTES :");
minute=Integer.parseInt(br.readLine());   
 }
void display()throws IOException
{
System.out.println("Time : "+hour+":"+minute);
        if(minute==0)//Special Case
        System.out.println(arr_hour[hour]+" O'clock");
        if(minute>30)
        System.out.println(convert(60-minute)+" to "+arr_hour[hour+1]);
        else
        System.out.println(convert(minute)+" past "+arr_hour[hour]);
       }
String convert(int m)
    {
       if(m%15==0)
           return arr_na[m/15];
           else
           {
             if(m==1||m==0)
             return arr_hour[m]+" minute";
             if(m>1&&m<=9)
             return arr_hour[m]+" minutes";
             if(m%10==0)
             return end_with_zero[m/10]+" minutes";
             if(m>=11&&m<=19)
             return teens[m%10]+" minutes";
             if(m%10!=0)
             return end_with_zero[m/10]+" "+arr_hour[m%10]+" minutes";
            }
            
        return "Fatal ERROR";   
    }
public static void main()throws IOException
{
time ob =new time();
ob.accept();
if(minute>=60||minute<0||hour>12||hour<1)
System.out.println("Wrong INPUT");
else
ob.display();
}
}

OUTPUT:


Write a program to input an encoded sentence having only sequence of ASCII values without any spaces. Decoded the encoded text and print in the form of a statement.

/**
 * The class decode decodes the encrypted text eneterd by the user
*/
 
SOURCE CODE :
 
import java.io.*;
class decode
{
static String str1;
static String str=new String();
static String dec=new String();
static int l;
 
decode()
{
l=0;
str="";
}
static void accept()throws IOException
{
InputStreamReader read= new InputStreamReader(System.in);
BufferedReader br= new BufferedReader(read);
l=0;
System.out.print("Enter the Encoded message : ");//2312179862310199501872379231018117927
str1=br.readLine();
for(int i=str1.length()-1;i>=0;i--)
str=str+str1.charAt(i);
System.out.println("Reversed message : "+str);//7297118101329732781059910132689712132
System.out.println("the length of the string is "+str.length());
}
 static void Decrypt()
    {
        
        int c=0;
        
        while(c<str.length())
        {
        if(str.charAt(c)!='1')
        {
            
            
            dec=dec+((char)(Integer.parseInt(str.substring(c,c+2))));
            c+=2;
        }
        else
        {
            
            dec=dec+((char)(Integer.parseInt(str.substring(c,c+3))));
            c+=3;
        }
        }
    }
        static void display()
        {
            System.out.print("The decrypted code is : "+dec);
        }
    public static void main()throws IOException
    {
        decode ob=new decode();
        ob.accept();
        ob.Decrypt();
        ob.display();
    }
}
        
Algorithm :
Start
Step1: Accept the encoded text from the user
Step2: Reverse the encoded code using for loop statement
Step3: Start a conditioned while-loop. Give the condition as c(counter)< string length .
Step4: Extract each character and check if it begins with one or not. If yes go to step 5 else go to step 6.
Step5: Extract the first three digit of the code using substring function and explicitly convert from string to character. Increase the counter by three.
Step6: Extract the first two digits of the code using substring function and explicitly convert from string to character. Increase the counter by two.
Stop
 

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