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...

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.java package com.img.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.img.dao.ConnectionDAO; import com.img.pojo.Film_TablePojo; import com.img.pojo.imagePojo; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport { List<Object> values = new ArrayList<Object>();  public List<Object> getValues() { return values; } public void setValues(List<Object> values) { this.values = values; } /*HttpServletRequest request;     public void setServletRequest(HttpS...

Top Most Important Multithreading interview question and answers

1) What is Thread in java? Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application. Thread are light weight process. It belongs to java.lang package. It can run mutiple threads simutaneously .                                                               2) How to implements threads in java?         By   implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.   Thread creation by implementing java.lang.Runnable interface.      And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object          Thread threa...