String methods

Sunday, 28 June 2015
Posted by Unknown

class StringDemo
{
public static void main (String args[])
{
String str = "I am vijay";
System.out.println("The string is: " + str);
System.out.println("The string in upper case: " + str.toUpperCase());
System.out.println("Length of this string: " + str.length());
System.out.println("The character at position 5: "+ str.charAt(5));
System.out.println("The index of the character y: " + str.indexOf('y'));
System.out.println("The substring from 2 to 10: " + str.substring(2, 10));
System.out.println("The index of the beginning of the substring vijay: " + str.indexOf("vijay"));
}
}

Output:

The string is: I am vijay
The string in upper case: I AM VIJAY
Length of this string: 10
The character at position 5: v
The index of the character y: 9
The substring from 2 to 10: am vijay
The index of the beginning of the substring vijay: 5

class arithmetic
{
    public static void main(String args[])
    {
        short x = 8;
        int y = 2;
        float a = 10.5f;
        float b = 6f;
        System.out.println("Addition: "+(x+y));
        System.out.println("Subtraction: "+(x-y));
        System.out.println("Multiplication: "+(x*y));
        System.out.println("Division: "+(x/y));
        System.out.println("Modulus: "+(x%y));
        System.out.println("Floating point Division: "+(a/b));
    }
}

Output:

Addition: 10
Subtraction: 6
Multiplication: 16
Division: 4
Modulus: 0
Floating point Division: 1.75

Print Odd Numbers

Posted by Unknown
Tag :

class printOdd
{
    public static void main(String args[])
    {
        for(int x=1; x<10; x=x+2)
        {
        System.out.println(x);
        }
    }
}

Output:

1
3
5
7
9

Print Even Numbers

Posted by Unknown
Tag :

class printEven
{
    public static void main(String args[])
    {
        for(int x=0; x<10; x=x+2)
        {
        System.out.println(x);
        }
    }
}

Output:

8

The for Loop

Posted by Unknown
Tag :

Syntax:

for(initialization; condition; iteration) 
statement;

Program:

class printNumber
{
    public static void main(String args[])
    {
        for(int x=0; x<10; x++)
        {
        System.out.println(x);
        }
    }
}
 

Output:

0
1
2
3
4
5
6
7
8
9

Syntax:

if(condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else 
statement 4;

Grade System Program:

import java.util.Scanner;
class result
{
    public static void main(String[] args)
    {
           int mark;
           Scanner s=new Scanner(System.in);
           System.out.print("Enter your mark: ");                   
           mark=s.nextInt();
           if ( mark >= 90 )
           {
            System.out.println( "Grade A" );
           }
           else if ( mark >= 80 )
           {
            System.out.println( "Grade B" );
           }
           else if ( mark >= 70 )
           {
            System.out.println( "Grade C" );
           }
           else if ( mark >= 60 )
           {
            System.out.println( "Grade D" );
           }
           else if ( mark >= 50 )
           {
            System.out.println( "Grade E" );
           }
           else if ( mark >= 40 )
           {
            System.out.println( "Grade F" );
           }
           else 
           {
            System.out.println( "Failed" );
           }    
    }
}

Output:

Enter your mark: 56 
Grade E 

Enter your mark: 10 
Failed

Syntax:

if(condition)
statement 1;
else
statement 2;

Program:

import java.util.Scanner;
class result
{
    public static void main(String[] args)
    {
           int mark;
           Scanner s=new Scanner(System.in);
           System.out.print("Enter your mark: ");                   
           mark=s.nextInt();
           if ( mark <= 60 )
           {
                System.out.println("Fail");
           }
            else
           {
                System.out.println("Pass");
           }
    }
}

Output:

Enter your mark: 23
Fail
Enter your mark: 56
Pass
Welcome to My Blog

Translate

Popular Post

Total Pageviews

- Copyright © Learning Java Program - Powered by Blogger -