Friday, May 13, 2016

HashMap in java

package com.home;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo1 {
   
public static void main(String[] args) {
Map<Integer, String> a = new HashMap<Integer, String>();

a.put(1, "hari");
a.put(2, "Gopal");
a.put(3, "RAm");
a.put(4, "Mohan");
a.put(5, "Daro");

int size = a.size();

System.out.println(size);


a.remove(1);
System.out.println(a);

Set set = a.entrySet();

Iterator it = set.iterator();

while(it.hasNext()){

Map.Entry me = (Map.Entry) it.next();

System.out.println("This is key = "+me.getKey()+" : "+"This is value "+me.getValue());
}
}
}
The Output of the Following Program is:

5
{2=Gopal, 3=RAm, 4=Mohan, 5=Daro}
This is key = 2 : This is value Gopal
This is key = 3 : This is value RAm
This is key = 4 : This is value Mohan
This is key = 5 : This is value Daro

vector in java

package com.home;

import java.util.Iterator;
import java.util.Vector;

public class VectorDemo {

public static void main(String[] args) {

Vector obj = new Vector();

obj.add(12);
obj.add("ram");
obj.add(1);



//System.out.println(obj);

/* Iterator it = obj.iterator();

while(it.hasNext()){
System.out.println(it.next());
}
*/

for(int i=0;i<obj.size();i++){

System.out.println(obj.get(i));
}

}

}


The Output of the Following Program is:
12
ram
1

java arraylist

package com.home;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;



public class ArrayLIstDemo {


public static void main(String[] args) {

ArrayList<String> alist = new ArrayList<String>();

alist.add("ram");
alist.add("hari");
alist.add("gopal");
alist.add("barma");




Iterator it = alist.iterator();
while(it.hasNext()){
System.out.println(it.next());

}





}
}

The Output Of the above program is:
ram
hari
gopal
barma

Tuesday, December 29, 2015

Java program to find greatest number in array


Java program to find greatest number in array:



package arraydemo;


public class ArrayDemo {

    
    public static void main(String[] args) {
        
        
                
                int numbers[] = new int[]{11,12,13,44,55,66,77,88,99,23};
               
                
                int small = numbers[0];
                int large = numbers[0];
               
                for(int i=1; i< numbers.length; i++)
                {
                        if(numbers[i] > large)
                                large = numbers[i];
                        else if (numbers[i] < small)
                                small = numbers[i];
                       
                }
               
                System.out.println("Largest Number is : " + large);
                System.out.println("Smallest Number is : " + small);
        }
}
OUTPUT:
Largest Number is : 99

Smallest Number is : 11

Monday, December 28, 2015

Simple Java Program to calculate Restaurant Bill

 Simple Java Program to calculate Restaurant Bill:




package restaurants;

import java.util.Scanner;


public class Restaurants {

    
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        boolean quit= false;
        int sum=0;
        int wine=200,cold=20,bear=400,juice=100;
        String order="";
        
        do{
            System.out.println("ITEM"+"\t\tPrice");
            System.out.println("1.Wine"+"\t\t"+"200");
            System.out.println("2.ColdDrink"+"\t"+"20");
            System.out.println("3.Bear"+"\t\t"+"400");
            System.out.println("4.Juice"+"\t\t"+"100");
            System.out.println("5.Quit");
            
            int choice=input.nextInt();
            
            switch(choice){
                case 1:System.out.println("Wine"+"\n");
                        sum=sum+wine;
                        order=order.concat("wine"+"\n");
                        
                    break;
                case 2:
                    System.out.println("ColdDrink");
                    sum=sum+cold;
                    order=order.concat("ColdDrink"+"\n");
                    
                       break;
                case 3:
                    System.out.println("Bear");
                    sum=sum+bear;
                    order=order.concat("Bear"+"\n");
                      break;
                case 4:
                    System.out.println("Juice");
                    sum=sum+juice;
                    order=order.concat("Juice"+"\n");
                    break;
                case 5:
                     quit=true;
                     
                    break;
                default:
                    System.out.println("Wrong input");
            }
        
        }while(!quit);
       
        System.out.println("Your Orders are:\n"+order);
        System.out.println("Your total bill="+sum);
        
         System.out.println("Thank you");
    
        
        
        
    
    }
    
}


OUTPUT:
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
1
Wine

ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
2
ColdDrink
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
3
Bear
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
4
Juice
ITEM Price
1.Wine 200
2.ColdDrink 20
3.Bear 400
4.Juice 100
5.Quit
5
Your Orders are:
wine
ColdDrink
Bear
Juice

Your total bill=720
Thank you

Sunday, December 27, 2015

Java KingKong Program

Java KingKong Program:



package javaapplication16;


public class KingKongDemo {
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
        
            if(i%3==0){
                System.out.println(i+"King");
            }
            else if(i%5==0){
                System.out.println(i+"kong");
            }
            else if(i%3==0 && i%5==0){
                System.out.println("KingKong");
            }else{
                System.out.println(i);
            }
        }
    }
}

OUTPUT:

1
2
3King
4
5kong
6King
7
8
9King
10kong
11
12King
13
14
15King
16
17
18King
19
20kong
21King
22
23
24King
25kong
26
27King
28
29
30King
31
32
33King
34
35kong
36King
37
38
39King
40kong
41
42King
43
44
45King
46
47
48King
49
50kong



Relational Operator

Relational Operator:


Operators                             Result

==                                          Equql to

!=                                           Not Equal to

>                                            Greater Than

<                                             Lesser than

>=                                           Greater than or equal to

<=                                           Lesser than or equal to  




package javaapplication16;


public class RelationalOperatorDemo {
    public static void main(String[] args) {
        int a=1;
        int b=2;
        int c=1;
        if(a==c){
            System.out.println(" a is Equal to c"+a);
        }else{
            System.out.println("a is not equal to c");
        }
        
        if(a!=b){
            System.out.println("a is Not Equal to b");
        }
        
        if(a>b){
            System.out.println("a is greater than b");
        }
           if(a<b){
               System.out.println("a is less than b");
           }  
           
           if(a>=b){
               System.out.println("a is greater or eual to b");
           }else{
            System.out.println("a is  not greater or eual to b");
           }
           
           if(a<=b){
               System.out.println("a is greater or equal to b");
           }else{
           System.out.println("a is not greater or equal to b");
           }
    }
}

Output:
 a is Equal to c1
a is Not Equal to b
a is less than b
a is  not greater or eual to b
a is greater or equal to b