Wednesday, August 11, 2010

Core Java

Core Java

A)    Starting with Core Java:
Java is an Object oriented programs (OOPS) as I have mentioned in the above subjects. Java is a very Robust, Simple and Architecture neutral, i.e. it supports everything we use in programming in C, Abstraction, Encapsulation, Polymorphism, and Inheritance. Although all the programs in Core java are Command line based.





B)    To Run a Basic Java Program:
1)    We have to write the java codes in a notepad file
2)    Save the notepad file with the class name used (class will be studied further) with proper K state i.e. if class dip then file name is “dip” and not “Dip” or other conventions.
3)    Extension: dip.java instead of dip.txt.
4)    Save the file to the root C:\SDK\jdk\bin>.
5)    To Compile any program in java use the follow notations:

* C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dip.java Press Enter
* C:\>java dip Press Enter


 5)    Following piece of code will print "This is a simple Java program." In the command line, to do so we have to write a program as it is:

class dip {
public static void main(String args[]) {
System.out.println("This is a simple Java program.");
}
}


In this program class is the address of the Java file, class include the name of the file carrying the java codes, public static void main is the method (function) of the class i.e. action we want as output. (String args[]) is an array or Storage of the set of codes Encapsulated inside the function, the action System.out.println tell the system to print out the command written inside the Encapsulation, println or print is to print the command onto command line. Here ln is used for new line.
Output:

C) Data Types:
There are many Data Types we have come across the C and C++ programs, similarly in java there are Data types.
1)    Simple Data type:
a)    Int: All Integer type Variables are Stored inside int data type
b)    Char: All Character type Variables are Stored inside char data type.
c)    Float: All floating-type number or decimals are Stored inside float data type
d)    Boolean: This Variable consists of only “True” or “False”.

2)    A Simple program on data type int (pre-defined):

class dip1
{
public static void main(String args[])
{
int a,b,c;
a=3;
b=4;
c=a+b;
System.out.println("Answer is: "+c);
}
}

Save the file name as it is the class name because class name is the address, without or incorrect name will not be detected by JVM and hence it will throw an Error or Exception, i.e. dip1.java
Use same notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac  dip1.java Press Enter
* C:\>java dip1 Press Enter

Output:



3)    Another Simple Program, in this we will take the input from the User (user-defined):
class dip2
{
public static void main(String arg[])
{
System.out.println("Your name is: "+arg[0]);
System.out.println("Your name is: "+arg[1]);
}
}

Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dip2.java Press Enter
* C:\>java dip2 Dip Pramanik Press Enter
Output:



Have you seen some difference between the previous program and this one?
Yes, the difference is C:\>java dip2 Dip Pramanik
Dip Pramanik is the Command line argument, it wasn’t pre-defined, it is defined by the User so called User-defined.
4)    Write a Program by taking number from the user and adding it:
class dip3
{
public static void main(String arg[])
{
int a,b,c;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[1]);
c=a+b;
System.out.println("Answer is: "+c);
}
}

Here Integer.parseInt takes the command line argument in the form of Integer only
Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dip3.java Press Enter
* C:\>java dip3 3 5 Press Enter
Output:



E)    Control-Statements:
1)    Types of Control statements
a)    If-Else condition: If-Else conditions are used when there is a condition weather this or that, its easy to use and we can  loop it too with Else-if condition.
b)    For loop: To loop or repeat a process “for” loop is used.
c)    Switch-Case: This is a User-defined statement, in this statement we ask to enter a command as command line argument to the user. This argument act as a Case, it detects the suitable case from the code and throws the output to the console.
d)    While: The logic is While (condition) do this.
e)    Do-While: The logic is Do (this) While (Condition).
f)    It seems that there is no difference but there is a large difference between while and Do-While.

2)    Write a program by using if condition where greater of two numbers.
class dip4
{
public static void main(String arg[])
{
int a,b,c;
a=Integer.parseInt(arg[0]);
b=Integer.parseInt(arg[1]);
if(a>b)
System.out.println(a+" is Greater");
else
System.out.println(b+" is Greater");
}
}

Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dip4.java Press Enter
* C:\>java dip4 3 5 Press Enter
Output:



3)    Write a program using For loop.
class dip5
{
public static void main(String arg[])
{
int c;
for(c=1;c<=5;c++)
{
System.out.println(c+"\t Dip Pramanik");
}
}
}

Here \t is used for Tab.
Use notation:
C:\>cd\sdk\jdk\bin Press Enter
* C:\>javac dip5.java Press Enter
* C:\>;java dip5 Press Enter
Output:

 -

4)    Some Assignment Questions you can try and give me reply using Comments
I will solve it to you tomorrow
a)    Write a program to print average of two number
Logic a=1   b=2
c=a+b/2
print c
b)    Write a program to swap two value
Logic a=1    b=2
                 c
c as temporary variable
c=a;
a=b;
b=c;
Output: a=2 b=1
c)    Write a program to check area of circle
Logic r=3;
Area of circle 3.14*r2
c=3.14*r*r
print c
d)    Write a program using if-else condition
Check if age<=12 print child
Age<=30 print young
Age<=45 print matured
Age>=45 print old
e)    Write a program using if-else condition
Check if Ist class,  IInd class, pass class, fail class
f)    Print odd number 1 to 10 using for loop
Logic Print c(initial value to print 1)
for(c=1;c<=10;c+2)
Print c;
g)    Print a table of the given number
Logic defines a,c,b[10];
Double d;
 a=integer.parseInt(arg[0]);
For(c=1;c<=10;c++)
{
b[]=c;
}
d=a*b[];
print axb[]=d;
Here double is an integer type and accepts ASCII symbols also.
b[10] is the array of storage 10.
h)    Print sum of five given number
Use for loop to accept 5 numbers from user
i)    Print a right angle Pascal triangle of
                         *
                       *  *
                     *  *  *
                   *  *  *  *
               *   *   *   *   *

1 comment: