Thursday, July 8, 2010

IF ELSE sample program

This program will determine the age of person if 18 above  or 18 below. the program will show message " Your qualified to vote" if the age of person is 18 above, else the program will show message " Your not qualified to vote" if the age of person is 18 below.

here is the sample program.

import java.io.*;

public class SampleIf
{
public static void main(String args[])
{
//declaration
int age=18;

if(age>=18)
   {
     System.out.println("Your Qualified to Vote");
    }
else
   {
    System.out.print("Your not Qualified to vote");
   }
}

}

Wednesday, July 7, 2010

If Statement

IF statement - is a conditional statement.

syntax:

if(conndition)
{
statement 1;
}

IF ELSE - also conditional statement that the program will choose the true statement or false statement depending on thier condition.

syntax:
if(condition)
{
statement 1;
stmt 2;
}
else
{
statement 1;
stmt 2;
}

Thursday, July 1, 2010

Sample Program Using Class and Objects

I will create a program using classes and objects. I Used two class name which are MainProgram and Message.

In class name MainProgram, the user will input thier name, and this input data will pass to class Message to print.

Program Code:

import java.io.*;
import java.util.Scanner;

public class MainProgram
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
String name="";
System.out.print("Enter your name:");
input=name.nextLine();

//calling the class
//create an object to call the method in class Message
Message x = new Message();
x.printing(name);
}
}

Class Program:

class Message
{
public void printing(String nm)
{
System.out.print("Your name is:" + nm);
}
}

If you run this program you go to the MainProgram . The mainProgram will process all class program.

Output:

Your name is: Jaypee

Compute two numbers using java

this program will compute two numbers and get the sum, difference, product and quotient. x=5, y=6.

program code:

import java.io.*;
public class Computation
{
public static void main(String args[])
{
//Declaration
int x=5;
int y=6;
int sum=0;
int diff=0;
int prod=0;
int quo=0;

//computation
sum= x + y;
diff= x - y;
prod= x * y;
quo = x / y;

//Display result
System.out.println("The sum is:" + sum);
System.out.println("The diff is: + diff);
System.out.println("The prod is: + prod);
System.out.print("The Quo is: + quo);
}
}

Tuesday, June 29, 2010

Add two numbers using java

This program will compute two numbers and get the sum.
a=90, c=80

program code:


import java.io.*;

public class compute
{
public static void main(String args[])
{
//Declaration of variables
int a=90;
int b=80;
int sum=0;

//compute the sum
sum=a+b;

System.out.print("The sum is" + sum);
}
}

Friday, June 25, 2010

Encapsulation in Java

Encapsulation - refers to the hiding of data and methods within an objects. and also protects data from corruption.
                       - easier to modify programs since one object type is modified at a time.

Abstraction in java

Abstraction refers to the ability to make a class abstract in OOP.

An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.