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
Post a Comment