Sunday, 26 April 2015

A PROGRAM IN JAVA TO FIND THE NUMBER OF SENTENCES,VOWEL , AND WORD IN A STRING!!!

/* In this program , we are taking out the number of vowels, words and sentences in a paragraph
 * We first run a loop to take out each character of the paragraph
 * Then we check whether the it is the end of the sentence which is marked by '!' , '.' or '?'
 * If it is the end of the sentence , then we take out each word from the sentence
 * From each word we extract each alphabet
 * Then we check whether the alphabet is a vowel or not
 * So here we go :
 */    

class Parts_of_a_paragraph//Here have assumed that there is no space between the ending alphabet of the sentence and '.' , '?' or '!' and also between the first alphabet of the next sentence
    {
        void main(String paragraph)
        {
          paragraph = paragraph+" ";
            int len = paragraph.length() , no_sentence = 0 , no_vowel = 0 , no_word = 0 ;
            String word = "" ;
            String sentence ="" ;
            char character , var , ch;
            System.out.println("Sentence   No. of vowels   No. of words");
            for(int i = 0 ; i<len ; i++)
            {
                character = paragraph.charAt(i);//To extract the each character from the paragraph
                if((character=='.')||(character=='!')||(character=='?'))//check whether it is the end of the                           sentence
                {
                    no_sentence++;
                    sentence = sentence+" ";
                    for(int j = 0 ; j<(sentence.length()) ; j++)
                    {
                       var = sentence.charAt(j);//To extract each character from the sentence
                       if(var==' ')//to check whether it is the end of the word
                        {
                           word = word +" ";
                           no_word++;
                           for(int v = 0 ; v<(word.length()) ; v++)
                            {
                                ch = word.charAt(v);//To extract each character from the word
                               if((ch=='a')||(ch=='A')||(ch=='E')||(ch=='e')||(ch=='I')||(ch=='i')||(ch=='o')||(ch=='O')||                                 (ch=='u')||(ch=='U'))//to check whether ch is a vowel
                                {
                                    no_vowel++;
                                    }
                                else
                                ;//a null statement
                            }
                            word = "";
                               }
                        else
                        {
                            word = word + var ;
                        }
                            }
                        System.out.println(" "+no_sentence+"  \t   "+no_vowel+"             "+no_word);
                        no_vowel = 0 ; no_word = 0 ;
                        sentence = "";
                        }
                        else
                                 sentence = sentence + character;
                            }
                        }
                    }
        PLEASE GIVE SUGGESTIONS AND ASK  QUESTIONS DOWN IN THE COMMENTS SECTION

Thursday, 23 April 2015

A PROGRAM TO GENERATE RECEIPT AS IN A GENERAL STORE AFTER SCANNING THE BARCODE !!!

/* Wonder how are receipts generated in a General Store after Scanning the barcode ??
 * So here is what to do :
 * 1. Initialise 2 arrays of product name and price.
 * Remember the codes entered are respective index numbers of the elements in the array eg. 0 for Ear phones , 1 for Headphone , etc
 * Using Switch Case to check for matching the codes
 * We have to calculate discount on products with codes {2,4,5,7,8,9}
 * I have first calculated the final discount and subtracted it at the last, while printing
 * We also have to find out the discount on each product liable for discount
 * The Discount is liable only the product is bought in multiple quantities
 * So here it is :
   */   

My Input
 class Receipt_Generator
    {
        void main(int code[] , int quantity[

],String date)//Taking parameterized input
        {
            double cost = 0.0 , dis=0.0, final_discount=0.0;
            int c =0;
            String product_name[] = {"Ear phones" , "Headphone" , "Tablet    " , "Memory Card" , "Hard disk" , " Phone " , "Wrist Watch " ,"Laptop    ","Television","Desktop"};//An array of each Array
            int price[] = {20 , 100 , 800 , 250 , 500, 700 , 200 ,3000,10000,2500};//Price of each Product
            //We have discounts on the following products = {2,4,5,7,8,9};
            for(int i = 0 ;i<code.length;i++)
            {
            switch (code[i])//The codes are respective index numbers of the elements in the array eg. 0 for Ear phones , 1 for Headphone , etc
            {
             
                case 0:
                cost += quantity[i]*20.0;
                break ;

                case 1:
                cost += quantity[i]*100.0;
                break ;
             
                case 2:
                c = (quantity[i]*800);
                 if((c>=1600)&&(c<2400))
                dis = (10/100.0)*c ;
                else if(c>=2400&&c<=32000)
                dis = (15/100.0)*c;
                else if(c>3200)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount ;
                cost+=c;
                break ;
                case 3:
                cost += quantity[i]*250.0;
                break;
                case 4:
                c = (quantity[i]*500);
              if((c>=1000)&&(c<1500))
                dis = (10/100.0)*c ;
                else if(c>=2000&&c<=25000)
                dis = (15/100.0)*c;
                else if(c>3000)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount ;
                cost+=c;
             
                break ;
                case 5:
                c = (quantity[i]*500);
                if((c>=1000)&&(c<1500))
                dis = (10/100.0)*c ;
                else if(c>=2000&&c<=2500)
                dis = (15/100.0)*c;
                else if(c>3000)
                dis = (20/100.0)*c;
             
                final_discount=dis+final_discount;
                cost+=c;
                break ;
                case 6:
                cost += quantity[i]*700;
                break;
                case 7:
                 c = (quantity[i]*200);
                if((c>=400)&&(c<600))
                dis = (10/100.0)*c ;
                else if(c>=600&&c<=800)
                dis = (15/100.0)*c;
                else if(c>1000)
                dis = (20/100.0)*c;
                final_discount = dis+final_discount;
                cost+=c;
             
                break ;
                case 8:
                c = (quantity[i]*10000);
                if((c>=20000)&&(c<30000))
                dis = (10/100.0)*c ;
                else if(c>=30000&&c<=40000)
                dis = (15/100.0)*c;
                else if(c>50000)
                dis = (20/100.0)*c;
                final_discount=dis+final_discount;
                cost+=c;
             
                break ;
                case 9:
             
                c = (quantity[i]*2500);
                if((c>=5000)&&(c<7500))
                dis = (10/100.0)*c ;
                else if(c>=7500&&c<=10000)
                dis = (15/100.0)*c;
                else if(c>12500)
                dis = (20/100.0)*c;
                final_discount=dis+final_discount;
             
                cost+=c;
             
                break ;
                default :
                System.out.println("WRONG CHOICE");
                System.out.println("RE-ENTER THE CODE");
            }
        }
       System.out.println("Date: "+date);
       System.out.println("###########################################YOUR BILL###############################################");
        System.out.println("Product ID \t Product Name \t Price \t Quantity \t Total Cost");
        for(int j = 0 ; j<quantity.length;j++)//for printing all the items
        {
             
        System.out.println(code[j]+"         \t "+product_name[code[j]]+"\t"+price[code[j]]+"   \t"+quantity[j]+"     \t \t"+(price[code[j]]*quantity[j]));
        }
        System.out.println("Total Cost: "+cost+"$");
        System.out.println("Total discount: "+final_discount+"$");
        System.out.println("Grand Total :"+(cost-final_discount)+"$");
        System.out.println("***********************THANK YOUR FOR SHOPPING***********************");
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$SHOP AGAIN$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        System.out.println(" \n");
    System.out.println("________________________________");
    System.out.println("           SIGNATURE");
    }
}
My Output
PLEASE GIVE SUGGESTIONS AND ASK  QUESTIONS DOWN IN THE COMMENTS SECTION

Tuesday, 14 April 2015

CALCULATE DIFFERENCE BETWEEN TWO DATES WITHOUT USING CALENDAR CLASS

/* 
 * Let 1st date be dd/mm/yy 
 * let 2nd date be da/ma/ya
 * Whether latest date is entered first or not it does not make a difference 
 */    
My Input


public class Date
    {
        int date , month , year ;
        Date( int dt , int mn , int yr)// A parameterized Constructor to initialize vslues
        {
            date = dt ;
            month = mn ;
            year = yr ;
        }
        void main(int dd , int mm , int yy , int da , int ma , int ya )//Taking parameterized input
        {
               Date obj = new Date(dd , mm , yy);//creating an object of the first Date
               Date obj2 = new Date(da , ma , ya);//creating an object of the second Date
               int sum = obj.date+obj.month*30+obj.year*365;//calculating total number of days in the 1st date
               int sum2 = obj2.date+obj2.month*30+obj2.year*365;//calculating total number of days in the 2st date
               int diff = sum - sum2 ;//taking difference of the two date in terms of days
               int years = diff/365;
               int rem1 = diff%365 ;
               int months = rem1/30 ;
               int days = rem1%30 ;
               if(days<0)// if the latest date is entered in 2nd Date
               {
                   days = days*-1;
                   months = months*-1;
                   years = years*-1;
                }
               System.out.println("Difference in Days = "+days+" Months = "+months+" Years = "+years);//printing the output
            }
        }
     
My Output

Friday, 10 April 2015

INPUT A LOCATION OF A FILE AND PRINT THE PATH , NAME AND EXTENSION OF THE FILE

/* eg : INPUT --> "C:/program files/new program/path/show path/example.prg"
  ----------------------------------------------------------------------------
  OUTPUT:
  PATH --> C:/program files/new program/path/show path/
  FILE NAME --> example
  EXTENSION --> prg 
  */

import java.util.*;//I am using Scanner class to take the input
class location
{
    Scanner in = new Scanner(System.in);
    void main()
    {
        System.out.println("Enter the Location of a file");
        String p = in.next();//to input the location
        int index_file = p.lastIndexOf('/');//to store the last index of '/' in the location
        String file_with_extention = p.substring(index_file+1);//to store the file and extention  
        int index_dot = file_with_extention.lastIndexOf('.');//to sore the last index of '.' in order to find the extention
        String path = p.substring(0,index_file+1);//to take out the path of the file
        String extension = file_with_extention.substring(index_dot+1);//to sore the extension of the file
        String file = file_with_extention.substring(0,index_dot);//to store the name of file
      System.out.println("Path: "+path);
    System.out.println("File Name: "+file);
    System.out.println("Extension: "+extension);
    }
}
My Computation

Tuesday, 7 April 2015

INPUT A STRING AND CHANGE IT INTO PIG LATIN

import java.util.*;//I am using SCANNER CLASS TO TAKE INPUT
class piglatin
    {
        public void main()
        {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the String that is to be converted to PIGLATIN");
            String nm = in.next();//Taking INPUT from the user
            int check = -1 ;
             nm = nm.toUpperCase();//Converting String 'nm' to Upper case
             for(int i = 0 ; i<nm.length();i++)
             {
                 char ch = nm.charAt(i);//Extracting every character from the String      
                 switch (ch)//Checking whether that character is a vowel or not          
                 {
                     case 'A':case 'E':case 'I':case 'O' :case 'U'://Using FALL THROUGH
                     check = nm.indexOf(ch);//Taking Out the index of the vowel
                     break;
                   
                    }
                    if(check>-1)//The index would be Positive indicating that a vowel has been found
                    break;
                 
                }
             
                String before_vowel = nm.substring(0,check);//Taking the part of String before the vowel  
                String after_vowel = nm.substring(check);//Taking the part of String after the vowel[including the vowel]
                String pigltn = after_vowel+before_vowel+"AY";
                System.out.println("The PIGLATIN WORD OF "+nm+" is "+pigltn);
        }
    }