Skip to main content

Top java programs for fresher



1) Write a program to find a repeated word?


package com.repeat;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

public class RepeatedWords {

public static void main(String[] args) 
{
//Creating wordCountMap which holds words as keys and their occurrences as values
        
        HashMap<String, Integer> wordCountMap = new HashMap<String, Integer>();
     
        BufferedReader reader = null;
         
        try
        {
            //Creating BufferedReader object
             
            reader = new BufferedReader(new FileReader("C:\\sample.txt"));
             
            //Reading the first line into currentLine
             
            String currentLine = reader.readLine();
             
            while (currentLine != null)
            {    
                //splitting the currentLine into words
                 
                String[] words = currentLine.toLowerCase().split(" ");
                 
                //Iterating each word
                 
                for (String word : words)
                {
                    //if word is already present in wordCountMap, updating its count
                     
                    if(wordCountMap.containsKey(word))
                    {    
                        wordCountMap.put(word, wordCountMap.get(word)+1);
                    }
                     
                    //otherwise inserting the word as key and 1 as its value
                    else
                    {
                        wordCountMap.put(word, 1);
                    }
                }
                 
                //Reading next line into currentLine
                 
                currentLine = reader.readLine();
            }
             
            //Getting the most repeated word and its occurrence
             
            String mostRepeatedWord = null;
             
            int count = 0;
             
            //Set<Entry<String, Integer>> entrySet = wordCountMap.entrySet();
             
            for (Entry<String, Integer> entry : entrySet)
            {
                if(entry.getValue() > count)
                {
                    mostRepeatedWord = entry.getKey();  //getting through the key value
                     
                    count = entry.getValue();
                }
            }
             
            System.out.println("The most repeated word in input file is : "+mostRepeatedWord);
             
            System.out.println("Number Of Occurrences : "+count);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();           //Closing the reader
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }    

}//Closing the class


2)Write a program to convert the ArrayToString?


package com.array;

import java.util.List;
import java.util.ArrayList;

public class ArraytoString
{
public static void main(String args[])
{
List al= new ArrayList<String>();
al.add("One");
al.add("Two");
al.add("Three");
al.add("Four");
al.add("Five");
String[] stringArrayObject = new String[al.size()];
al.toArray(stringArrayObject);
for(String temp : stringArrayObject)
System.out.println(temp);
}

}


3)Write a program to create a hotel bill?

  import java.util.Scanner;


public class HotelBill

{
   
    
public static void main(String args[])
{
int t=0,Choice,n1,i=1;
System.out.println(" MENU                   Rate   ");
System.out.println("1.idly                   10      ");
System.out.println("2.poori                  20     ");
System.out.println("3.Dosai                  30      ");
System.out.println("4.Chapthi                40     ");
System.out.println("5.vada                   50      ");
System.out.println("6.Pongal                 60     ");
System.out.println("7.Chicken                70       ");
System.out.println("8.Rice                   80       ");
System.out.println("9.PAN                    90       ");
System.out.println("10.Mutton                100       ");
    Scanner sc = new Scanner(System.in);
    //System.out.println(" Enter the Number of the order: ");
     //Choice=sc.nextInt();
//System.out.println(" Enter the number of plates:");
     //n1 = sc.nextInt();
    while(i==1)
    {
    System.out.println(" Enter the Number of the order: ");
     Choice=sc.nextInt();
System.out.println(" Enter the number of plates:");
     n1 = sc.nextInt();
     switch(Choice)
    {
    
    case 1 :
    t=t+(10*n1);
//     System.out.println("Total:" +t);
    break;
    
    case 2:
    t=t+(20*n1);
       // System.out.println("Total:" +t);
        break;
    
        case 3:
        t=t+(30*n1);
       // System.out.println("Total:" +t);
        break;

        case 4:
        t=t+(40*n1);
    //System.out.println("Total:" +t);
    break;
    case 5:
    t=t+(50*n1);
    //System.out.println("Total:" +t);
    break;
    
    case 6:
    t=t+(60*n1);
       // System.out.println("Total:" +t);
        break;
    
        case 7:
        t=t+(70*n1);
        //System.out.println("Total:" +t);
        break;

        case 8:
        t=t+(80*n1);
    //System.out.println("Total:" +t);
    break;
    case 9:
    t=t+(90*n1);
    //System.out.println("Total:" +t);
    break;
    
       
        case 10:
        t=t+(100*n1);
        //System.out.println("Total:" +t);
        break;

        default:
        //System.out.println();
            break;
}
    System.out.println("Do you want to continue");
    i=sc.nextInt();
    }  
    System.out.println("the total bill:"+t);
}
}

Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. What is synchronization and why is it important?                      With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Withou

All difference Question in java(common difference) (Collections)

1)Difference Between Interfaces And Abstract class? All the methods declared in the Interface are Abstract,where as abstract class must have atleast one abstract method and others may be concrete. In abstract class keyword abstract must be used for method, where as in Interface we need not use the keyword for methods. Abstract class must have Sub class, where as Interface can’t have sub classes.  An abstract class can extend only one class, where as an Interface can extend more than one. 2)Difference between Arrays and Vectors?             Because Vectors are dynamically-allocated, they offer a relatively memory-efficient way of handling lists whose size could change drastically. Arrays are fixed in size and it's wastage of memory problem. A Vector might be a good choice for such an arrangement. Moreover, Vectors have some useful member functions that make coding simpler. While in array is the bad choice of the arrangement and the code become more complex. Vecto

MYSQL Interview question..

MySQL is an open source relational database management system RDBMS based on Structured Query Language SQL MySQL runs on virtually all platforms, including Linux, Unix, Windows. Although it can be used in a wide range of applications, MySQL is most often associated with web-based applications and online publishing and is an important component of an open source enterprise. 1) What is SQL? SQL stands for Structured Query Language. SQL was initially developed at IBM in 1970s. SQL is the standard language to communicate with relational database management systems like Oracle, MS Access, MS SQL Server, MySQL, DB2 etc. It is used to connect the objects with the database . 2)what is the Purpose of SQL? • SQL is used to Create New Databases. • SQL is used to Create New Tables in a Database. • SQL is used to Insert, update, Delete records in a Database. • SQL is used to Retrieve data from a Database. • SQL is used to execute queries against a Database. • SQL can set permis