Thursday, June 9, 2011

Core Java II

A Program Using Switch-Case To Print Weekdays

class week
{
public static void main(String arg[])
{
int a;
a=Integer.parseInt(arg[0]);

switch(a)
{
case 0:
System.out.println("Monday");
break;
case 1:
System.out.println("Tuesday");
break;
case 2:
System.out.println("Wednesday");
break;
case 3:
System.out.println("Thursday");
break;
case 4:
System.out.println("Friday");
break;
case 5:
System.out.println("Satday");
break;
case 6:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid Input, enter Input between 0-6: ");
break;
}
}
}

Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac week.java Press Enter
* C:\>;java week 0 Press Enter



Two Programs Showing Difference Between While and Do-While Statements


Using While Statement:

class whil
{
public static void main(String[] arg)
{
int i=0;
while(i!=5)
{
System.out.println(i+"\t Google");
i++;
}
}
}

Using Do-While Statements:

class dowhil
{
public static void main(String[] arg)
{
int i=0;
do
{
System.out.println(i+"\t Google");
i++;
}
while(i!=5);
}
}

Use notation:
for While:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac whil.java Press Enter
* C:\>;java whil Press Enter
 Output:
for do-while:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dowhil.java Press Enter
* C:\>;java dowhil 0 Press Enter
Output:


A Small Discussion On How to store a word inside a variable called array:

Array: Array is a collection of similar elements stored inside a common variable.
Suppose 'a' is an array type variable,
'a' can store either integer type or character type values,
For example: a[]={'d','i','p'};
a[]={1,2,3};
Types of Array:
  1. Single Dimension, for eg: a[]={1,2,3};
  2. Multi-Dimension for eg a[][]={{1,2,3},{4,5,6},{7,8,9}}
A Program demonstrating a Single Dimensional Array:

class arr
{
public static void main(String arg[])
{
int a[]={30,20,12,11,56},i,j,k,n,temp;
for(i=0;i<5;i++)
{
System.out.println("Array data: "+a[i]);
}
}
}
Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac arr.java Press Enter
* C:\>;java arr Press Enter
Output:


A Program demonstrating a Multi-Dimensional Array:

class multi
{
public static void main(String arg[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac multi.java Press Enter
* C:\>;java multi Press Enter
Output:


String: String is a set of elements stored inside a common variable.
For example: String can be defined as follows:
  1. byte a[]={65,45,36,78};
    String s=new String(a);
  2. char b[]={'d','i','p'};
    String s1=new String(b);
  3. String a="dip"; //also known as String literal

String can be Manipulated, There are lots of Predefined Functions in Java use to Manipulate a String,
The Following are the types of String Manipulation Functions:
  •  Character Type Manipulation:
    1. getchars(int source start, int source end, char target[], int target start)
    2. regionmatches(start index, string2, start index, integer number of character)
  • String Type Manipulation:
    1. length()
    2. concat()
    3. indexof()
    4. substring()
    5. replace()
    6. trim()
    7. touppercase()
    8. tolowercase()
    9. equals()
    10. equalsignorecase()
    11. reverse()
    12. regionmatches()
    13. startswith()
    14. endswith()
Examples of the above topic will be seen in the next context.....