Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

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

Arithmetic Operators

Arithmetic Operators:




package javaapplication16;


public class ArithemiticOperatorDemo {
    public static void main(String[] args) {
        int a=10;
        int b=5;
        
        int c=a+b;
        int d=a/b;
        int e=a*b;
        int f=a%b;
        
        System.out.println("Arithmetic Operator Operation");
        System.out.println("Addition = "+a);
        System.out.println("Division = "+b);
        System.out.println("Multiplification = "+e);
        System.out.println("Modulus = "+f);
    }
}

OUTPUT:
Arithmetic Operator Operation
Addition = 10
Division = 5
Multiplification = 50
Modulus = 0

Increment And Decrement Operator

Increment and Decrement Operator:


package javaapplication16;


public class IncrementAndDecrementOPeratorDemo {
    public static void main(String[] args) {
        int a=5;
        int b=3;
        int c;
        int d;
        
        c= ++b;
        d=++a;
        c++;
        
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("d= "+d);
    }
}

OUTPUT:
a = 6
b = 4
c = 5
d= 6

Saturday, December 26, 2015

Java Program to add two Matrix

Java Program to add two Matrix:


package javaapplication22;

import java.util.Scanner;


public class AddTwoMatrix {
    public static void main(String[] args) {
       Scanner s = new Scanner(System.in);

       System.out.print("Enter number of rows: ");
       int rows = s.nextInt();

       System.out.print("Enter number of columns: ");
       int cols = s.nextInt();

       int[][] a = new int[rows][cols];
       int[][] b = new int[rows][cols];

       System.out.println("Enter the first matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               a[i][j] = s.nextInt();
           }
       }
       System.out.println("Enter the second matrix");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               b[i][j] = s.nextInt();
           }
       }
       int[][] c = new int[rows][cols];
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               c[i][j] = a[i][j] + b[i][j];
           }
       }
       System.out.println("The sum of the two matrices =");
       for (int i = 0; i < rows; i++) {
           for (int j = 0; j < cols; j++) {
               System.out.print(c[i][j] + " ");
           }
           System.out.println();
       }
   }
}

Output:
Enter number of rows: 2
Enter number of columns: 2
Enter the first matrix
1 2
3 4
Enter the second matrix
1 4
3 5
The sum of the two matrices =
2 6 
6 9 

Java BizzBuzz Program

Java BizzBuzz Program:


package javaapplication22;


public class BizzBuzzDemo {
   
    public static void main(String[] args) {
        for(int i=1;i<=50;i++){
            if(i%3==0){
                System.out.println("Bizz"+i);
            }else if(i%5==0){
                System.out.println("Buzz"+i);
            }else if(i%3==0 && i%5==0){
                System.out.println("BizzBuzz"+i);
            }else{
                System.out.println(i);
            }
       
        }
    }
}
OUTPUT:


1
2
Bizz3
4
Buzz5
Bizz6
7
8
Bizz9
Buzz10
11
Bizz12
13
14
Bizz15
16
17
Bizz18
19
Buzz20
Bizz21
22
23
Bizz24
Buzz25
26
Bizz27
28
29
Bizz30
31
32
Bizz33
34
Buzz35
Bizz36
37
38
Bizz39
Buzz40
41
Bizz42
43
44
Bizz45
46
47
Bizz48
49
Buzz50


IndexOf() and last indexOf()

Searching Strings:

1)IndexOf() = searches for the first occur of a character 
                           int indexOf(int ch);

2)last indexOf()=searches for the last occur of a character 
                              int lastIndexOf(int ch);






package stringdemo;


public class IndexOfAndlastIndexOfDemo {
    
    public static void main(String[] args) {
        String s="I am a bad guy";
        
        System.out.println(s);
        System.out.println("indexOf(a)"+s.indexOf('a'));
        System.out.println("lastindexOf(g)"+s.lastIndexOf('a'));
    }
    
}
Output:
I am a bad guy
indexOf(a)2
lastindexOf(g)8

Friday, December 25, 2015

length() and capacity() method

StringBuffer:



length() and capacity()

The length of the StringBuffer can be found by the length() method.while the total capacity can be found through the capacity() method.They have the following forms:

                      int length();
                      int capacity();




package stringdemo;


public class LengthAndCapacity {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("Dance");
        
        System.out.println("Length="+s.length());
        System.out.println("Capacity"+s.capacity());
    }
}
OUTPUT:
Length=5
Capacity21

Here Length is 5 and capacity is 21 because room for 16 additional characters is automatically added,so result is 21.

delete() and deleteCharAt() method

delete() and deleteCharAt() method:

Java  provides us to StringBuffer the ability to delete character using the method delete() and deleteCharAt(). These method are shown here:

         StringBuffer delete(int startIndex,int endIndex)
         StringBuffer deleteCharAt(int loc)

Here is a small program to demonstrate these method:


package stringdemo;


public class DeleteDemo {
    public static void main(String[] args) {
        StringBuffer s= new StringBuffer("I love you");
        
        s.delete(2, 5);
        System.out.println("After delete:"+s);
        
        s.deleteCharAt(0);
        System.out.println("After deleteCharAt:"+s);
    }

}

OUTPUT:
After delete:I e you
After deleteCharAt: e you

Java program to show number pyramid

Number Pyramid:


package diamond;


public class PyramidDemo {
    public static void main(String[] args) {
        for(int i=0;i<5;i++) {
         for(int j=0;j<5-i;j++) {
             System.out.print(" ");
         }
        for(int k=0;k<=i;k++) {
            System.out.print("* ");
        }
        System.out.println();
    }

}
}


OUTPUT:
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 

Fibonacci Using Recusion method

Fibonacci Using Recusion method:

package diamond;

import static diamond.Fibonacci.fibonacciRecusion;


public class FibonacciDemo {
    public static void main(String[] args) {
        System.out.println("\nFibonacci by recusion");
        int febCount=15;
    for(int i=1; i<=febCount; i++){
      System.out.print(fibonacciRecusion(i) +" ");
    }
       
   
    }
}
The output of this program is as:
Fibonacci by recusion
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

Thursday, December 24, 2015

replace() method

Modifying a String:


replace();

    The replace method is used to replace the one character of string.It has the general form:
      
String replace(char original,char replace);


Here is a small program to show how replace() method works:


package stringdemo;


public class ReplaceDemo {
    public static void main(String[] args) {
        String s="HelloWorld";
       
        System.out.println(s.replace("H", "W"));
    }
}
Output:

WelloWorld


concat() and trim() method

concat()

You can concat two String using concat() as shown below:


package stringdemo;


public class Concat {
    public static void main(String[] args) {
        String s1="ram";
        String s2="john";
        
        System.out.println(s1.concat(s2));
    }

}
The output of the above program is:
ramjohn



trim()

trim() method of String in java is used to remove whitespace.It has the general form:
String trim();
Here is a small program to demonstrate trim()method:

package stringdemo;


public class Trim {
    public static void main(String[] args) {
        String s="  Hello World";
        
        System.out.println(s.trim());
    }
}
The output of the above program is:
Hello World

equals() Versus ==

equals() Versus ==

We should be clear that equals() method and == operator perform two different operations.The equals() method compares the character inside a string whereas == operator compares two object reference to see whether they refer to the same instances.Here is a small program to demonstrate the difference between equals()method vs == operator:


package stringdemo;


public class Equal {
    public static void main(String[] args) {
        String s1="john";
        String s2=new String(s1);
       
        System.out.println(s1.equals(s2));
        System.out.println(s1==s2);
    }
}
The above program gives the following output:
true
false

equals() and equalsIgnoreCase()

String Comparision:

The String class includes many methods that compares Strings or Substrings within strings.

equals() and equalsIgnoreCase()


package stringdemo;


public class StringComparision {
    public static void main(String[] args) {
        String str="john";
        String str1="john";
        String str2="JOHN";
        String str3=new String("john");
        
        
        if(str==str1){
            System.out.println("Equal");
        }else{
        System.out.println("Not Equal");
        }
        
        if(str.equals(str1)){
            System.out.println("str is equal to str1");
        }else{
        System.out.println("str is not equal to str1");
        }
        
        if(str==str2){
        System.out.println("str is equal to str2");
        }else{
        System.out.println("str is not equal to str2");
        }
        
        if(str.equals(str2)){
            System.out.println("equals");
        }else{
        System.out.println(" not equals");
        }
        if(str.equalsIgnoreCase(str2)){
        System.out.println("equalsss");
        }else{
        System.out.println(" not equalsss");
        }
        
        if(str1==str3){
            System.out.println("str1 is equal to str3");
        }else{
        
        System.out.println("str1 is not  equal to str3");
        }
        
        if(str.equals(str3)){
            System.out.println("bang");
        }else{
            System.out.println("not bang");
        }
        }
    }

   
    The output of the above program is as below:

Equal
str is equal to str1
str is not equal to str2
 not equals
equalsss
str1 is not  equal to str3
bang





Character Extraction

Character Extraction:


2)getChars()


If you want to extract more than one character at a time,you can use the getChars() method.
It has general form:
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart);


package stringdemo;


public class NewClass {
    public static void main(String[] args) {
       
      String s="This is the demo of getChar()method";
      int start=5;
      int end=8;
       char ch[]=new char[end-start];
     
       s.getChars(start, end, ch, 0);
        System.out.println(ch);
    }
}
The output is:
is 

Character Extraction

Character Extraction:


1) charAt()


charAt() method is used to extract single character from String.Here is a small program to demonstrate chatAt() method.  


package stringdemo;


public class NewClass {
    public static void main(String[] args) {
       
      String  ch="java";
        System.out.println(ch.charAt(1));
    }
}

The output of this program is as:
a

String Concatenation with other data type

String Concatenation with other data type:

package stringdemo;


public class StringS {
    public static void main(String[] args) {
        String s="nine:"+4+4;
        System.out.println(s);
    }
}

Here output will be like 
nine:44.
   rather than
nine:8

To get 2nd type of output we must change the 
String s="nine:"+(4+4);

This will generate the output as:
nine:8